也许在clojure [英] Maybe in clojure

查看:69
本文介绍了也许在clojure的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试在clojure中编写一个以第一个nil值退出的组合函数(例如,您可以通过在Haskell中将Maybes链接在一起)来实现以下目的:

Trying to write a composed function in clojure that exits at the first nil value, (e.g something you'd do by chaining Maybes together in haskell) with the following:

(defn wrap [f] (fn [x] (if (nil? x) nil (f x))))

(defn maybe [arg & functs] ( (comp (reverse (map wrap functs))) arg))

'd get,例如

(defn f1 [x] (+ x 1))

(maybe 1 f1 f1 ) => 3

(maybe nil f1 f1) => nil

不幸的是,这给了我:
ClassCastException clojure.lang.PersistentList无法强制转换到clojure.lang.IFn用户/也许(NO_SOURCE_FILE:1)

Which is unfortunately giving me this: ClassCastException clojure.lang.PersistentList cannot be cast to clojure.lang.IFn user/maybe (NO_SOURCE_FILE:1)

有人可以对我在这里做错的事情提供帮助吗?

Can someone provide some help on what I'm doing wrong here? What's the idiomatic way to do this?

推荐答案

惯用的方法是使用 some- > 。请参阅此文档的文档宏以获得更多详细信息。

The idiomatic way to do this is to use some->. See the documentation of this macro for more details.

当然不要让那阻止您制作自己的照片!

Don't let that stop you from making your own, of course!

这篇关于也许在clojure的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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