Length of Sequence

1,609 views
Skip to first unread message

Peter Wolf

unread,
Jan 28, 2009, 2:15:17 PM1/28/09
to clo...@googlegroups.com
Here's a dumb question, but I can't find it in the docs:

How do I get the length of a sequence? Is there some generic way to
find the number of elements in something that might be list, map, vector
or lazy?

There must be some sort of built in function, or an idiom

Thanks
P

Kevin Downey

unread,
Jan 28, 2009, 2:17:44 PM1/28/09
to clo...@googlegroups.com
(doc count)
-------------------------
clojure.core/count
([coll])
Returns the number of items in the collection. (count nil) returns
0. Also works on strings, arrays, and Java Collections and Maps
nil
--
And what is good, Phaedrus,
And what is not good—
Need we ask anyone to tell us these things?

Chouser

unread,
Jan 28, 2009, 2:19:20 PM1/28/09
to clo...@googlegroups.com
On Wed, Jan 28, 2009 at 2:15 PM, Peter Wolf <opu...@gmail.com> wrote:
>
> How do I get the length of a sequence? Is there some generic way to
> find the number of elements in something that might be list, map, vector
> or lazy?

user=> (doc count)


-------------------------
clojure.core/count
([coll])
Returns the number of items in the collection. (count nil) returns
0. Also works on strings, arrays, and Java Collections and Maps

Note how the name and docs both cleverly avoid use of the words "size"
or "length", to help the function remain undiscovered by searches
through the source code and by uses of find-doc.

--Chouser

Peter Wolf

unread,
Jan 28, 2009, 2:36:27 PM1/28/09
to clo...@googlegroups.com
Thanks guys! I knew I could 'count' on you ;-)

Rich Hickey

unread,
Jan 28, 2009, 6:00:32 PM1/28/09
to Clojure


On Jan 28, 2:19 pm, Chouser <chou...@gmail.com> wrote:
Patch welcome for this - could mention it returns the length of
strings and arrays and the size of Java collections.

Rich

Peter Wolf

unread,
Jan 28, 2009, 6:17:38 PM1/28/09
to clo...@googlegroups.com
How about this? Needlessly wordy to make it more search-able...

clojure.core/count
([coll])
Returns the length of a list or vector, the number of keys in a map,
the size of a string, or the number of items in a sequence or
collection. (count nil) returns 0. Also works on Java Collections and
Maps.

Chouser

unread,
Feb 16, 2009, 1:39:38 AM2/16/09
to clo...@googlegroups.com
On Wed, Jan 28, 2009 at 6:17 PM, Peter Wolf <opu...@gmail.com> wrote:
>
> How about this? Needlessly wordy to make it more search-able...
>
> clojure.core/count
> ([coll])
> Returns the length of a list or vector, the number of keys in a map,
> the size of a string, or the number of items in a sequence or
> collection. (count nil) returns 0. Also works on Java Collections and
> Maps.

I think that since a patch was requested, you're welcome to send in
your CA [1], create and issue [2] and attach your patch. Otherwise
this is likely to get lost in the flood that is this discussion group.

[1] http://clojure.org/contributing
[2] http://code.google.com/p/clojure/issues

--Chouser

Reply all
Reply to author
Forward
0 new messages