Reputation: 1882
I'm just starting out learning Clojure (my first Lisp-like language) so I'm sorry if this question is very silly.
I'd like to apply multiple methods on a list. So far I've come up with
(defn sqr [x] (* x x))
(def my-list '(4 7 9))
(map inc (map sqr (map dec my-list)))
;= (10 37 65)
Is there a better (more concise/idiomatic) way to do this? Ideally I'd like to do something like
(apply-multiple (dec sqr inc) my-list)
...which would also return (10 37 65)
.
Upvotes: 4
Views: 105
Reputation:
Just use function composition:
(map (comp inc sqr dec) my-list)
Or if you prefer to write them down in the reverse order (like in your example):
(map #(-> % dec sqr inc) my-list)
Upvotes: 8