如何更好地迭代在Clojure中的状态(monad?) [英] How to Better Iterate over State in Clojure (monad?)

查看:168
本文介绍了如何更好地迭代在Clojure中的状态(monad?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚写了这段代码:

 (defn parameters [transform-factory state] 
(lazy-seq (let [[r1 state](uniform state)
[r2 state](uniform state)
[t state](transform-factory state)
(cons [t [r1 r2] ](参数transform-factory state)))))

(defn repeated-transform [mosaic n transform-factory state]
(parameters transform-factory state))))

参数函数生成从 state 生成的延迟序列的值,用于参数化某物(在这种情况下为马赛克)的重复变换。 / p>

在我看来,参数显示了一个相当常见的模式,当你有一些状态(在这种情况下生成随机值)。有这个名字吗?



有没有更好的方式来写第一个函数?相关的问题通常可以用 reduce 来解决,它携带状态,但在这里我没有减少。类似地, reduction 似乎不合适。这是monad的好例子吗? (从理论pov我没有看到你如何定义一种方式将多个实例合并为一个,但也许不改变实际应用 - 它似乎像monads解决的其他地方,其中一些状态需要的类型



(ps我提到的随机数,但我不能替换这个解决方案使用后面的可变状态 - 作为正常随机

解决方案

你可以看看状态monad,看看它是否是一个非常适合你。



使用monad的一般原则是:




  • 顺序(管道操作)

  • 可重复使用的模块化副作用处理(例如:错误处理/记录/
    状态)

  • 保留业务逻辑清除纯函数



我发现Monads上的一些资源非常有用(对于Clojure)是



Adam Smyczek:
Monads介绍(视频)
http://www.youtube.com/watch?v=ObR3qi4Guys




Jim Duey:
Monads in Clojure
http://www.clojure.net/2012/02 / 02 / Monads-in-Clojure /


I just wrote this code:

(defn parameters [transform-factory state]
  (lazy-seq (let [[r1 state] (uniform state)
                  [r2 state] (uniform state)
                  [t state] (transform-factory state)]
              (cons [t [r1 r2]] (parameters transform-factory state)))))

(defn repeated-transform [mosaic n transform-factory state]
  (reduce transform-square mosaic
    (take n (parameters transform-factory state))))

the parameters function generates a lazy sequence of values generated from the state, which are used to parameterise a repeated transformation of something (a "mosaic" in this case).

it seems to me that parameters shows a fairly common pattern which surfaces when you have some state that must be carried around (in this case to generate random values). is there a name for this?

is there a better way to write the first function? related problems can often be solved with reduce, which "carries along" the state, but here i have nothing to reduce. similarly, reductions doesn't seem to fit. is this a good case for a monad? (from a theoretical pov i don't see how you define a way to combine multiple instances into one, but perhaps that doesn't change the practical application - it does seem like the kind of problem monads solve elsewhere, where some state needs to be carried around).

(ps i mentioned random numbers, but i can't replace this with a solution that uses mutable state behind the scenes - as "normal" random routines do - for reasons unrelated to the question).

解决方案

You could certainly look at the state monad to see if it is a good fit for you.

General guidelines to use monads are:

  • Sequential execution (pipeline operations)
  • Reusable modular side effect processing (ex: error handling/ logging/ state)
  • Keep your business logic clean in pure functions

Some resources on monads that I found very useful (for Clojure) are

Adam Smyczek: Introduction to Monads (Video) http://www.youtube.com/watch?v=ObR3qi4Guys

and Jim Duey: Monads in Clojure http://www.clojure.net/2012/02/02/Monads-in-Clojure/

这篇关于如何更好地迭代在Clojure中的状态(monad?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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