rplevy / swiss-arrows

a collection of arrow macros
565 stars 23 forks source link

composition arrows #24

Open neapel opened 10 years ago

neapel commented 10 years ago

I'm quite new to clojure and not sure if there isn't a better way already, but I found myself writing things like

(fn [x] (->> x (f a) (g b)))

quite often. Which I instinctively want to write in a point-free style, but this means I'll loose the comfort and readability of arrows:

(comp (partial g b) (partial f a))

I'm using these trivial helper macros now:

(defmacro fn->
  "(fn-> f g...) expands to (fn [x] (-> x f g...)"
  [& args]
  `(fn [x#] (-> x# ~@args)))

(defmacro fn->>
  "(fn->> f g...) expands to (fn [x] (->> x f g...))"
  [& args]
  `(fn [x#] (->> x# ~@args)))

which allow writing

(fn->> (f a) (g b))

Would that be a candidate for inclusion in swiss-arrows?

rplevy commented 10 years ago

Did you try the clojure.core/cond-> arrow?

neapel commented 10 years ago

I don't understand how this would work using cond->? It accepts a value as the first argument like all the other arrows, my idea was about introducing a higher-order arrow that returns a function, which when given a value returns the same result as the other arrows do when given that value as the first argument.

Edit (confused cond-> and condp. There wouldn't be a useful fncond-> version because the clauses would have no way to access the value)