Skip to content

Instantly share code, notes, and snippets.

@mbertheau
Last active September 24, 2015 20:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mbertheau/9089f1a5c5e1c83130d2 to your computer and use it in GitHub Desktop.
Save mbertheau/9089f1a5c5e1c83130d2 to your computer and use it in GitHub Desktop.
(defn f [x]
(vec (repeat (rand-int 5) [x])))
(defn g []
(-> []
(into ["begin"])
(into (apply concat (for [i [\a \b \c]]
(f i))))
(into ["end"])))
;; a little bit better
(defn g2 []
(-> []
(conj "begin")
(into (reduce into (for [i [\a \b \c]]
(f i))))
(conj "end")))
@mbertheau
Copy link
Author

Someone suggested elsewhere: (conj (into ["begin"] (mapcat f [\a \b \c])) "end")

@pkobrien
Copy link

I think this does a better job of expressing the intent of the function, in so far as I understand what you are trying to do:

(defn g []
  (cons "begin" (conj (into [] (mapcat f [\a \b \c])) "end")))

Note that this version returns a lazy sequence, whereas yours returns a vector. Not sure if that matters to you or not, but you should be aware of lazy vs. non-lazy for such things so that you don't get bitten without knowing it. You could take the code I posted and thread it if you wanted, depending on what the real code will be doing. But that's just syntactical sugar, for the most part.

@mbertheau
Copy link
Author

Yeah, I want to get a vector for hiccup out of that. Thanks for your input!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment