TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Learning Clojure: comparing with Java streams

117 pointsby nfrankelover 6 years ago

7 comments

vlaaadover 6 years ago
Also, this bit:<p><pre><code> (-&gt;&gt; justice-league (map #(:vehicles %)) (filter #(not (nil? %))) (flatten)) </code></pre> can be simplified to just `(mapcat :vehicles justice-league)`
评论 #18378135 未加载
roenxiover 6 years ago
Not a lot of comments here for the number of upvotes, but the trend of the comments strikes me as lopsidedly negative and I&#x27;d like to throw in something positive to the mix - Clojure needs more people publishing beginner friendly code even if it isn&#x27;t using some specific function that does the same thing with less typing. So, y&#x27;know, I&#x27;m really glad we&#x27;ve got people out there who are documenting how they manage to get things to work under the new and generally unfamiliar paradigm of &quot;practically functional&quot;. Especially since Clojure&#x27;s Achilles heel is the combined double-whammy of its incoherent error messages and the learning curve for a beginner trying to pick up how to do common tasks in a world with &#x27;no variables&#x27;.<p>We have a programmer with &gt;15 years experience, who is learning a new language and in the process producing code that is neat, functional (in the sense of no bugs) and not unreasonably verbose. Clojure is a language that is dense with clever ideas, it takes a lot longer than a few months to get good at using them if they are new. The terse comments throwing out improvements or making corrections seem a little harsh, I&#x27;d hoped to see people talking more about concepts than naming functions.
评论 #18470516 未加载
评论 #18380708 未加载
Sharlinover 6 years ago
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”).
评论 #18377895 未加载
评论 #18377897 未加载
outworlderover 6 years ago
&gt; and Clojure’s syntax is pretty limited<p>Erm, what? I am confused by the use of the word &#x27;limited&#x27; in this context.
评论 #18377415 未加载
评论 #18378000 未加载
评论 #18377572 未加载
评论 #18378073 未加载
zmmmmmover 6 years ago
Out of curiosity, can anybody comment whether the Clojure versions of these are &quot;lazy&quot; 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:<p><pre><code> (first (map #(extract-name %) justice-league)) </code></pre> 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.
评论 #18379680 未加载
评论 #18383359 未加载
madmax96over 6 years ago
It seems strange to me that `(map extract-name justice-league)` isn&#x27;t listed under &quot;idiomatic improvements.&quot; Supposing you already had `extract-name` defined for some reason, it&#x27;s cleaner and simpler to just map that unary function than wrap that function in an anonymous function - whether that wrapping is done with the `#(...)` reader syntax or explicitly constructed with `fn`.
评论 #18377328 未加载
评论 #18377103 未加载
评论 #18377255 未加载
didibusover 6 years ago
Did I miss the Java counterpart of the examples?