fork(1) 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. ;; (grep-n 1 #{:b :d} [:a :b :c :d :e :f :g :h])
  14.  
  15. (deftest grep-n-test
  16. (are [n pred #_=> result]
  17. (= (grep-n n pred [:a :b :c :d :e :f :g :h])
  18. result)
  19.  
  20. 1 #{:b} #_=> [:a :b :c]
  21. 1 #{:b :d} #_=> [:a :b :c :d :e]
  22. 1 #{:b :g} #_=> [:a :b :c :f :g :h]
  23. 3 #{:d} #_=> [:a :b :c :d :e :f :g]
  24. 2 #{:c :f} #_=> [:a :b :c :d :e :f :g :h]
  25. ))
  26.  
  27. (test/successful? (test/run-tests))
  28.  
Success #stdin #stdout 1.48s 86136KB
stdin
Standard input is empty
stdout
Testing clojurach.core

Ran 1 tests containing 5 assertions.
0 failures, 0 errors.