(ns clojurach.core
  (:require [clojure.test :as test :refer [deftest testing is are]]))

(defn grep-n [n pred coll]
  (->> coll
       (map-indexed (fn [i x] (clojure.lang.MapEntry. i x)))
       (partition (-> n (* 2) inc) 1)
       (filter #(-> % (nth n) val pred))
       (apply concat)
       distinct
       (map val)))

;; (grep-n 1 #{:b :d} [:a :b :c :d :e :f :g :h])

(deftest grep-n-test
  (are [n pred #_=> result]
    (= (grep-n n pred [:a :b :c :d :e :f :g :h])
       result)

    1 #{:b}    #_=> [:a :b :c]
    1 #{:b :d} #_=> [:a :b :c :d :e]
    1 #{:b :g} #_=> [:a :b :c :f :g :h]
    3 #{:d}    #_=> [:a :b :c :d :e :f :g]
    2 #{:c :f} #_=> [:a :b :c :d :e :f :g :h]
    ))

(test/successful? (test/run-tests))
