简体   繁体   English

我可以使用带有clojurescript的monad吗?

[英]Can I use monads with clojurescript?

我知道monad可用于clojure,但有没有人证实他们在clojurescript中工作?

Monads work in clojurescript. Monads在clojurescript中工作。 The only thing is the way you reference macros in clojurescript [1]. 唯一的就是你在clojurescript [1]中引用宏的方式。

What I did is copy all tools/macro.clj and all the monads.clj code into a big file which I then reference from clojurescript. 我所做的是将所有tools / macro.clj和所有monads.clj代码复制到一个大文件中,然后我从clojurescript中引用它。

It's perhaps not the most elegant way but it works. 它可能不是最优雅的方式,但它的工作原理。

You can find the file in https://github.com/cotarmanach/clojurescript-monad-macros (I copy it in my project and change the namespace to be the one of my project) 您可以在https://github.com/cotarmanach/clojurescript-monad-macros中找到该文件(我将其复制到我的项目中并将命名空间更改为我的项目之一)

[1] See https://github.com/clojure/clojurescript/wiki/Differences-from-Clojure [1]参见https://github.com/clojure/clojurescript/wiki/Differences-from-Clojure

ClojureScript's macros are written in Clojure, and are referenced via the require-macros keyword in namespace declarations: ClojureScript的宏是用Clojure编写的,并通过命名空间声明中的require-macros关键字引用:

(ns my.namespace (:require-macros [my.macros :as my])) The :as prefix selector is required in :require-macros. (ns my.namespace(:require-macros [my.macros:as my])):require-macros中需要:as前缀选择器。 One point of note is that the code generated by ClojureScript macros must target the capabilities in ClojureScript. 需要注意的是,ClojureScript宏生成的代码必须以ClojureScript中的功能为目标。

Monads, the Functional programming method, only require a language with higher order functions and closures. Monads是函数式编程方法,只需要具有更高阶函数和闭包的语言。 You would need a ClojureScript monad library. 你需要一个ClojureScript monad库。 Or you can go ahead and write your ClojureScript in a monadic style 或者你可以继续以monadic风格编写你的ClojureScript

EDIT: by "monadic style" I was referring to writing and composing functions that take and return monadic values and implement the three monad laws . 编辑:通过“monadic风格”,我指的是写作和编写功能,采取和返回monadic值并实现三个monad法则 There are too many people better qualified to explain monads than I, so I will just link to one of my personal favorite videos on monads . 有太多的人比我更有资格解释monad,所以我只会链接到我个人最喜欢的monad视频

There is a great port of the Fluokitten Library to ClojureScript by Chris Zheng: https://github.com/purnam/brahmin Chris Zheng为ClojureScript提供了一个很棒的Fluokitten Library端口: https//github.com/purnam/brahmin

Chris has a great post reflecting on Monads as a result of this. 由于这个原因,Chris有一篇很好的文章反映了莫纳德

The cats library for Category Theory is also good https://funcool.github.io/cats/latest/ 分类理论的猫图书馆也很好https://funcool.github.io/cats/latest/

Monads in ClojureScript is alive and well. ClojureScript中的Monads活得很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM