fork download
  1. (ns clojurach.core
  2. (:require [clojure.test :as test :refer [deftest testing is are]]))
  3.  
  4. (defn grep-n [n pred coll]
  5. (->> coll
  6. (map-indexed (fn [i x] (clojure.lang.MapEntry. i x)))
  7. (partition (-> n (* 2) inc) 1)
  8. (filter #(-> % (nth n) val pred))
  9. (apply concat)
  10. distinct
  11. (map val)))
  12.  
  13. (deftest grep-n-test
  14. (is (= '(0) (grep-n 1 #{0} (range 1))))
  15. (is (= '(0 1 2) (grep-n 2 #{2} (range 3)))))
  16.  
  17. ;; hack :)
  18.  
  19. (defn grep-n-hack [n pred coll]
  20. (->> (concat (repeat n ::nil) coll (repeat n ::nil))
  21. (map-indexed (fn [i x] (clojure.lang.MapEntry. i x)))
  22. (partition (-> n (* 2) inc) 1)
  23. (filter #(some-> % (nth n) val pred))
  24. (apply concat)
  25. distinct
  26. (map val)
  27. (remove #{::nil})))
  28.  
  29. (deftest grep-n-hack-test
  30. (is (= '(0) (grep-n-hack 1 #{0} (range 1))))
  31. (is (= '(0 1 2) (grep-n-hack 2 #{2} (range 3)))))
  32.  
  33. (test/successful? (test/run-tests))
Success #stdin #stdout 1.58s 89124KB
stdin
Standard input is empty
stdout
Testing clojurach.core

FAIL in (grep-n-test) (prog.clj:14)
expected: (= (quote (0)) (grep-n 1 #{0} (range 1)))
  actual: (not (= (0) ()))

FAIL in (grep-n-test) (prog.clj:15)
expected: (= (quote (0 1 2)) (grep-n 2 #{2} (range 3)))
  actual: (not (= (0 1 2) ()))

Ran 2 tests containing 4 assertions.
2 failures, 0 errors.