[Prev][Next][Index][Thread]
count
-
To: info-dylan@ai.mit.edu
-
Subject: count
-
From: Bruce Hoult <bruce@hoult.org>
-
Date: Sun, 4 Mar 2001 20:00:06 -0500 (EST)
-
Mail-Copies-To: nobody
-
Organization: ihug ( New Zealand )
-
User-Agent: MT-NewsWatcher/3.0 (PPC)
-
Xref: traf.lcs.mit.edu comp.lang.dylan:13089
Is there any particular reason that Dylan doesn't have a "count()"
function built-in?
Of course it's easy to do yourself -- including synthesising it
functionally from other operations -- but at a cost in efficiency. And
it's a common enough thing that other competitor languages such as
Common Lisp and Python have it built in.
The signature should probably be something like:
define method count(sequence, element, #key test = \=)
=> (count :: <integer>)
Examples of how you can do it using existing primitives include:
choose(curry(test, element), sequence).size
reduce1(\+, map(method(e) if(test(element, e)) 1 else 0 end, sequence)
The better way would be to rewrite choose() slightly:
for (t = 0 then if (test(element, e)) t + 1 else t end,
e in sequence)
finally
t;
end for;
What do y'all think?
-- Bruce
Follow-Ups:
- Re: count
- From: Hugh Greene <q@tardis.ed.ac.uk>
- Re: count
- From: Gabor Greif <gabor68@yahoo.com>
- Re: count
- From: Andreas Bogk <andreas@andreas.org>