-
-
Save munro/88c578c7eb0551daacf9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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