Welcome! Please see the About page for a little more info on how this works.

+3 votes
in Transducers by

Discussion: https://groups.google.com/d/topic/clojure-dev/NaAuBz6SpkY/discussion

It comes up when I would otherwise use (take-while pred coll), but I need to include the first item for which (pred item) is false.

(take-while pos? [1 2 0 3]) => (1 2) (take-until zero? [1 2 0 3]) => (1 2 0)

Patch: clj-1451.patch

  • Includes transducer arity of take-until
  • Includes inclusion in transducer generative tests

21 Answers

0 votes
by

Comment made by: gshayban

I feel like this is superceded by CLJ-1906

0 votes
by

Comment made by: gshayban

And this is definitely superseded by halt-when

0 votes
by

Comment made by: alexmiller

It's not lazy but this is one way to write take-until with halt-when:

(defn take-until [p s] (transduce (halt-when p (fn [r h] (conj r h))) conj [] s))

0 votes
by

Comment made by: steveminer@gmail.com

I wanted to suggest: (sequence (halt-when p conj) s) but sequence doesn't support stopping short on a reduced value so that won't work.

0 votes
by

Comment made by: alexmiller

Yeah, halt-when is a little tricky to use in transducible contexts other than transduce.

0 votes
by
Reference: https://clojure.atlassian.net/browse/CLJ-1451 (reported by ataggart)
...