Clojure 中的可插入向量处理单元

发布于 2024-09-28 00:36:23 字数 628 浏览 9 评论 0原文

我正在 Clojure 中开发一些模拟软件,需要处理大量矢量数据(基本上源自 Java 浮点数组的偏移量,长度通常在 10-10000 范围内)。大量的这些向量将需要经历各种处理步骤 - 例如,对向量进行归一化、将两个向量流连接在一起、计算移动平均值等。

我希望做的不是以命令式方式完成所有操作,而是创建一个更多的处理步骤函数式 Clojure 解决方案将执行以下操作:

  • 允许将任何向量函数转换为可插入模块,例如 (def module-a (make-module some-function))
  • 允许这些模块在管道中组合,例如(def组合模块(组合串联模块-a模块-b))会将模块a的输出馈送到模块b的输入
  • 允许辅助函数来访问存储在给定模块中的状态,例如(get-moving-average some-moving-average-module),即使 some-moving-average-module 深深嵌入到一个模块中,它也需要工作组合管道
  • 在幕后隐藏任何样板代码,例如为向量计算分配足够大的临时数组。

这听起来是一个明智的方法吗?

如果是这样,有任何可能有帮助的实现提示或库吗?

I'm developing some simulation software in Clojure that will need to process lots of vector data (basically originating as offsets into arrays of Java floats, length typically in 10-10000 range). Large numbers of these vectors will need to go through various processing steps - e.g. normalising the vectors, concatenating together two streams of vectors, calculating a moving average etc.

Rather than doing everything in an imperative style, I was hoping to do was create a more functional-style Clojure solution that would do the following:

  • allow any vector function to be turned into a pluggable module, e.g. (def module-a (make-module some-function))
  • allow these modules to be composed in pipelines, e.g. (def combined-module (combine-in-series module-a module-b)) would feed the output of module-a into the input of module-b
  • allow auxillary functions to access state stored within a given module, e.g. (get-moving-average some-moving-average-module), which would need to work even if some-moving-average-module is embedded deep within a combined pipeline
  • hide any boilerplate code behind the scenes, e.g. allocating sufficiently large temporary arrays for vector calculation.

Does this sound like a sensible approach?

If so, any implementation hints or libraries that might help?

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

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

发布评论

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

评论(2

夏了南城 2024-10-05 00:36:23

在函数式语言中,一切都是数据流。您可以使用函数作为模块概念。

为了解决您的每个用例:

  • pluggagble 模块是一个 Clojure 函数,它采用一个参数,即数据向量的状态。例如 (def module-a some-function) 为了方便模块扩展,我建议使用 Clojure 映射作为您的状态,其中一个字段是浮点数组。
  • 组合模块就是功能组合。例如 (def linked-module (compose module-a module-b)
  • 辅助函数是访问器函数,从数据中提取状态。例如,如果您的数据是带有以下内容的 Clojure 映射 是您的访问器函数,状态不会存储在模块中。
  • :moving-average 字段,则关键字 :moving-average 隐藏在函数的实现中,可以在任何地方声明,可能在另一个文件和命名空间中。

In a functional language, everything is dataflow. You can use functions as your module concept.

To address each of your use-cases:

  • A pluggagble module is a Clojure function that takes a single argument that is the state of your data vector. e.g. (def module-a some-function) To allow for easy extension by modules, I suggest using a Clojure map as your state, where one field is your array of floats.
  • Composing modules is function composition. e.g. (def combined-module (compose module-a module-b)
  • Auxiliary functions are accessor functions, extracting state from your data. e.g. If your data is a Clojure map with a :moving-average field, then the keyword :moving-average is your accessor function. State is not stored in modules.
  • Boilerplate code is hidden in the implementation of your functions, which can be declared anywhere, possibly in another file and namespace.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文