Skip to content

Instantly share code, notes, and snippets.

@craftybones
Last active December 31, 2016 05:32
Show Gist options
  • Save craftybones/399dfcd2cf074e1fcb4d535f876a92fe to your computer and use it in GitHub Desktop.
Save craftybones/399dfcd2cf074e1fcb4d535f876a92fe to your computer and use it in GitHub Desktop.
(defn take-while-adjacent
[f coll]
(lazy-seq
(when-let [s (seq coll)]
(if (f (first s) (second s))
(cons (first s) (take-while-adjacent f (rest s)))
(cons (first s) nil)))))
; => (take-while-adjacent not= [1 2 3 3])
; (1 2 3)
; => (take-while-adjacent = [1 1 1 2])
; (1 1 1)
; => (take-while-adjacent square-of [2 4 16 256 100])
; (2 4 16 256)
; => (take-while-adjacent subset-of [1 [1] [[1] [1 2]] [4]])
; (1 [1] [[1] [1 2]])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment