Map和Reduce Monad for Clojure ... Juxt Monad怎么样? [英] Map and Reduce Monad for Clojure... What about a Juxt Monad?

查看:142
本文介绍了Map和Reduce Monad for Clojure ... Juxt Monad怎么样?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在学习Clojure的同时,我花了很多年的时间试图理解monads - 他们是什么,以及我们如何使用它们...没有太多的成功。不过,我发现了一个非常好的Monads for Dummies视频系列 - http://vimeo.com/20717301 - 由Brian Marik for Clojure

Whilst learning Clojure, I've spent ages trying to make sense of monads - what they are and how we can use them.... with not too much success. However, I found an excellent 'Monads for Dummies' Video Series - http://vimeo.com/20717301 - by Brian Marik for Clojure

到目前为止,我对monads的理解是它类似于一个宏,它允许一组语句以一种形式很容易阅读 - 但monad更正式。我的观察仅限于两个例子:

So far, my understanding of monads is that it is sort of like a macro in that it allows a set of statements to be written in a form that is easy to read - but monads are much more formalised. My observations are limited to two examples:

1。身分Monad(或letmonad)取自 http://onclojure.com/2009/03/05/a-monad-tutorial-for-clojure-programmers-part-1/

我们希望写的表单是:

(let [a  1
      b  (inc a)]
  (* a b))

p>

and the corresponding monad is

(domonad identity-m
    [a  1
     b  (inc a)]
 (* a b))

2。序列Monad(或formonad)取自 http://onclojure.com/2009/03/06/a-monad-tutorial-for-clojure-programmers-part-2/

我们希望写的形式是:

(for [a (range 5)
      b (range a)]
  (* a b))

(domonad sequence-m
  [a (range 5)
   b (range a)]
  (* a b))

Monad Clojure中的定义

查看源代码,使用clojure monads库 - https:/ /github.com/clojure/algo.monads

Looking at the source, using clojure monads library - https://github.com/clojure/algo.monads:

user=>(use 'clojure.algo.monads)
nil

indentity monad:

indentity monad:

user=> (source identity-m)
(defmonad identity-m
  [m-result identity
   m-bind   (fn m-result-id [mv f]
              (f mv))
  ])

序列monad:

user=> (source sequence-m)
(defmonad sequence-m
   [m-result (fn m-result-sequence [v]
               (list v))
    m-bind   (fn m-bind-sequence [mv f]
               (flatten* (map f mv)))
    m-zero   (list)
    m-plus   (fn m-plus-sequence [& mvs]
               (flatten* mvs))
    ])

结论是,monad是一种广义的高阶函数,它接受输入函数和输入值,添加自己的控制逻辑,并输出可以在domonad块中使用的东西。

So my conclusion is that a monad is some sort of a generalised higher-order function that takes in an input-function and input-values, adds its own control logic and spits out a 'thing' that can be used in a 'domonad' block.

问题1

最后,对于问题:如何写一个monad并说我想写一个map monad模仿clojure中的map形式:

So finally, to the questions: I want to learn how to write a monad and say I want to write a 'map monad' that imitates the 'map' form in clojure:

(domonad map-m
  [a [1 2 3 4 5]
   b [5 6 7 8 9]]
  (+ a b))

应等同于

(map + [1 2 3 4 5] [5 6 7 8 9])

并返回值

[6 8 10 12 14]

如果我看看源代码,它应该给我类似于identity-m和sequence-m的东西:

If I look at the source, it should give me something similar to identity-m and sequence-m:

user=> (source map-m)
(defmonad map-m
   [m-result ...
    m-bind   ...
    m-zero   ...
    m-plus   ...
    ])

我也想要定义'reduce-m',这样我可以写:

I also want to be able to define 'reduce-m' such that I can write:

(domonad reduce-m
  [a [1 2 3 4 5]]
  (* a))

这可能会给我1 x 2 x 3 x 4 x 5 = 120或

this could potentially give me 1 x 2 x 3 x 4 x 5 = 120 or

(domonad reduce-m
  [a [1 2 3 4 5]
   b [1 2 3 4 5]]
  (+ a b))

会给我(1 + 2 + 3 + 4 + 5)+(1 + 2 + 3 + 4 + 5) = 30

will give me (1+2+3+4+5) + (1+2+3+4+5) = 30

最后
我也可以写一个'juxt monad'模仿juxt函数,在绑定的值,我传入一组函数。 :

Finally Would I also be able to write a 'juxt monad' that imitates the juxt function but instead of passing in values for binding, I pass in a set of functions. :

(domonad juxt-m
  [a #(+ % 1)
   b #(* % 2)]
  '([1 2 3 4 5] b a) )

/ p>

gives

[ [2 2] [4 3] [6 4] [8 5] [9 6] ] 

潜在地,我可以做所有这些事情与宏,所以我不知道这些'monad'或者如果他们甚至被认为是monad...用互联网上的所有资源,在我看来,如果我想正确地学习Monads,我必须学习Haskell,现在,学习另一种语法形式是太难了。

Potentially, I could do all of those things with macros so I don't really know how useful these 'monads' will be or if they are even considered 'monads'... With all the resources on the internet, It seems to me that if I wanted to learn Monads properly, I have to learn Haskell and right now, learning another syntactic form is just too hard. I think I found some links that maybe relevant but it is too cryptic for me

推荐答案

您的示例不是monad。 monad代表可组合的计算步骤。在简单的同一性单子中,计算步骤仅仅是表达式求值。在可能的monad中,一个步骤是可能成功或失败的表达式。在序列monad中,步骤是产生可变数量的结果(序列的元素)的表达式。在作者monad中,计算步骤是表达式求值和日志输出的组合。在状态monad中,计算步骤涉及访问和/或修改一段可变状态。

Your examples are not monads. A monad represents composable computational steps. In the trivial identity monad, the computational step is just an expression evaluation. In the maybe monad, a step is an expression that may succeed or fail. In the sequence monad, a step is an expression that produces a variable number of results (the elements of the sequence). In the writer monad, a computational step is a combination of expression evaluation and log output. In the state monad, a computational step involves accessing and/or modifying a piece of mutable state.

在所有这些情况下,monad管理员负责正确地组合步骤。 m-result函数打包一个普通值以适合单进制计算方案,m-bind函数将一个计算步骤的结果馈入下一个计算步骤。

In all these cases, the monad plumbery takes care of correctly combining steps. The m-result function packages a "plain" value to fit into the monadic computation scheme, and the m-bind function feeds the result of one computational step into the next computational step.

在(map + ab)中,没有要组合的计算步骤。没有秩序的概念。它只是嵌套表达式求值。减少相同。

In (map + a b), there are no computational steps to be combined. There is no notion of order. It's just nested expression evaluation. Same for reduce.

这篇关于Map和Reduce Monad for Clojure ... Juxt Monad怎么样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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