Skip to content

Instantly share code, notes, and snippets.

@nodename
Created February 26, 2012 19:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nodename/1918635 to your computer and use it in GitHub Desktop.
Save nodename/1918635 to your computer and use it in GitHub Desktop.
H%5: map-while
(ns ex5.map-while-spec
(:use ex5.map-while)
(:use clojure.test))
(deftest test-map-while-1
(let [f #(* % %)
s (range 100)
pred #(< % 50)
result (map-while-1 f s pred)]
(is (= (count result) 50))
(is (= (first result) 0))
(is (= (last result) (f 49)))))
(deftest test-map-while-2
(let [f #(* % %)
s (range 100)
pred #(< % 50)
result (map-while-2 f s pred)]
(is (= (count result) 50))
(is (= (first result) 0))
(is (= (last result) (f 49)))))
(deftest test-map-while-1-again
(let [f #(* % %)
s '(20 40 60 35 10)
pred #(< % 50)
result (map-while-1 f s pred)]
(is (= (count result) 2))
(is (= (first result) (f 20)))
(is (= (last result) (f 40)))))
(deftest test-map-while-2-again
(let [f #(* % %)
s '(20 40 60 35 10)
pred #(< % 50)
result (map-while-2 f s pred)]
(is (= (count result) 2))
(is (= (first result) (f 20)))
(is (= (last result) (f 40)))))
(ns ex5.map-while)
(defn map-while-1 [f s pred]
(for [x s :while (pred x)] (f x)))
(defn map-while-2 [f s pred]
(map f (take-while pred s)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment