简体   繁体   中英

Can I use monads with clojurescript?

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

Monads work in clojurescript. The only thing is the way you reference macros in 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.

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)

[1] See 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:

(ns my.namespace (:require-macros [my.macros :as my])) The :as prefix selector is required in :require-macros. One point of note is that the code generated by ClojureScript macros must target the capabilities in ClojureScript.

Monads, the Functional programming method, only require a language with higher order functions and closures. You would need a ClojureScript monad library. Or you can

EDIT: by "monadic style" I was referring to writing and composing functions that take and return monadic values and implement the three monad laws . 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 .

There is a great port of the Fluokitten Library to ClojureScript by Chris Zheng: https://github.com/purnam/brahmin

Chris has a great post reflecting on Monads as a result of this.

The cats library for Category Theory is also good https://funcool.github.io/cats/latest/

Monads in ClojureScript is alive and well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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