美文网首页
学习clojure第七天

学习clojure第七天

作者: ukao | 来源:发表于2017-03-27 19:47 被阅读0次

创建数据结构

创建方法如下:

(list 1 2 3)            ; ⇒ '(1 2 3)
(vector 1 2 3)          ; ⇒ [1 2 3]
(hash-map :a 1 :b 2)    ; ⇒ {:a 1 :b 2}
(hash-set :a :b :c)     ; ⇒ #{:a :b :c}

数据结构在vector,map和set的互转

(def my-vec [1 2 3])
(set my-vec)                   ; ⇒ #{1 2 3}

(def my-map {:a 1 :b 2})
(vec my-map)                   ; ⇒ [[:a 1] [:b 2]]
(flatten (vec my-map))         ; ⇒ (:a 1 :b 2)
(set my-map)                   ; ⇒ #{[:b 2] [:a 1]}

(def my-set #{:a :b :c :d})
(vec my-set)                   ; ⇒ [:a :c :b :d]

;; And for fun:
(zipmap [:a :b :c] [1 2 3])    ; ⇒ {:c 3 :b 2 :a 1}
(apply hash-map [:a 1 :b 2])   ; ⇒ {:a 1 :b 2}

这里有一个简写的列表

literal  long name  short name
-------  ---------  ------------------
()       list       *{no short name}*
[]       vector     vec
{}       hash-map   *{no short name}*
#{}      hash-set   set

怎么使用数据结构

;; Vectors
(def v [:a :b :c])
(nth v 1)             ; ⇒ :b
(v 1)                 ; ⇒ :b  (same)
(first v)             ; ⇒ :a
(rest v)              ; ⇒ (:b :c)
(next v)              ; ⇒ (:b :c)
(last v)              ; ⇒ :c

;; Lists
;; Same as vectors, but can't index.

;; Maps
(def m {:a 1 :b 2})
(get m :a)            ; ⇒ 1
(m :a)                ; ⇒ 1       (same)
(:a m)                ; ⇒ 1       (same!)
(get m :x 44)         ; ⇒ 44      (if no :x, 44 is the default)
(keys m)              ; ⇒ (:a :b)
(vals m)              ; ⇒ (1 2)
;; Grab a key or a val from a single map entry:
(key (first m))       ; ⇒ :a
(val (first m))       ; ⇒ 1
;; Of course, note that maps are not ordered.

;; Sets
(def s #{:a :b :c})
(s :a)                ; ⇒ :a
(s :z)                ; ⇒ nil

需要注意是clojure的数据结构是不可改变的,如果对数据结构进行操作,那么它会复制一份新的。

请看如下操作:

;; Vectors
(def v   [:a :b :c])
(def li '(:a :b :c))
(conj v  :d)          ; ⇒ [:a :b :c :d]
(conj li :d)          ; ⇒ (:d :a :b :c)

v   ; ⇒ is still [:a :b :c]
li  ; ⇒ is still (:a :b :c)

;; Maps
(def m {:a 1 :b 2})
(assoc m :c 3)        ; ⇒ {:a 1 :c 3 :b 2}
(dissoc m :b)         ; ⇒ {:a 1}

m   ; ⇒ is still {:a 1 :b 2}

;; Sets
(def s #{:a :b})
(conj s :c)           ; ⇒ #{:a :c :b}
(disj s :a)           ; ⇒ #{:b}

s   ; ⇒ is still #{:a :b}

String的操作

我们之前讲过clojure的namespaces,这里使用require进行引入

(str "hi" "there")
;; ⇒ "hithere"
(count "hello")
;; ⇒ 5
(require '[clojure.string :as str])
;; ⇒ nil
(str/split "hello there" #" ")
;; ⇒ ["hello" "there"]
(str/join ["hello" "there"])
;; ⇒ "hellothere"
(str/join " " ["hello" "there"])
;; ⇒ "hello there"
(str/replace "hello there" "ll" "LL")
;; ⇒ "heLLo there"

还有一些操作

(first "hello")
;; ⇒ \h
(last "hello")
;; ⇒ \o
(rest "hello")
;; ⇒ (\e \l \l \o)
(nth "hello" 1)
;; ⇒ \e
(doseq [letter "hello"] (println letter))
;; h
;; e
;; l
;; l
;; o
;; ⇒ nil

值,不变性和持久化(Values, Immutability, and Persistence)

一个value就是一个常量,在clojure中,所有的scalars和data structure都是这样的,不可被改变,没有"远处的改变",你可以放心的使用一个数据,因为不用担心这个数据被改变。如下:
``
(def a [1 2 3 4 5])
(def b a)
;; Do what you will with b, ...
(my-func a) ; but it won't affect a.

相关文章

  • Clojure学习资料汇总

    注:转自《Clojure学习资料汇总》 官方文档 中文资料(强烈推荐):clojure入门教程clojure文档翻...

  • 学习clojure第七天

    创建数据结构 创建方法如下: 数据结构在vector,map和set的互转 这里有一个简写的列表 怎么使用数据结构...

  • Clojure 学习笔记 :10 美妙的递归

    Clojure 零基础 学习笔记 递归 尾递归 Clojure 学习笔记 :10 美妙的递归 递归,或者说函数的递...

  • 逐级做题,一种不错的学习编程的方法

    最近在学习Clojure语言,在这个网站做题, 网址是:http://www.4clojure.com/probl...

  • Clojure 学习笔记 :1 初探 Clojure

    Clojure 零基础 学习笔记 欢迎来到 Clojure 的世界。 让我们先从最经典的 hello world ...

  • 学习Clojure

    你想坚持每天做的事情是? 学习clojure 这件事情对你有什么重大的意义? 想了解函数式已经很久了,一直没有行动...

  • Clojure 学习笔记 :5 我来组成函数~

    Clojure 零基础 学习笔记 函数式编程 函数即是值。 终于,我们要介绍 Clojure 中最重要的部分了。在...

  • Clojure 脚本

    Clojure 语法 https://www.w3cschool.cn/clojure/clojure_basic...

  • 4Ruby分级题目

    闲话几句 最近学习Clojure语言,4Clojure这种分级题目的方式感觉不错,受此启发,网上没发现类似的Rub...

  • clojure 内部

    The Life of a Clojure Expression: A Quick Tour of Clojure...

网友评论

      本文标题:学习clojure第七天

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