difference between first & peek; rest & pop

1,375 views
Skip to first unread message

Seven Hong

unread,
May 30, 2013, 12:43:14 AM5/30/13
to clo...@googlegroups.com
Hi all,

Could some one explain what's the difference between first and peek, rest and pop? For me it looks like they behave exactly same on sequences..

Thanks!

Best,

Seven Hong

Michał Marczyk

unread,
May 30, 2013, 1:00:42 AM5/30/13
to clo...@googlegroups.com
They are equivalent when operating on lists, but not so when operating
on vectors (peek = constant time last, pop = log time remove last
element) or queues (peek = first, pop = remove first element returning
a queue -- rest/next would return a seq).

Also, they refuse to work with non-list sequences (try (peek (range))
or (peek (cons :foo ())) at the REPL).

In general, they operate on clojure.lang.IPersistentStack instances.
(That's right, persistent queues implement IPersistentStack.)

Cheers,
Michał
> --
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clo...@googlegroups.com
> Note that posts from new members are moderated - please be patient with your
> first post.
> To unsubscribe from this group, send email to
> clojure+u...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en
> ---
> You received this message because you are subscribed to the Google Groups
> "Clojure" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to clojure+u...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

Ethan Brooks

unread,
Nov 9, 2017, 9:06:13 PM11/9/17
to Clojure
Also, pop throws an exception on the empty list whereas rest returns ().

Justin Smith

unread,
Nov 10, 2017, 6:50:19 AM11/10/17
to clo...@googlegroups.com
first and rest are defined in terms of position, and work on anything that can be treated as an ordered collection

peek and pop work in terms of "natural insertion order" and only work with things that behave like a stack - (so not lazy-seqs, strings, etc.)

lists push and pop from the front, vectors push and pop from the end, queues push to one end, pop from the other

--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Gary Verhaegen

unread,
Nov 10, 2017, 7:27:04 AM11/10/17
to clo...@googlegroups.com
Using a dynamic language doesn't mean you should not think in terms of (abstract) types; it only means that you're note relying on the compiler to check types for you. If anything, that should encourage you to be more disciplined in your thought processes (as you don't have anything else to rely on).

first, rest, next and conj are part of the sequence abstraction; use them when you're logically manipulating a sequence.

peek, pop and conj can be part of either the stack abstraction (if used on lists or vectors) or the queue abstraction (if used on persistent queues).

Because Clojure is highly polymorphic, some names, like conj, can be (and are) reused across abstractions, because they have very similar meanings (e.g. "add something" for conj). You still have to know what context you're using them in in order to understand exactly what they're going to do (e.g. conj on a vector or on a seq across the same vector will not do the same thing).

On 10 November 2017 at 12:49, Justin Smith <noise...@gmail.com> wrote:
first and rest are defined in terms of position, and work on anything that can be treated as an ordered collection

peek and pop work in terms of "natural insertion order" and only work with things that behave like a stack - (so not lazy-seqs, strings, etc.)

lists push and pop from the front, vectors push and pop from the end, queues push to one end, pop from the other
On Thu, Nov 9, 2017 at 6:06 PM Ethan Brooks <ethana...@gmail.com> wrote:
Also, pop throws an exception on the empty list whereas rest returns ().

On Thursday, May 30, 2013 at 12:43:14 AM UTC-4, Seven Hong wrote:
Hi all,

Could some one explain what's the difference between first and peek, rest and pop? For me it looks like they behave exactly same on sequences..

Thanks!

Best,

Seven Hong

--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to

For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to

For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages