August 13, 2018
4clojure 144 - Oscilrate
An oscillating iterate: Takes an initial value and a variable number of functions. Returns a lazy sequence of the functions applied to the value in order, restarting from the first function after it hits the end.
(ns live.test
(:require [cljs.test :refer-macros [deftest is run-tests]]))
(defn oscilrate [v & f] (reductions #(%2 %) v (cycle f)))
(deftest oscilrate-test
(is (= (take 3 (oscilrate 3.14 int double)) [3.14 3 3.0]))
(is (= (take 5 (oscilrate 3 #(- % 3) #(+ 5 %))) [3 0 5 2 7]))
(is (= (take 12 (oscilrate 0 inc dec inc dec inc)) [0 1 0 1 0 1 2 1 2 1 2 3])))
(run-tests)