clojure/core -> vs ->>

点我查看->官方文档及更多示例

点我查看->>官方文档及更多示例

(ns $clojureDoc.core.sub-big-vs-sub-big-big)
;; ->
;; 使用方式:(-> x & forms)
;; Threads the expr through the forms. Inserts x as the
;; second item in the first form, making a list of it if it is not a
;; list already. If there are more forms, inserts the first form as the
;; second item in second form, etc.
;; 有道翻译:
;; 将expr贯穿表单。将x作为第一个表单中的第二项插入,如果它还不是一个列表,
;; 则将其作为列表。如果有更多的表单,则将第一个表单插入到第二个表单中的第二个项目中,等等。

;; 将整个表达式串起来:x作为第二项的第一个参数,注意是第一个参数第二项接收到参数后进行运算,
;; 将返回结果作为第三项的参数,第三项运算后返回结果输出或继续向下计算

;; ->>
;; 使用方式:(->> x & forms)
;; Threads the expr through the forms. Inserts x as the
;; last item in the first form, making a list of it if it is not a
;; list already. If there are more forms, inserts the first form as the
;; last item in second form, etc.
;; ->>与->相反,是将x最为第二项的最后一个参数

;; macro ->将x作为第二项的第一个参数的重要性
(println (-> 0 (/ 1)))
;; => 0
(println (macroexpand '(-> 0 (/ 1))))
;; => (/ 0 1)
;; 若调反顺序,将报错:除数为零
;(println (-> 1 (/ 0)))
;; => java.lang.ArithmeticException: Divide by zero

;; 来看看macro ->>吧
(println (->> 1 (/ 0)))
;; => 0
(println (macroexpand '(->> 1 (/ 0))))
;; => (/ 0 1)
;; 再调反顺序,将报错:除数为零
;(println (->> 0 (/ 1)))
;; => java.lang.ArithmeticException: Divide by zero

(defn f [front a1 a2 end] (str front "-" a1 "-" a2  "-" end))
(println (f 1 2 3 4))
;; => 1-2-3-4
(println (-> 1 (f 2 3 4)))
;; => 1-2-3-4
(println (->> 1 (f 2 3 4)))
;; => 2-3-4-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、付费专栏及课程。

余额充值