如何从 Java 过渡到 Clojure?
After discovering Clojure I have spent the last few days immersed in it.
What project types lend themselves to Java over Clojure, vice versa, and in combination?
What are examples of programs which you would have never attempted before Clojure?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Clojure 非常适合并发编程。 它提供了处理线程的出色工具,如软件事务内存和可变引用。
作为 Western Mass Developer's Group 的演示,Rich Hickey 制作了一个蚁群模拟,其中每只蚂蚁都是它自己的线程,并且所有变量都是不可变的。 即使线程数量非常多,效果也很好。 这不仅因为 Rich 是一位了不起的程序员,还因为他在编写代码时不必担心锁定。 您可以在此处查看他关于蚁群的演示。
Clojure lends itself well to concurrent programming. It provides such wonderful tools for dealing with threading as Software Transactional Memory and mutable references.
As a demo for the Western Mass Developer's Group, Rich Hickey made an ant colony simulation in which each ant was its own thread and all of the variables were immutable. Even with a very large number of threads things worked great. This is not only because Rich is an amazing programmer, it's also because he didn't have to worry about locking while writing his code. You can check out his presentation on the ant colony here.
如果您打算尝试并发编程,那么我认为 clojure 比您从 Java 中获得的开箱即用的要好得多。 看一下此演示文稿即可了解原因:
http://blip.tv/file/812787
我在我的博客上记录了我使用 Clojure 的前 20 天
http:// Loufranco.com/blog/files/category-20-days-of-clojure.html
我从 SICP 讲座开始,然后构建了一个并行素数筛。 我也玩过宏。
If you are going to try concurrent programming, then I think clojure is much better than what you get from Java out of the box. Take a look at this presentation to see why:
http://blip.tv/file/812787
I documented my first 20 days with Clojure on my blog
http://loufranco.com/blog/files/category-20-days-of-clojure.html
I started with the SICP lectures and then built a parallel prime number sieve. I also played around with macros.
如果你想开发一个供 Java 和 Clojure 使用的框架,我发现用 Java 编写主要抽象(接口和基类)比用 Clojure 编写要好(我发现 Clojure 的 gen- class 有点乏味,而是使用
proxy
)。如果您是 Hibernate 或任何其他大量使用 Java 注释而不提供编程替代方案的框架的用户,您将会遇到一些麻烦,因为使用 Clojure 的数据结构模拟注释 POJO 并不是一件容易的事。
除此之外,我还没有遇到过 Clojure 比 Java 更不合适的用例。 当然,您必须应对静态类型的丢失,一开始这感觉有点令人不安,但往往会消失。
If you want to develop a framework that is to be consumed by Java and Clojure, I've found writing the main abstractions (interfaces ad base classes) in Java to be preferable over writing them in Clojure (I find Clojure's
gen-class
to be somewhat tedious and rather useproxy
).If you're a user of Hibernate or any other framework that makes heavy use of Java-annotations without offering a programmatic alternative, you'll have some trouble, since it's not trivial to emulate annotated POJOs with Clojure's data structures.
Apart from that, I've experienced no use cases for which Clojure is less appropriate than Java; you have to cope with the loss of static typing, of course, which feels somewhat disconcerting at first, but tends to go away.