Flatten a Sequence

20 views
Skip to first unread message

Charlie Griefer

unread,
Jun 17, 2014, 5:51:03 PM6/17/14
to clj...@googlegroups.com
At our initial meeting at the library (2 months ago), Brandon and I worked through 4Clojure problem #28, Flatten a Sequence (see http://www.4clojure.com/problem/28).

Brandon had sent an email with the code we came up with. https://gist.github.com/bitmage/37fa6e453dd4970d4c2d

It still needed (or needs) some refactoring, but overall it worked, and was a good learning experience.

I bring it up now because I just saw somebody post their solution to twitter. Check it out: https://www.refheap.com/86732

letfn and build are new to me. Need to dig into those in the docs.

Thought you folks might find it interesting.

--
Charlie Griefer

"Give light, and the darkness will disappear of itself." 
-- Desiderius Erasmus

Paul M Bauer

unread,
Jun 17, 2014, 5:54:55 PM6/17/14
to clj...@googlegroups.com
(fn f [x] (if (coll? x) (mapcat f x) [x]))


--
You received this message because you are subscribed to the Google Groups "clj-phx" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clj-phx+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Charlie Griefer

unread,
Jun 17, 2014, 6:01:14 PM6/17/14
to clj...@googlegroups.com
Wow that’s… concise :)

Will play around with that one later on.

Andy C

unread,
Jun 18, 2014, 10:56:52 AM6/18/14
to clj...@googlegroups.com
this is my - more classic  - version:

(fn X [y]
  (reduce concat
    (map (fn [x] (if(coll? x) (X x) [x])) y)
  )
)

now I now mapcat :-). Also it seems that some ppl factor out (if (coll? ...) ...) from reduce/mapcat - interesting

Cheers,
A.

Brandon Mason

unread,
Jun 18, 2014, 1:43:17 PM6/18/14
to clj...@googlegroups.com
Concise because mapcat does most of what you would want to do with flatten.  :-)

Does anyone know if mapcat is a clojure equivalent to what they call 'flatmap' in Scala?
Brandon Mason
Torchlight Software

Paul Bauer

unread,
Jun 18, 2014, 2:38:31 PM6/18/14
to clj...@googlegroups.com, clj...@googlegroups.com
roughly, yes

Andy C

unread,
Jun 18, 2014, 2:42:14 PM6/18/14
to clj...@googlegroups.com

Torchlight

unread,
Jun 18, 2014, 11:43:34 PM6/18/14
to clj...@googlegroups.com
1 
"flatMap is the bind function in List monad" No, flatMap is the common Scala name for all bind functions. –  Alexey Romanov Dec 6 '13 at 4:25


That I suppose was the answer I was looking for.  So they are equivalent for purposes of collections.
Reply all
Reply to author
Forward
0 new messages