seq? vs sequential? vs coll?

1,056 views
Skip to first unread message

Mark Engelberg

unread,
Nov 26, 2012, 9:01:01 AM11/26/12
to clojure
I understand that these functions test for different interfaces, but I don't have a clear sense for which things respond differently to these predicates.  Has anyone compiled a little table of what things satisfy which predicates?

So far, I've figured out that although lists, strings, vectors, and sets all can seq:
lists are seq?, sequential? and coll?
vectors are not seq?, are sequential? and coll?
sets are not seq? and not sequential?, but are coll?
strings are not seq?, sequential? or coll?

From these examples, it appears that:
All seq? are sequential?
All sequential? are coll?

Is this really true, or have I just not found enough edge cases?

Thanks.

Jim foo.bar

unread,
Nov 26, 2012, 9:12:37 AM11/26/12
to clo...@googlegroups.com
My understanding is that everything (all data-structures) is coll?
(obviously not strings)
lists and vectors are sequential? (and coll?)
sets are only coll?
maps are map? (and coll?)

> All seq? are sequential?
Yes and also coll?

> All sequential? are coll?
Well yeah but not only...all persistent data-structures are coll? which
means that map? is also coll?

basically coll? is the top-level one, right? It tests whether something
implements 'Collection' (all clojure data-structures do)

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

Philip Potter

unread,
Nov 26, 2012, 9:19:36 AM11/26/12
to clo...@googlegroups.com
I think seq? and coll? are pretty self-explanatory - they test if it
is a valid seq or collection respectively.

Sequential seems to be an equality partition of some sort -- ie two
Sequentials with the same contents in the same order should compare
equal -- but in my view if you ever have a Sequential which is not a
java.util.List you cause yourself pain because List is also an
equality partition, and so you can break the transitive equality law.
(This affects PersistentQueue, see CLJ-1059 for details).

Phil

Jim foo.bar

unread,
Nov 26, 2012, 9:27:18 AM11/26/12
to clo...@googlegroups.com
On 26/11/12 14:19, Philip Potter wrote:
> Has anyone compiled a little table of what things satisfy which predicates?

I'm not 100% sure, but I think "Clojure Programming " has a table like
this...I can confirm as soon as I get back home...

Jim

Philip Potter

unread,
Nov 26, 2012, 9:31:14 AM11/26/12
to clo...@googlegroups.com
And to answer your original question, ISeq extends
IPersistentCollection, so all seq? should be coll?.

I *think* all concrete ISeqs are Sequential in practice, and all
Sequentials are IPersistentCollections, but I'm not 100%.

Phil

On 26 November 2012 14:01, Mark Engelberg <mark.en...@gmail.com> wrote:

Philip Potter

unread,
Nov 26, 2012, 9:31:42 AM11/26/12
to clo...@googlegroups.com
On 26 November 2012 14:27, Jim foo.bar <jimpi...@gmail.com> wrote:
> On 26/11/12 14:19, Philip Potter wrote:
>>
>> Has anyone compiled a little table of what things satisfy which
>> predicates?

I don't think I wrote this.

Jim foo.bar

unread,
Nov 26, 2012, 9:33:26 AM11/26/12
to clo...@googlegroups.com
Ooops!!!
No, Mark asked this in his first post...

sorry for the confusion!

Jim

Sean Corfield

unread,
Nov 26, 2012, 2:43:06 PM11/26/12
to clo...@googlegroups.com
A colleague showed me this page recently which seems like a nice quick reference:




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



--
Sean A Corfield -- (904) 302-SEAN
An Architect's View -- http://corfield.org/
World Singles, LLC. -- http://worldsingles.com/

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

Herwig Hochleitner

unread,
Nov 26, 2012, 4:11:08 PM11/26/12
to clo...@googlegroups.com
seq?, sequential? and coll? are the predicates for ISeq, Sequential and IPersistentCollection, respectively.

Sequential is just a marker interface which doesn't promise anything but a defined order of elements.
Since ISeq already is a seq and IPersistentCollection derives from Sequable, both will succeed in a seq call.

Additionally, seq does some coercion for native types: https://github.com/clojure/clojure/blob/master/src/jvm/clojure/lang/RT.java#L471
There is a predicate in contrib to conclusively test whether it will succeed: https://github.com/clojure/core.incubator/blob/master/src/main/clojure/clojure/core/incubator.clj#L77


2012/11/26 Sean Corfield <seanco...@gmail.com>

Philip Potter

unread,
Nov 26, 2012, 4:19:19 PM11/26/12
to clo...@googlegroups.com
On 26 November 2012 21:11, Herwig Hochleitner <hhochl...@gmail.com> wrote:
> seq?, sequential? and coll? are the predicates for ISeq, Sequential and
> IPersistentCollection, respectively.
>
> Sequential is just a marker interface which doesn't promise anything but a
> defined order of elements.
> Since ISeq already is a seq and IPersistentCollection derives from Sequable,
> both will succeed in a seq call.

A Seqable isn't necessarily a seq:

user=> (seq? [])
false
user=> (contains? (ancestors (class [])) clojure.lang.Seqable)
true

Herwig Hochleitner

unread,
Nov 26, 2012, 4:47:52 PM11/26/12
to clo...@googlegroups.com
2012/11/26 Philip Potter <philip....@gmail.com>
> Since ISeq already is a seq and IPersistentCollection derives from Sequable,
> both will succeed in a seq call.

A Seqable isn't necessarily a seq:

Didn't say that, but Sequable defines the .seq() method, so it will succeed in a seq call.

Frank Siebenlist

unread,
Nov 26, 2012, 4:51:02 PM11/26/12
to clo...@googlegroups.com, Frank Siebenlist
Guess we need a test for "seq'able" ;-)

-FS.

Sean Corfield

unread,
Nov 26, 2012, 5:38:51 PM11/26/12
to clo...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages