take-until

1,183 views
Skip to first unread message

Alexander Taggart

unread,
Jun 19, 2014, 11:36:59 AM6/19/14
to cloju...@googlegroups.com
It isn't a big deal, but I often find myself reimplementing this function; perhaps it's appropriate to sit alongside take-while

(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)))))))

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.

E.g.,
(take-while pos? [1 2 0 3]) => (1 2)

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






Mike Drogalis

unread,
Jun 19, 2014, 11:43:20 AM6/19/14
to cloju...@googlegroups.com
I've had this one come up a bunch of times as well. I'd be in favor of seeing this in core.


--
You received this message because you are subscribed to the Google Groups "Clojure Dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure-dev...@googlegroups.com.
To post to this group, send email to cloju...@googlegroups.com.
Visit this group at http://groups.google.com/group/clojure-dev.
For more options, visit https://groups.google.com/d/optout.

James Reeves

unread,
Jun 19, 2014, 1:05:11 PM6/19/14
to cloju...@googlegroups.com
This is something I've needed occasionally as well, but I'd mostly forgotten about it until now.

I've added take-until to Medley, in case anyone's interested.

- James

Paul deGrandis

unread,
Jun 19, 2014, 5:11:16 PM6/19/14
to cloju...@googlegroups.com
I've also reached for and written this one.

Paul

Alexander Taggart

unread,
Jun 20, 2014, 11:04:17 AM6/20/14
to cloju...@googlegroups.com
Since this seems to be of some utility, I've added a patch: http://dev.clojure.org/jira/browse/CLJ-1451

Tom Faulhaber

unread,
Jun 20, 2014, 11:16:33 AM6/20/14
to clojure-dev
If we were to add take-until to complement take-while, shouldn't we also add drop-until to complement drop-while? It seems like take and drop fns should always come in pairs.

Tom


Alexander Taggart

unread,
Jun 20, 2014, 11:20:09 AM6/20/14
to cloju...@googlegroups.com
Well spotted!  Adding it to the patch I'm putting together.

Alex Miller

unread,
Jun 20, 2014, 11:24:11 AM6/20/14
to cloju...@googlegroups.com
Please make it a separate thing.  drop-until seems like something you'd need less often?  While it sounds like take-until has been reimplemented several times, it would be telling if drop-until has not.

James Reeves

unread,
Jun 20, 2014, 12:23:33 PM6/20/14
to cloju...@googlegroups.com
Isn't (drop-until p c) just (drop-while (complement p) c)?

e.g.

(drop-until zero? [1 2 0 1])
=> [0 1]
(drop-while (complement zero?) [1 2 0 1])
=> [0 1]

The reason take-until is useful is that it's not just take-while + complement.

- James

Gary Fredericks

unread,
Jun 20, 2014, 12:39:41 PM6/20/14
to cloju...@googlegroups.com
I don't think drop-until would be the same as drop-while. Since the results of take-while and drop-while concat back into the same sequence, I think you'd want for take-until/drop-until as well, right? But since take-until/drop-until are just take-while/drop-while shifted over by one, drop-until should be implementable pretty easily with drop-while and rest.

(defn drop-until [p coll] (rest (drop-while (complement p) coll)))?

Alexander Taggart

unread,
Jun 20, 2014, 12:40:06 PM6/20/14
to cloju...@googlegroups.com, ja...@booleanknot.com
(take-until zero? [1 2 3 0 4 5 6]) => (1 2 3 0)
(drop-until zero? [1 2 3 0 4 5 6]) => (4 5 6)

James Reeves

unread,
Jun 20, 2014, 1:59:15 PM6/20/14
to cloju...@googlegroups.com
On 20 June 2014 17:40, Alexander Taggart <ma...@ataggart.ca> wrote:
(take-until zero? [1 2 3 0 4 5 6]) => (1 2 3 0)
(drop-until zero? [1 2 3 0 4 5 6]) => (4 5 6)

Yeah, okay, that makes sense.

- James

Philip Potter

unread,
Jun 21, 2014, 7:07:08 AM6/21/14
to cloju...@googlegroups.com
Just to raise a contrary viewpoint on the naming. My expectation
based on the name "take-until" is that it's precisely (take-while
(complement p) ...), and does *not* include the first element which
passes the predicate.

I see while/until as being strictly about complementing the sense of
the predicate. My viewpoint is partly formed by being an old perl
hacker, and in perl until is just "while not":
http://perldoc.perl.org/perlsyn.html#Compound-Statements

That said, I definitely think there is value in this function; I have
implemented similar functionality with loop/recur before but this fn
would have made for a much more clear and concise definition.

Julian Gindi

unread,
Jun 21, 2014, 4:14:37 PM6/21/14
to cloju...@googlegroups.com
I'm going to agree with that. I interpret the meaning of "take-until" to be "Grab all the elements of some seq until some predicate is met, and return all the values before than". 

James Reeves

unread,
Jun 21, 2014, 4:25:03 PM6/21/14
to cloju...@googlegroups.com
Perhaps "take-upto" and "drop-upto" might be better names?

(take-upto zero? [1 2 3 0 4 5 6])  =>  [1 2 3 0]
(drop-upto zero? [1 2 3 0 4 5 6])  =>  [4 5 6]

To me, "upto" implies inclusivity more than "until", e.g. "upto age 18", "until age 18".

- James

John Mastro

unread,
Jun 21, 2014, 6:07:43 PM6/21/14
to cloju...@googlegroups.com
James Reeves <ja...@booleanknot.com> wrote:
> Perhaps "take-upto" and "drop-upto" might be better names?
>
> (take-upto zero? [1 2 3 0 4 5 6])  =>  [1 2 3 0]
> (drop-upto zero? [1 2 3 0 4 5 6])  =>  [4 5 6]
>
> To me, "upto" implies inclusivity more than "until", e.g. "upto age 18", "until age 18".

"Through" is the clearest way to signal inclusivity, in my opinion. So
maybe take-through?

- John

Alex Miller

unread,
Jun 21, 2014, 6:50:20 PM6/21/14
to cloju...@googlegroups.com
I would leave the ticket as is and include these as alternative names in the description. Rich will make any final decision on something like that.

Sean Corfield

unread,
Jun 21, 2014, 9:29:06 PM6/21/14
to cloju...@googlegroups.com
+1 on "upto" - my initial reaction on take-until was exactly the same as Philip's.
signature.asc

Gary Fredericks

unread,
Jun 21, 2014, 9:46:57 PM6/21/14
to cloju...@googlegroups.com
I am probably Wrong about this but my sense of "-upto" is that it almost sounds index-based ("take up to the third element"), while "-while" and "-until" sound much more predicate-based.

Sean Corfield

unread,
Jun 21, 2014, 9:49:27 PM6/21/14
to cloju...@googlegroups.com
On Jun 21, 2014, at 6:46 PM, Gary Fredericks <frederi...@gmail.com> wrote:
> I am probably Wrong about this but my sense of "-upto" is that it almost sounds index-based ("take up to the third element"), while "-while" and "-until" sound much more predicate-based.

Oh I'll concede it's not ideal - just better than "until" for this case :) I'm at a loss to suggest something better. I'm not convinced by "through" either.

Sean Corfield -- (904) 302-SEAN
An Architect's View -- http://corfield.org/

"Perfection is the enemy of the good."
-- Gustave Flaubert, French realist novelist (1821-1880)



signature.asc

James Reeves

unread,
Jun 21, 2014, 10:06:12 PM6/21/14
to cloju...@googlegroups.com
On 22 June 2014 02:46, Gary Fredericks <frederi...@gmail.com> wrote:
I am probably Wrong about this but my sense of "-upto" is that it almost sounds index-based ("take up to the third element"), while "-while" and "-until" sound much more predicate-based.

It does sound a little index-y, but since we already have "take" and "drop" for that, it doesn't seem like it would be a major source of confusion.

There's no good English word (I can think of) that means "up to and including", so any word chosen will be inaccurate to some degree.

- James

Julian Gindi

unread,
Jun 21, 2014, 10:10:28 PM6/21/14
to cloju...@googlegroups.com, ja...@booleanknot.com
I'd be interested to see what other languages implement similar functionality, and what they label it. 

James Reeves

unread,
Jun 21, 2014, 10:22:19 PM6/21/14
to cloju...@googlegroups.com
I looked around for similar functions in other languages, but came up empty. If anyone can find some, I'd be interested to know.

- James


--

John Mastro

unread,
Jun 21, 2014, 10:48:59 PM6/21/14
to cloju...@googlegroups.com
Sean Corfield <se...@corfield.org> wrote:
> Oh I'll concede it's not ideal - just better than "until" for this
> case :) I'm at a loss to suggest something better. I'm not convinced
> by "through" either.

I think any of the suggestions here would be fine, but in retrospect I
should have explained my thinking in my original email. I didn't, so
I'll offer it here.

I thought of "through" in the context of dates. If I say "I'll be gone
until the first", I mean the sequence of dates on which I'll be gone
ends immediately before the first. On the other hand, if I say "I'll be
gone through the first", then that sequence of dates includes the first.

Hardly definitive, but that's where the thought came from. I asked
Google to define "through" for me, and it does mention inclusive ranges,
but only if you click the downward-pointing arrow to expand it, and it
indicates the usage is specific to North America.

- John

James Reeves

unread,
Jun 22, 2014, 9:28:32 AM6/22/14
to cloju...@googlegroups.com
"I'll be gone through the first" doesn't sound right to me. It sounds like a colloquialism.

Also, "drop through zero" doesn't read well to me, unlike "drop while zero" or "drop up to zero".

- James


--
Reply all
Reply to author
Forward
0 new messages