Clojure

Add take-until

Details

  • Type: Enhancement Enhancement
  • Status: Open Open
  • Priority: Minor Minor
  • Resolution: Unresolved
  • Affects Version/s: None
  • Fix Version/s: None
  • Component/s: None
  • Labels:
    None
  • Patch:
    Code and Test
  • Approval:
    Triaged

Description

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)

Impl:

(defn take-until
  "Returns a lazy sequence of successive items from coll until
  (pred item) returns true, including that item. pred must be
  free of side-effects."
  [pred coll]
  (lazy-seq
    (when-let [s (seq coll)]
      (if (pred (first s))
        (cons (first s) nil)
        (cons (first s) (take-until pred (rest s)))))))

List of other suggested names: take-upto, take-to, take-through. It is not easy to find something in English that is short and unambiguously means "up to and including". That is one of the dictionary definitions for "through".

  1. 0001-CLJ-1451-add-take-until.patch
    18/Dec/14 1:52 AM
    4 kB
    Michael Blume
  2. 0001-CLJ-1451-add-take-until.patch
    13/Nov/14 11:54 PM
    2 kB
    Michael Blume
  3. 0002-CLJ-1451-add-drop-until.patch
    13/Nov/14 11:54 PM
    2 kB
    Michael Blume
  4. 0003-let-take-until-and-drop-until-return-transducers.patch
    14/Nov/14 12:11 AM
    3 kB
    Michael Blume
  5. CLJ-1451-drop-until.patch
    20/Jun/14 3:12 PM
    2 kB
    Alexander Taggart
  6. CLJ-1451-take-until.patch
    20/Jun/14 3:12 PM
    2 kB
    Alexander Taggart

Activity

Hide
Alex Miller added a comment -

Patch welcome (w/tests).

Show
Alex Miller added a comment - Patch welcome (w/tests).
Hide
Alexander Taggart added a comment -

Impl and tests for take-until and drop-until, one patch for each.

Show
Alexander Taggart added a comment - Impl and tests for take-until and drop-until, one patch for each.
Hide
Jozef Wagner added a comment -

Please change :added metadata to "1.7".

Show
Jozef Wagner added a comment - Please change :added metadata to "1.7".
Hide
Alexander Taggart added a comment -

Updated to :added "1.7"

Show
Alexander Taggart added a comment - Updated to :added "1.7"
Hide
John Mastro added a comment -

I'd like to propose take-through and drop-through as alternative names. I think "through" communicates more clearly how these differ from take-while and drop-while.

Show
John Mastro added a comment - I'd like to propose take-through and drop-through as alternative names. I think "through" communicates more clearly how these differ from take-while and drop-while.
Hide
Andy Fingerhut added a comment -

Both patches CLJ-1451-drop-until.patch and CLJ-1451-take-until.patch dated Jun 20 2014 no longer apply cleanly to latest Clojure master due to some changes committed earlier today. I haven't checked whether they are straightforward to update, but would guess that they merely require updating a few lines of diff context.

See the section "Updating stale patches" at http://dev.clojure.org/display/community/Developing+Patches for suggestions on how to update patches.

Show
Andy Fingerhut added a comment - Both patches CLJ-1451-drop-until.patch and CLJ-1451-take-until.patch dated Jun 20 2014 no longer apply cleanly to latest Clojure master due to some changes committed earlier today. I haven't checked whether they are straightforward to update, but would guess that they merely require updating a few lines of diff context. See the section "Updating stale patches" at http://dev.clojure.org/display/community/Developing+Patches for suggestions on how to update patches.
Hide
Ghadi Shayban added a comment -

Would be nice to cover the transducer case too.

Show
Ghadi Shayban added a comment - Would be nice to cover the transducer case too.
Hide
Michael Blume added a comment -

rerolled patches

Show
Michael Blume added a comment - rerolled patches
Hide
Michael Blume added a comment -

Covered transducer case =)

Show
Michael Blume added a comment - Covered transducer case =)
Hide
Michael Blume added a comment -

Actually I like take/drop-through as well

Show
Michael Blume added a comment - Actually I like take/drop-through as well
Hide
Ghadi Shayban added a comment -

Michael, no volatile/state is necessary in the transducer, like take-while. Just wrap in 'reduced to terminate

Show
Ghadi Shayban added a comment - Michael, no volatile/state is necessary in the transducer, like take-while. Just wrap in 'reduced to terminate
Hide
Michael Blume added a comment -

a) you're clearly right about take-until

b) seriously I don't know what I was thinking with my take-until implementation, I'm going to claim lack of sleep.

c) I'm confused about how to make drop-until work without a volatile

Show
Michael Blume added a comment - a) you're clearly right about take-until b) seriously I don't know what I was thinking with my take-until implementation, I'm going to claim lack of sleep. c) I'm confused about how to make drop-until work without a volatile
Hide
Michael Blume added a comment -

Ghadi and I discussed this and couldn't think of a use case for drop-until. Are there any?

Here's a new take-until patch, generative tests included.

Open questions:

Is take-until a good name? My biggest concern is that take-until makes it sound like a slight modification of take, but this function reverses the sense of the predicate relative to take.

Show
Michael Blume added a comment - Ghadi and I discussed this and couldn't think of a use case for drop-until. Are there any? Here's a new take-until patch, generative tests included. Open questions: Is take-until a good name? My biggest concern is that take-until makes it sound like a slight modification of take, but this function reverses the sense of the predicate relative to take.

People

Vote (2)
Watch (3)

Dates

  • Created:
    Updated: