September 20, 2018
Just Juxt #37: Re-implement Map (4clojure #118)
Map is one of the core elements of a functional programming language. Given a function
f
and an input sequences
, return a lazy sequence of(f x)
for each elementx
ins
.
(ns live.test
(:require [cljs.test :refer-macros [deftest is testing run-tests]]))
(defn juxt-map [f s]
(flatten (keep (juxt f) s)))
(deftest juxt-map-test
(is (= [3 4 5 6 7]
(juxt-map inc [2 3 4 5 6])))
(is (= (repeat 10 nil)
(juxt-map (fn [_] nil) (range 10))))
(is (= [1000000 1000001]
(->> (juxt-map inc (range))
(drop (dec 1000000))
(take 2)))
(= [1000000 1000001]
(->> (juxt-map inc (range))
(drop (dec 1000000))
(take 2)))))
(comment "uncomment to run tests - this may take awhile..."
(run-tests))