clojure/core reduce

点我查看原文

(ns $clojureDoc.core.reduce)
;; 使用方法:(reduce f coll) (reduce f val coll)
;; f should be a function of 2 arguments.
;; 函数f需要两个参数

;; If val is not supplied, returns the result of applying f to the first 2 items in coll, then
;;  applying f to that result and the 3rd item, etc.
;; 如果val为空,即(reduce f coll),那么返回一个运算结果:coll的前两项作为f的参数,得到的结果再与coll的第三项参与f运算,依次类推,直到coll最后一项

;; If coll contains no items, f must accept no arguments as well, and reduce returns the
;;  result of calling f with no arguments.
;; 如果集合coll为空,函数f变成不接受参数的函数,reduce此时返回的就是调用无参函数f的结果

;; If coll has only 1 item, it is returned and f is not called.
;; 如果集合coll只有一项,那么直接将coll的此项作为结果返回,函数f没有被调用

;; If val is supplied, returns the result of applying f to val and the first item in coll, then
;;  applying f to that result and the 2nd item, etc.
;; 如果有val,那么reduce返回一个结果:val和coll的第一项参与函数f运算,结果与coll的第二项参与运算,依次类推,直到coll最后一项

;; If coll contains no items, returns val and f is not called.
;; 如果有val,集合coll为空,那么直接返回val,函数f没有被调用

(println (reduce + '(1 2 3 4 5)))
;; => 15

(println (identity '()))
;; => ()

;; 返回的是调用无参函数f的结果
(println (reduce (fn [] 1) '()))
;; => 1

(println (reduce (fn [] (println "===")) '(1)))
;; => 1

(println (reduce + 1 '(2 3 4 5)))
;; => 15

(println (reduce (fn [] (println "===")) 1 '()))
;; => 1

 

  • 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、付费专栏及课程。

余额充值