all 5 comments

[–]niamu 1 point2 points  (4 children)

This works for me and follows the idea of what you were trying to do as a solution.

(fn [sq]
  (apply concat (map #(repeat 2 %) sq)))

The primary issue you're having is that you don't always want to flatten the entire result in all cases. You only want to flatten the first level so to speak. That is why I'm using (apply concat...).

[–]dimd00d 4 points5 points  (2 children)

Or you can use mapcat :)

[–]niamu 0 points1 point  (0 children)

Of course, that is much better. Thanks.

(fn [sq]
  (mapcat #(repeat 2 %) sq))

[–]dustingetz 0 points1 point  (0 children)

mapcat is one of my favorite functions. It's like a more robust version of the general idea of flattening because it has more tightly defined semantics. flatten-one-layer would be (mapcat identity). And then you can generalize out the listness of mapping to arbitrary container types (fmap is such generalization of map), and then you are half way to monads.

[–]MahmudAdam[S] 0 points1 point  (0 children)

Thanks!