Maybe in clojure -
trying write composed function in clojure exits @ first nil value, (e.g you'd chaining maybes in haskell) following:
(defn wrap [f] (fn [x] (if (nil? x) nil (f x)))) (defn maybe [arg & functs] ( (comp (reverse (map wrap functs))) arg)) so i'd get, e.g.
(defn f1 [x] (+ x 1)) (maybe 1 f1 f1 ) => 3 (maybe nil f1 f1) => nil which unfortunately giving me this: classcastexception clojure.lang.persistentlist cannot cast clojure.lang.ifn user/maybe (no_source_file:1)
can provide on i'm doing wrong here? what's idiomatic way this?
the idiomatic way use some->. see the documentation of macro more details.
don't let stop making own, of course!
Comments
Post a Comment