Advertisement
Guest User

Kleisli category in Clojure

a guest
Jul 4th, 2019
468
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (ns playground.core
  2.   (:require [try-let :refer [try-let]])
  3.   (:gen-class))
  4.  
  5. (defn opt [f] (fn [x] (try-let [res (f x)]
  6.                                res
  7.                               (catch Exception ex nil))))
  8.  
  9. (defn safe_inv [x] ((opt (partial / 1)) x))
  10. (defn safe_sqrt [x] ((opt #(Math/sqrt %)) x))
  11.  
  12. (defn compose [f g]
  13.   (fn [x]
  14.     (if-let [res (f x)]
  15.       (g res)
  16.       "Fail")))
  17.  
  18. (defn -main
  19.   []
  20.   (println (map #((compose safe_inv safe_sqrt) %) [4 3 2 1 0 -1 -2]))
  21. )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement