Last active
May 5, 2016 19:56
-
-
Save paultopia/02afbbf9c165590d81c1a662da2556da to your computer and use it in GitHub Desktop.
filter one vector based on applying the predicate to a different vector, in clojure.
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 filter-by-other | |
[target test pred] | |
(let [pairs (map vector target test)] | |
(mapv first (filter #(pred (second %)) pairs)))) | |
; filters target based on applying pred to test. probably will blow up with infinite sequences, and god only knows what will | |
; happen with lazy ones. Assumes target and test are vectors of equal length, pred is a single-arity function | |
; | |
; EXAMPLE: | |
; (filter-by-other [:a :b :c :d] [1 2 3 4] even?) | |
; => [:b :d] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment