This is a cop out, but...
A function should seq the argument when it is appropriate. I've
written some functions that depends on the input NOT being a map (SQL
IN clause generation, for example), and I wouldn't want this to work
for a map.
I think the way you documented rand-elt gives you an answer, because
documentation should reflect design intent. Since you mention it
works on a seq in the docs, it should be written to work with anything
the is seqable. As you well know (others may not), rand-elt could be
written this way:
(defn rand-elt
"Return a random element of this seq"
[s]
(let [seq-s (seq s)]
(nth seq-s (rand-int (count seq-s)))))
This also has the benefit of complaining when
(instance? clojure.lang.Seqable s)
is false.
Just my $.02
Sean
On Jul 27, 12:44 pm, Stuart Halloway <[email protected]>
wrote:
> Coming from the Ruby and JavaScript worlds, I am used to having
> functions that normalize their arguments, such as the "$" function in
> various JavaScript libraries. Other library functions tend to
> automatically call "$" for you, so you don't have to remember whether
> to do it or not.
>
> One analog in Clojure is functions that work on seqs (or seq-ables).
> In general, I would rather not have to remember, and assume that most
> functions call seq internally if they need to.
>
> To make this concrete, should the following function from
> clojure.contrib.seq-utils call seq on its arg? As written, you cannot
> call it with an associative collection unless you remember to wrap it
> in a seq:
>
> (defn rand-elt
> "Return a random element of this seq"
> [s]
> (nth s (rand-int (count s))))
>
> Stu
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---