美文网首页
Docker下安装 Clojure

Docker下安装 Clojure

作者: mecury | 来源:发表于2019-01-23 11:25 被阅读7次

    Docker环境下Clojure

    1. 搜索 Clojure 镜像,下载,并在命令行下启动镜像
    > docker search clojure
    > docker pull clojure
    > docker run -i -t clojure /bin/bash
    
    1. 使用 ClojureREPL(standing for Read-Eval-Print Loop)
      当进入 Clojure 下的命令行模式下:
    > lein new hello-world
    > cd hello-world/
    > lein repl                    # 启动 repl 环境
    

    执行最后一句时,会自动安装一些东西,并且进入 REPL 环境.如下所示:

    root@5d542fdfabba:/tmp/hello-world# lein repl
    Retrieving org/clojure/clojure/1.9.0/clojure-1.9.0.pom from central
    Retrieving org/clojure/spec.alpha/0.1.143/spec.alpha-0.1.143.pom from central
    Retrieving org/clojure/core.specs.alpha/0.1.24/core.specs.alpha-0.1.24.pom from central
    Retrieving org/clojure/clojure/1.9.0/clojure-1.9.0.jar from central
    Retrieving org/clojure/core.specs.alpha/0.1.24/core.specs.alpha-0.1.24.jar from central
    Retrieving org/clojure/spec.alpha/0.1.143/spec.alpha-0.1.143.jar from central
    nREPL server started on port 37573 on host 127.0.0.1 - nrepl://127.0.0.1:37573
    REPL-y 0.4.3, nREPL 0.5.3
    Clojure 1.9.0
    OpenJDK 64-Bit Server VM 1.8.0_181-8u181-b13-2~deb9u1-b13
        Docs: (doc function-name-here)
              (find-doc "part-of-name-here")
      Source: (source function-name-here)
     Javadoc: (javadoc java-object-or-class-here)
        Exit: Control+D or (exit) or (quit)
     Results: Stored in vars *1, *2, *3, an exception in *e
    
    hello-world.core=>
    

    语法特性说明来自于下面链接,这里只列举了一些我看代码用到了一些语法:

    Clojure API

    一些语法特性:

    ->

    (-> x & forms)  // x 作为 forms 的第二个参数进行处理
    
    => (-> 1 (+ 3) (/ 2))  // (/ (+ 1 3) 2 
    2
    

    ->>

    (->> x & forms)  // x 作为 forms 的最后一个参数进行处理
    
    => (->> 1 (+ 3) (/ 2))
    1/2
    

    assoc

    (assoc map key val)
    (assoc map key val & kvs)  //添加后面的 kv (存在就更新)返回一个新的 map
    
    => (assoc {:key1 "old value1" :key2 "value2"}:key1 "value1" :key3 "value3")
    {:key1 "value1", :key2 "value2", :key3 "value3"}
    => (assoc [1 2 3] 0 10)    //替换index 为 0 的value为 10
    [10 2 3]
    => (assoc [1 2 3] 3 10)
    [1 2 3 10]
    

    apply

    (apply f args)
    (apply f x args)
    (apply f a b c d & args)  //应用由 f 构造的匿名方法,参数是后面的各个项的拆分
    
    => (max [1 2 3])
    [1 2 3]
    => (apply max [1 2 3])
    3
    => (apply max 1 2 3)
    IllegalArgumentException Don't know how to create ISeq from: java.lang.Long  clojure.lang.RT.seqFrom (RT.java:550)
    => (max 1 2 3)
    3
    

    let

    (let [bindings*] exprs*)   //在bingings中进行数据绑定。然后执行表达式expres
    
    => ( let [a 1 b 2] (+ a b))
    3
    => (let [c (+ 1 2) [d e] [5 6]] (-> (+ d e) (- c)))
    8
    

    if-let

    (if-let bindings then)(if-let bindings then else & oldform)
    
    (defn if-let-demo [arg]
      (if-let [x arg]
        "then"
        "else"))
    
    (if-let-demo 1) ; anything except nil/false
    ;;=> "then"
    (if-let-demo nil)
    ;;=> "else"
    (if-let-demo false)
    ;;=> "else"
    

    merge

    (merge & maps) 返回一个map,后面的 maps 添加到前面的map
    
    => (merge {:a 1 :b 2 :c 3} {:b 9 :d 4})
    {:a 1, :b 9, :c 3, :d 4}
    

    str

    (str)
    (str x)
    (str x & ys) 拼接字符串
    
    => (str "L" 5 "a")
    "L5a"
    

    cond->

    (cond-> expr & clauses) //
    
    => (defn say [n]
         (cond-> nil
           (divisible-by? 3 n) (str "Fizz")
           (divisible-by? 5 n) (str "Buzz")
           :always             (or (str n))))
    #'user/say
    
    => (say 1)
    "1"
    
    => (say 3)
    "Fizz"
    
    => (say 5)
    "Buzz"
    
    => (say 15)
    "FizzBuzz"
    

    select-keys

    (select-keys map keyseq)
    
    user=> (select-keys {:a 1 :b 2} [:a])
    {:a 1}
    user=> (select-keys {:a 1 :b 2} [:a :c])
    {:a 1}
    user=> (select-keys {:a 1 :b 2 :c 3} [:a :c])
    {:c 3, :a 1}
    

    相关文章

      网友评论

          本文标题:Docker下安装 Clojure

          本文链接:https://www.haomeiwen.com/subject/yjbfsxtx.html