Clojure 和 Java 之间的界限在哪里?

发布于 2024-10-12 21:36:22 字数 451 浏览 8 评论 0原文

我有一个有趣的架构问题,涉及我正在使用 Clojure 和 Java 开发的应用程序。该应用程序涉及大量需要编排的密集、并发数据处理任务。

以下是混合 Clojure 和 Java 的基本原理:

  • 一些相当CPU 密集型数字代码需要 Java,我需要优化算法以在 JVM 上尽可能快地运行。 Clojure 还不能完全实现这一点,而且这样的代码在 Clojure 中也不是很惯用,因为出于性能原因,算法需要大量可变数据。
  • Clojure(恕我直言)在编排应用程序的整体流程方面要好得多,因为它对函数式编程、REPL 上的交互式动态开发和并发功能提供了出色的支持。

鉴于我正在使用两种语言 - 我应该应用什么逻辑或原则来确定两者之间的分界线?我特别感兴趣的是如何设计一个 API/接口,使其处于适当的级别以利用两种语言的相对优势。

I have an interesting architectural question regarding an application that I am developing using both Clojure and Java. The application involves a lot of intensive, concurrent data processing tasks that need to be orchestrated.

Here's the rationale for mixing both Clojure and Java:

  • Java is needed for some pretty CPU-intensive numerical code, where I need to optimise the algorithms to run as fast as possible on the JVM. Clojure can't quite achieve this yet, and such code would not be very idiomatic in Clojure because the algorithms require a lot of mutable data for performance reasons.
  • Clojure is (IMHO) far better for orchestrating the overall flow of the application, with its excellent support for functional programming, interactive dynamic development at the REPL and concurrency features.

Given that I'm using both languages - what logic or principles should I apply to determine the dividing line between the two? In particular, I'm interested in how to design an API/interface that would be at the right kind of level to take advantage of the relative strengths of both languages.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱本泡沫多脆弱 2024-10-19 21:36:22

如果不评论您对 Java 和 Clojure 相对优势的看法,并假设您至少进行了一些微基准测试来验证该假设有可能是正确的,那么正确的方法似乎是仅将 Java 留给需要优化的部分。

负责数字代码和计算的类应该用 Java 编写,其他所有内容都应该用 Clojure 编写。我什至会采取更积极的方法,将类设计为不同的,以便它们可以用 Java 编写,但实际上用 Clojure 编写它们,如果性能被证明是一个问题,则用 Java 重写它们。

Without commenting on your perception of the relative advantages of Java and Clojure, and assuming that you did at least some micro-benchmarking to validate that the assumption has some chance of being correct, then the correct approach would seem to be to leave Java only for the parts that require optimization.

The classes responsible for the numeric code and calculation should be written in Java, and everything else in Clojure. I would even take a more aggressive approach and just design the classes to be distinct so that they could be written in Java, but actually write them in Clojure and rewrite them in Java if performance proves to be a problem.

世俗缘 2024-10-19 21:36:22

Clojure 很好地帮助开发人员以简单的函数式风格完成大部分工作,并将突变工作隔离到有限的区域。

我将在这里应用相同的指导原则:尽可能多地隔离 java 代码,就像您以“纯函数式风格”尽可能多地隔离 clojure 代码一样。因此,考虑到您的限制,java 岛将尽可能小,并且对 java 岛的访问将通过一小组 clojure 函数。

不确定这有多大帮助,但无论如何!

Clojure does a good job of helping developers get most of their part in simple functional style, and isolating mutational work into confined areas.

I would apply the same guidelines here: isolate java code as much as you can, as you would do as much of your clojure code in "pure functional style". So the java island would be as small as possible given your constraints, and access to the java island would go through a small set of clojure functions.

Not sure this helps much, but anyway !

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文