Last active
September 24, 2015 20:02
-
-
Save mbertheau/9089f1a5c5e1c83130d2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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"))) |
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.
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
Someone suggested elsewhere:
(conj (into ["begin"] (mapcat f [\a \b \c])) "end")