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.

Design Patterns in Clojure

155 pointsby susi22over 9 years ago

12 comments

kilinkover 9 years ago
I like Clojure quite a bit more than Java, but some of these are unfair if you are comparing against modern Java.<p>For instance, the sorting example can be simplified:<p><pre><code> Comparator&lt;User&gt; userOrdering = Comparator.comparing(User::isSubscription).thenComparing(User::getName); &#x2F;&#x2F; forward sort Collections.sort(users, userOrdering); &#x2F;&#x2F; reverse sort Collections.sort(users, userOrdering.reversed()); </code></pre> A lot less verbose than it&#x27;s made out to be.
评论 #10764714 未加载
评论 #10764747 未加载
评论 #10765146 未加载
jphover 9 years ago
Brilliant-- this is among the best design pattern writeups I&#x27;ve ever seen, and will greatly help developers who are coming to Clojure.<p>This would be excellent for expanding to book-length guide to help people learn functional programming along with the patterns. Thank you Misha for writing this!
patrickmayover 9 years ago
Peter Norvig covered this two decades ago: <a href="http:&#x2F;&#x2F;norvig.com&#x2F;design-patterns&#x2F;design-patterns.pdf" rel="nofollow">http:&#x2F;&#x2F;norvig.com&#x2F;design-patterns&#x2F;design-patterns.pdf</a><p>Paul Graham mentioned Norvig&#x27;s work in 2002: <a href="http:&#x2F;&#x2F;www.paulgraham.com&#x2F;icad.html" rel="nofollow">http:&#x2F;&#x2F;www.paulgraham.com&#x2F;icad.html</a>
rfwover 9 years ago
I love the idea! A few examples are a bit strawmanish but really when you&#x27;re comparing languages like that who can resist the strawman.<p>The Clojure solutions do sacrifice a lot of safety though, due to dynamic typing (e.g. the visitor pattern, which relies on basically strings to do its dispatching).
评论 #10764753 未加载
0x0deaover 9 years ago
<a href="http:&#x2F;&#x2F;wordsmith.org&#x2F;anagram&#x2F;anagram.cgi?anagram=SerpentHillREE&amp;t=1" rel="nofollow">http:&#x2F;&#x2F;wordsmith.org&#x2F;anagram&#x2F;anagram.cgi?anagram=SerpentHill...</a><p>Subtle.
moominover 9 years ago
I&#x27;ll just plug my own (not as comprehensive) contribution to this meme: <a href="http:&#x2F;&#x2F;www.colourcoding.net&#x2F;blog&#x2F;archive&#x2F;2014&#x2F;10&#x2F;27&#x2F;design-patterns-happy-birthday-and-goodbye.aspx" rel="nofollow">http:&#x2F;&#x2F;www.colourcoding.net&#x2F;blog&#x2F;archive&#x2F;2014&#x2F;10&#x2F;27&#x2F;design-p...</a>
yendaover 9 years ago
I would love to have some Java programmer opinion on this piece.
评论 #10764795 未加载
richcole2over 9 years ago
Really nice writeup of patterns. What turned me off Clojure is that the dynamic typing makes it very hard to refactor. e.g. even renaming a function is really quite difficult, let alone adding function parameters. Forget extracting a function. The dynamic typing also means a typo can yield a nil that travels far from the mistake site making debugging difficult. There are hints towards better debugging in clojure, but last time I checked it was still very much a work in progress. Also lazy lists are easy to screw up, e.g. if you put a concat inside a reduce then stack go boom.<p>Still this is a great post because it teaches you both good Clojure and the design patterns. Since java has closures now you can take a lot of what you learn from Clojure and put it in your well typed and refactorable Java programs.
评论 #10764809 未加载
评论 #10764833 未加载
评论 #10766030 未加载
moominover 9 years ago
Whilst I agree in general with the sentiment, it really doesn&#x27;t help if you start with a misunderstanding of the command pattern. The essential property of command is that it&#x27;s data that can be executed. Implicitly, it&#x27;s possible to do other things with them: log them, undo them and so on. A better model would be a record that implements IFn.
OmarIsmailover 9 years ago
I think the Visitor example is a mistake. Couldn&#x27;t you replicate the Clojure version in Java by having a &quot;Formatter&quot; singleton that has a function for each of the different item type&#x2F;format type combinations?
评论 #10765747 未加载
js8over 9 years ago
Excellent, definitely going to read this. I would like to see this for Haskell, though. And some Lisp idioms (from On Lisp, for example) could be thrown in for a good measure, too.
greydiusover 9 years ago
&gt; Eve: Stop talking, show me the code!<p>I like Eve.