开发者

clojure, using a list of functions

I would like to define a list of functions to use within juxt, however I am having trouble implementing it.

Heres an example of what I want:

(defn sin [n] (Math/sin n))
(defn cos [n] (Math/cos n))
((juxt sin cos) 4)
>> [-0.75680249530792开发者_如何学运维82 -0.6536436208636119]

Now instead of saying ((juxt sin cos) 4) I would prefer to say ((juxt trig) 4) where (def trig [sin cos]). I have attempted ((apply juxt trig) 4) and a few other things but nothing seems to stick. Thanks!


apply seems to work fine:

user=> ((juxt sin cos) 4)
[-0.7568024953079282 -0.6536436208636119]
user=> ((apply juxt trig) 4)
[-0.7568024953079282 -0.6536436208636119]


I think you have a correct solution. For me it works:

Clojure 1.2.1
user=> (defn sin [n] (Math/sin n))
#'user/sin
user=> (defn cos [n] (Math/cos n))
#'user/cos
user=> (def trig [sin cos])
#'user/trig
user=> ((apply juxt trig) 4)
[-0.7568024953079282 -0.6536436208636119]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜