I was proud of myself for coming up with a terse solution to @4clojure Problem 33 "Replicate a Sequence" until I discovered "interleave" doesn't accept 1 argument in older versions of clojure https://www.4clojure.com/problem/33
So for Clojure 1.6.0, my solution:
#(apply interleave (repeat %2 %1))
will satisfy the following when replacing the fill-in-the-blank symbol:
(= (__ [1 2 3] 2) '(1 1 2 2 3 3))
(= (__ [4 5 6] 1) '(4 5 6))
Unfortunately, on 4clojure, the second example above fails with an arity exception on interleave
, i.e., wrong number of arguments. Then I came across this thread citing the same issue. So 4clojure must be using an older version of Clojure to verify the results... at least I wasn't going crazy!
Rather than re-implement interleave
, my uninteresting workaround is to return the collection if n is 1:
#(if (> %2 1) (apply interleave (repeat %2 %1)) %1)
User _pcl came up with a great solution. Rather than repeating the sequences then combining them with interleave, the approach is to map the sequence onto a result that repeats each item and flatten the result. The "map then flatten" is expressed as mapcat
in Clojure
So a better solution:
#(mapcat (partial repeat %2) %1)
Log in or sign up for Devpost to join the conversation.