zoukankan      html  css  js  c++  java
  • Clojure学习之比线性箭头操作

    1. 单箭头( -> )

      单箭头操作符会把其参数form迭代式地依次插入到相邻的下个一个form中作为该form的第一个参数。这就好像把这些form串起来了,即线性化(Threading)。 由于它总是把前一个参数作为接下来的form的第一个参数插入,这种操作也叫 thread-first 。注意,如果在->的参数序列中存在非form的元素,则->会先把它转化成一个form,然后再把它们串起来,这对下面提到的双箭头操作符也是适用的,比如下面三行代码就是等价的:它们得到的结果都是,(5 4 2 3),请注意前两行对rest使用上的不同。

    1 (-> [1 2 3] rest (conj 4 5))
    2 (-> [1 2 3] (rest)(conj 4 5))
    3 (conj (rest [1 2 3]) 4 5)

      其中第一行代码由->操作符串起了[1 2 3]rest(conj 4 5)三个参数。其中第一个为一 vector 数据,第二个为一函数,第三个为一个简单的form。

    2. 双箭头( ->> )

      双箭头操作符与单箭头操作符类似,不过它是把前一个参数form做为最后一个参数插入到接下来的form中的,所以它又叫 thread-last,示例1

    user=> (->> (range)
                (map #(* % %))
                (filter even?)
                (take 10)
                (reduce +))
    1140
    
    ;; This expands to:
    user=> (reduce +
                   (take 10
                         (filter even?
                                 (map #(* % %)
                                      (range)))))
    1140

     示例2:-> 和 ->> 对比

    1 (->> "hello" (str " jmd"))
    2 " jmdhello"
    3 user=> (-> "hello" (str " jmd"))
    4 => "hello jmd"
  • 相关阅读:
    Linux xargs 命令
    Shell 流程控制
    springSecurity---AuthenticationProvider解析
    nginx unknown directive "stream"
    Linux清空文件内容
    解决RabbitMQ报错 Error: unable to connect to node rabbit@localhost:
    mysql bit类型 使用Mysql命令行查询的时候无法看到其值
    今日进度
    每周总结
    今日进度
  • 原文地址:https://www.cnblogs.com/sunfie/p/7400293.html
Copyright © 2011-2022 走看看