Why does peek on a seq of vector fail?

263 views
Skip to first unread message

Jason Gilman

unread,
Jun 20, 2013, 3:54:43 PM6/20/13
to clo...@googlegroups.com
Why does (peek (seq [1])) result in: 
ClassCastException clojure.lang.PersistentVector$ChunkedSeq cannot be cast to clojure.lang.IPersistentStack  clojure.lang.RT.peek (RT.java:634)

Peek documentation "For a list or queue, same as first, for a vector, same as, but much more efficient than, last. If the collection is empty, returns nil." implies that there shouldn't be a problem working with vectors.

This works without issue:
(peek [1]) 

This is on Clojure 1.5.1

Jason Gilman

unread,
Jun 20, 2013, 4:11:23 PM6/20/13
to clo...@googlegroups.com
Also, I really appreciate anyone who takes the time to answer. My company is currently evaluating Clojure. I'm trying to determine if this is non-idiomatic use of the language or some other issue. Doing something like this can cause the bug to occur without directly calling seq on a vector:

(defn bar [my-list n]
  (if (= n 0)
      (peek my-list)
      (bar (rest my-list) (dec n))))
(bar [1 2 3] 1)

Both rest and peek work on a vector without a problem but the combination of the two causes problems.

David Nolen

unread,
Jun 20, 2013, 4:31:28 PM6/20/13
to clojure
Calling seq on vector returns a sequence which is no longer a vector. The docstring is pretty specific about which types it's supposed to work on.

clojure.core/peek
([coll])
  For a list or queue, same as first, for a vector, same as, but much
  more efficient than, last. If the collection is empty, returns nil.

David


--
--
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.
 
 

László Török

unread,
Jun 20, 2013, 4:31:59 PM6/20/13
to clo...@googlegroups.com
Hi, 

I think it's by design. 

It only works with concrete data structure (list, vector, queue) types that implement 

clojure.lang.IPersistentStack

Las




2013/6/20 Jason Gilman <jason....@gmail.com>

--
--
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.
 
 



--
László Török

John D. Hume

unread,
Jun 20, 2013, 5:14:19 PM6/20/13
to clo...@googlegroups.com

On Jun 20, 2013 3:11 PM, "Jason Gilman" <jason....@gmail.com> wrote:
>
> (defn bar [my-list n]
>   (if (= n 0)
>       (peek my-list)
>       (bar (rest my-list) (dec n))))
> (bar [1 2 3] 1)
>

It seems likely you want either first and rest* (to work from the front of any seqable) or peek and pop (to work from the back of a vector or the front of a list or queue). Combining peek and rest on a vector as the bar function does makes no sense, since they work at opposite ends.

*or next in place of rest.

Jason Gilman

unread,
Jun 20, 2013, 6:22:45 PM6/20/13
to clo...@googlegroups.com
I think I get the difference now between a sequence, list, and a vector. Thanks for the quick answers. 
--
--
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 a topic in the Google Groups "Clojure" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/clojure/7DzLLsmnm-E/unsubscribe.
To unsubscribe from this group and all its topics, send an email to clojure+u...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages