Skip to content

Instantly share code, notes, and snippets.

@munro
Created December 29, 2015 06:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save munro/88c578c7eb0551daacf9 to your computer and use it in GitHub Desktop.
Save munro/88c578c7eb0551daacf9 to your computer and use it in GitHub Desktop.
(defn zip-against [values & {:keys [context test] :or {test =}}]
"Conditionally zip against a second sequence."
(loop [result []
index 0
value (first values)
remain (rest values)
context-item (first context)
context-rest (rest context)]
(cond
(empty? context-rest) nil
(test value context-item) (if (empty? remain)
(conj result [index value])
(recur (conj result [index value])
(+ 1 index)
(first remain)
(rest remain)
(first context-rest)
(rest context-rest)))
:else (recur result (+ index 1) value remain (first context-rest) (rest context-rest)))))
(deftest zip-against-test
(is (= (zip-against '(e l o w l) :context '(h e l l o w o r l d))
[[1 'e] [2 'l] [4 'o] [5 'w] [8 'l]]))
(is (= (zip-against '(e l o w l z) :context '(h e l l o w o r l d))
nil)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment