Skip to content

Instantly share code, notes, and snippets.

@mikebridge
Created March 14, 2012 02:25
Show Gist options
  • Save mikebridge/2033514 to your computer and use it in GitHub Desktop.
Save mikebridge/2033514 to your computer and use it in GitHub Desktop.
Codelesson Clojure Assignment 1
(ns codelesson.assignment-1)
;; Write a function called map-while which accepts a function f, a sequence s, and a predicate pred. When applied, it collects the results of applying f to s as long as applying pred to elements returns true.
(defn map-while [f s pred]
(map f (take-while pred s)))
@mikebridge
Copy link
Author

(ns codelesson.test.assignment-1
  (:use codelesson.assignment-1)
  (:use clojure.test))

(deftest match-only-predicate
  (is (= (map-while identity [1 2 3 4] (partial not= 3))
         [1 2])))

(deftest stop-when-nonmatching-encountered
  (is (= (map-while identity [1 2 3 1] (partial not= 3))
       [1 2])))

(deftest stops-when-no-matches
  (is (= (map-while identity [1 2 3 4] (partial not= 100))
         [1 2 3 4])))

(deftest handles-infinite-seq
  (is (= (map-while identity (range) (partial not= 100000))
         (range 100000))))

(deftest handles-nil
  (is (= (map-while identity nil (partial not= 100000))
         [])))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment