Skip to content

Instantly share code, notes, and snippets.

@paultopia
Last active May 5, 2016 19:56
Show Gist options
  • Save paultopia/02afbbf9c165590d81c1a662da2556da to your computer and use it in GitHub Desktop.
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.
(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