clojure/core juxt

点我查看原文及更多示例

(ns $clojureDoc.core.juxt)
;; 使用方法:(juxt f) (juxt f g) (juxt f g h) (juxt f g h & fs)
;; Takes a set of functions and returns a fn that is the juxtaposition
;; of those fns.  The returned fn takes a variable number of args, and
;; returns a vector containing the result of applying each fn to the
;; args (left-to-right).
;; ((juxt a b c) x) => [(a x) (b x) (c x)]
;; 接受多个函数作为参数,这些参数是并列的(地位相同,只分计算的先后顺序)
;; 每个函数接受的参数个数应该都与提供的参数个数相同
;; 返回的是一个由参数参与各个函数运算后的结果组成的vector

;;所有提供的参数参与juxt的每一个函数的运算,将结果组成一个vector

;; 把参数1放进去运算:inc 1和dec 1两个结果组合成一个vector
(println ((juxt inc dec) 1))
;; => [2 0]

;;
(println ((juxt + * min max) 3 4 6))
;; => [13 72 3 6]
;; (+ 3 4 6)
;; (* 3 4 6)
;; (min 3 4 6)
;; (max 3 4 6)

;; take和drop都接受两个参数:n coll
;; take获取coll的前n项,其余丢掉
;; drop让coll丢掉前n项,获取剩下的
(println ((juxt take drop) 2 [0 1 2 3 4 5 6]))
;; => [(0 1) (2 3 4 5 6)]

;; Extract values from a map, treating keywords as functions.
;; 从map中提取值,键在此视为函数的作用
(println ((juxt :a :b) {:a 1 :b 2 :c 3 :d 4}))
;;=> [1 2]

;; 根据运算结果排序原序列
(println (sort-by (juxt :a :b) [{:a 1 :b 3} {:a 1 :b 2} {:a 2 :b 1}]))
;; => ({:a 1, :b 2} {:a 1, :b 3} {:a 2, :b 1})
(println ((juxt :a :b) {:a 1 :b 3}))
;; => [1 3]

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zhangningboo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值