The equivalent of `flatMap` is not `flatten`. Semantically `flatMap` is equivalent to `(comp flatten map)` – hence the name! – but Clojure does offer it as a single function named `mapcat` (”map and catenate”).
Flatten is a steam-roller and should be used wisely: it recursively flatten a sequential collection. Most of the time this is not what you want. Most of the rest of the time when you think that’s what you want it’s a code smell telling you that the shape of your items has gone out of control. And there’s the rare occurrence where you can really use it.
Learning Clojure: comparing with Java streams
21–30 of 46 posts
Re: Learning Clojure: comparing with Java streams
#22The equivalent of `flatMap` is not `flatten`. Semantically `flatMap` is equivalent to `(comp flatten map)` – hence the name! – but Clojure does offer it as a single function named `mapcat` (”map and catenate”).
> Semantically `flatMap` is equivalent to `(comp flatten map)` With the caveat that `flatten` is recursive and can handle a mix of collections and non-collections, while `mapcat` is non-recursive and every value returned by the mapcatted function must be a collection or nil. (Identity returns its argument.) (flatten [1 [2 3] [[4 5 6]]]) > [1 2 3 4 5 6] (mapcat identity [1 [2 3] [[4 5 6]]]) > IllegalArgumentException…
Re: Learning Clojure: comparing with Java streams
#23Re: Learning Clojure: comparing with Java streams
#24Earlier quoted context omitted.
Actually, Clojure does not have reader macros, because it was thought to be the biggest cause of the lisp curse.
Nit: Clojure does not have user-defined reader macros. There are quite a few built-in reader macros, including #? added for reader conditionals.
Re: Learning Clojure: comparing with Java streams
#25 (first (map #(extract-name %) justice-league))
Does it map all the elements or only the first? This is actually the key aspect of streams, and more important in many ways than whatever syntax sugar is available to invoke the operations.Re: Learning Clojure: comparing with Java streams
#26Out of curiosity, can anybody comment whether the Clojure versions of these are "lazy" in the sense that if you only take the first element from the result, does the the stream process all the results or just the first? eg: (first (map #(extract-name %) justice-league)) Does it map all the elements or only the first? This is actually the key aspect of streams, and more important in many ways than whatever syntax suga…
Re: Learning Clojure: comparing with Java streams
#27Earlier quoted context omitted.
Doesn't Clojure have reader macros? The whole point of lisps is that you can extend them.
When I worked with Clojure the community seemed to treat macros as a nuclear option. Only use them when absolutely necessary. For some, this translated to “you can’t hug your children with nuclear arms”. If the community hasn’t changed, perhaps macros out of favor.
A good example of code with macro overkill is compojure-api. There is just so many clever macros that you need to read the original source code with a microscope to know what the code expands to.
Re: Learning Clojure: comparing with Java streams
#28Earlier quoted context omitted.
Doesn't Clojure have reader macros? The whole point of lisps is that you can extend them.
Actually, Clojure does not have reader macros, because it was thought to be the biggest cause of the lisp curse.
Re: Learning Clojure: comparing with Java streams
#29> and Clojure’s syntax is pretty limited Erm, what? I am confused by the use of the word 'limited' in this context.
lisps can be thought of languages that avoid syntax. the code you write is more like a high level AST.
Re: Learning Clojure: comparing with Java streams
#30> and Clojure’s syntax is pretty limited Erm, what? I am confused by the use of the word 'limited' in this context.
I confirm: the syntax is limited. There are only a few keywords and langage constructs. The power is in the number of functions made available, or the ones you create for yourself.