Hi,
I just solved the "Replicate a Sequence" problem on 4clojure [1] using
"interleave". However, I noticed that "interleave" cannot be called with
a single argument. My first attempt at solving the problem was like
this:
(defn replicate-n-times [xs n]
(apply interleave (replicate n xs)))
This works fine for n > 1:
clojure=> (replicate-n-times [1 2 3] 3)
(1 1 1 2 2 2 3 3 3)
However, for n = 1 the function fails with an exception:
clojure=> (replicate-n-times [1 2 3] 1)
ArityException Wrong number of args (1) passed to: core$interleave
clojure.lang.AFn.throwArity (AFn.java:437)
I had to add a special case to make this work:
(fn replicate-n-times [xs n]
(if (= n 1) xs (apply interleave (replicate n xs))))
I think it would be nice if "interleave" called with a single argument
would just return the argument itself, just like single-argument "+" and
"*" do.
Regards,
Denis Washington
[1] http://www.4clojure.com/problem/33
--
--
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
---
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 [email protected].
For more options, visit https://groups.google.com/groups/opt_out.