Clojure

Add take-until

Details

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

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)

Patch: clj-1451.patch

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

Prescreened by: Alex Miller

  1. 0001-CLJ-1451-add-take-until.patch
    18/Dec/14 1:52 AM
    4 kB
    Michael Blume
  2. 0002-CLJ-1451-add-drop-until.patch
    13/Nov/14 11:54 PM
    2 kB
    Michael Blume
  3. 0003-let-take-until-and-drop-until-return-transducers.patch
    14/Nov/14 12:11 AM
    3 kB
    Michael Blume
  4. clj-1451.patch
    11/Mar/16 2:49 PM
    5 kB
    Alex Miller
  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.
Hide
Andy Fingerhut added a comment - - edited

Michael, while JIRA can handle multiple attachments for the same ticket with the same name, it can get confusing for people trying to determine which one with the same name is meant. Could you remove or rename one of your identically-named attachments? Instructions for deleting patches are in the "Removing patches" section on this wiki page: http://dev.clojure.org/display/community/Developing+Patches

Show
Andy Fingerhut added a comment - - edited Michael, while JIRA can handle multiple attachments for the same ticket with the same name, it can get confusing for people trying to determine which one with the same name is meant. Could you remove or rename one of your identically-named attachments? Instructions for deleting patches are in the "Removing patches" section on this wiki page: http://dev.clojure.org/display/community/Developing+Patches
Hide
Alex Miller added a comment -

The patch was slightly stale so I updated to apply to master, but it's almost identical. Attribution retained.

Marked as prescreened.

Show
Alex Miller added a comment - The patch was slightly stale so I updated to apply to master, but it's almost identical. Attribution retained. Marked as prescreened.
Hide
Ghadi Shayban added a comment -

I feel like this is superceded by CLJ-1906

Show
Ghadi Shayban added a comment - I feel like this is superceded by CLJ-1906
Hide
Ghadi Shayban added a comment -

And this is definitely superseded by `halt-when`

Show
Ghadi Shayban added a comment - And this is definitely superseded by `halt-when`
Hide
Alex Miller added a comment -

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))
Show
Alex Miller added a comment - 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))
Hide
Steve Miner added a comment -

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.

Show
Steve Miner added a comment - 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.
Hide
Alex Miller added a comment -

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

Show
Alex Miller added a comment - Yeah, halt-when is a little tricky to use in transducible contexts other than transduce.

People

Vote (6)
Watch (4)

Dates

  • Created:
    Updated: