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. CLJ-1451-drop-until.patch
    20/Jun/14 3:12 PM
    2 kB
    Alexander Taggart
  2. 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.

People

Vote (0)
Watch (2)

Dates

  • Created:
    Updated: