Created
February 26, 2012 19:49
-
-
Save nodename/1918635 to your computer and use it in GitHub Desktop.
H%5: map-while
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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))))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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