函数式编程如何应用于模拟? [英] How does functional programming apply to simulations?

查看:151
本文介绍了函数式编程如何应用于模拟?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

除了标题中的一般问题,

Besides the general question in the title,


  • 功能程序员和函数式语言如何处理模拟领域,最自然地由面向对象语言处理?

  • 是否有以(主要)功能风格编写的复杂模拟的开源示例?

  • OO程序员需要什么样的透视变化,以便从功能范例中接受模拟?

我在问这个问题时,Clojure的创建者Rich Hickey专门设法驯服OO编程和可变状态的偶然复杂性,例如Clojure的身份和状态的分离很有意义(Hickey的ants.clj在研究列表中)。另一个相关领域是使用游戏的功能编程,这通常是模拟与许多有状态的东西遍布的地方;有一些关于FP和游戏的文章/论文,更受欢迎。

I'm asking this while learning how Clojure's creator Rich Hickey specifically sought to tame the "incidental complexity" of OO-programming and mutable state, e.g. Clojure's separation of identity and state makes a lot of sense (Hickey's ants.clj is on the study list). Another related area is using functional programming for games, which are often simulations with lots of stateful "things" all over the place; there are some articles/papers written about FP and games, more would be welcome.

也许经验丰富的功能程序员可以提供额外的背景和建议如何重新定位自己的思维到功能风格,专门用于模拟。提前感谢!

Perhaps experienced functional programmers can provide additional background and advice on how to re-orient one's thinking to functional style, specifically for simulations. Thanks in advance!

推荐答案

Michal的回答非常好,但我想我会添加一些其他整齐的例子,发现有用/有趣。

Michal's answer is excellent, but I thought I'd add a couple other neat examples I've personally found helpful/interesting.

第一个是关于 Lau Jenson的功能流体动力学。虽然他肯定在这里的可变路线的速度,风格是相当功能。我打赌Clojure 1.3这可以做(大部分!)不可改变与合理的性能。

The first is a post (and code) about functional fluid dynamics by Lau Jenson. Though he definitely goes the mutable route for speed here, the style is rather functional. I'd bet by Clojure 1.3 this could be done (mostly!) immutably with reasonable performance.

接下来是简单Snake游戏在Clojure中实现。足够容易阅读,虽然在一个小时左右,和风格是真的愉快和凝聚力。

The next is a simple Snake game implemented in Clojure. Easy enough to read though in an hour or so, and the style is really pleasant and cohesive.

此外,一些整洁的代码看起来(我想!)是代码建模神经网络。 Jeff Foster有一些单层感知器代码,以及一些代码的惯用修订。值得看,即使你不熟悉NNs。他还有一些关于流体动力学的最新帖子,虽然这次在Haskell。 (第一部分第二部分)我觉得很有趣他的实施游戏生活(& 第二部分 )。

Also, some neat code to look at (I think!) is code modeling neural networks. Jeff Foster has some single layer perceptron code, and some more idiomatic revisions of the code. Worth looking at, even if you're not acquainted with NNs. He also has some more recent posts regarding fluid dynamics, though this time in Haskell. (Part I and Part II) Also fun, I think, is his implementation of the Game of Life (& Part II).

最后,正如Michal在我之前提到的,Brian Carper正在从事 Clojure中的一个RPG 。他为该游戏最近发布了一些作品,因此我仍在投注它的工作; )

Finally, as Michal mentioned before me, Brian Carper is working on a RPG in Clojure. he recently posted some artwork for the game, so I'm betting it's still being worked on ;)

我喜欢使用序列库处理大量数据;使用如 map reduce 的抽象以及 juxt等有趣的方便工具感觉更自然而不是简单的命令式迭代。你已经发现,通过使用Clojure /功能语言来重新实现众所周知的和良好实现的命令式算法,你需要交税。

I love using the sequence libraries for working with tons of data; it feels more natural using abstractions like map and reduce, and fun, handy tools like juxt rather than simple imperative iterations. You do pay a tax, I've found, by using Clojure/functional langs in reimplementing well-known and well-implemented imperative algorithms.

有乐趣!

这篇关于函数式编程如何应用于模拟?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆