freeCodeCamp/guide/chinese/clojure/hashmaps/index.md

4.3 KiB
Raw Blame History

title localeTitle
Clojure Hashmaps Clojure Hashmaps

散列映射是将键映射到值的集合。他们有其他语言的各种名字; Python将它们称为字典而Javascript的对象基本上像hashmaps一样工作。

与许多集合一样hashmap可以以两种方式构造。有构造函数

;; Note that each argument is *prepended* to the hashmap, not appended. 
 (def a-hashmap (hash-map :a 1 :b 2 :c 3)) 
 a-hashmap 
 ; => {:c 3, :b 2, :a 1} 

您还可以使用hashmap文字定义它们。这通常更简洁明了。建议使用逗号分隔散列图中的键/值对,因为它可以使边界更清晰。

;; This hashmap is actually in the right order, unlike the one above. 
 (def another-hashmap {:a 1, :b 2, :c 3}) 
 another-hashmap 
 ; => {:a 1, :b 2, :c 3} 

关键字和从哈希映射中检索值

耽误。这是什么? :a :b :c 那看起来很奇怪。你看那些是关键词。它们被称为_关键词_因为它们经常被用作哈希映射中的键。

为什么他们经常被用作钥匙?好吧,与字符串不同,关键字可以用作从散列映射中提取值的函数;无需getnth

(def string-hashmap {"a" 1, "b" 2, "c" 3}) 
 ("a" string-hashmap) 
 ; => ClassCastException java.lang.String cannot be cast to clojure.lang.IFn 
 
 (def keyword-hashmap {:a 1, :b 2, :c 3}) 
 (:a keyword-hashmap) 
 ; => 1 
 
 ;; You can also pass a keyword a default value in case it's not found, just like get. 
 (:not-in-the-hashmap keyword-hashmap "not found!") 
 ; => "not found!" 

将其他集合转换为哈希映射

转换为hashmap非常棘手。为了演示让我们尝试使用它像vecseq

(hash-map [:a 1 :b 2 :c 3]) 
 ; => IllegalArgumentException No value supplied for key: [:a 1 :b 2 :c 3] 

hash-map函数认为我们正在尝试使用[:a 1 :b 2 :c 3]创建一个hashmap作为其中一个键。观察如果我们给它正确数量的参数会发生什么

(hash-map [:a 1 :b 2 :c 3] "foo") 
 ; => {[:a 1 :b 2 :c 3] "foo"} 

要将序列转换为hashmap您需要使用并理解apply 。幸运的是,这是非常简单的: apply基本应用功能之前destructures的集合。

;; These two expressions are exactly the same. 
 (+ 1 2 3) 
 ; => 6 
 (apply + [1 2 3]) 
 ; => 6 

这是您将矢量转换为hashmap的方法

(apply hash-map [:a 1 :b 2 :c 3]) 
 ; => {:c 3, :b 2, :a 1} 
 
 ;; This is the same as: 
 (hash-map :a 1 :b 2 :c 3) 
 ; => {:c 3, :b 2, :a 1} 

:rocket: IDEOne吧

更新哈希映射

您可以使用assoc更新hashmap内的值。这允许您添加新的键/值对或更改旧的键/值对。

(def outdated-hashmap {:a 1, :b 2, :c 3}) 
 
 (def newer-hashmap (assoc outdated-hashmap :d 4)) 
 newer-hashmap 
 ; => {:a 1, :b 2, :c 3, :d 4} 
 
 (def newest-hashmap (assoc newer-hashmap :a 22)) 
 newest-hashmap 
 ; => {:a 22, :b 2, :c 3, :d 4} 
 
 ;; Note that outdated-hashmap has not been mutated by any of this. 
 ;; Assoc is pure and functional. 
 outdated-hashmap 
 ; => {:a 1, :b 2, :c 3} 

何时使用hashmap

想要为变量命名时,散列图非常有用。如果你曾经想过自己, _“如果我使用了一个对象......”_在你突然发现它并意识到你正在使用Clojure之前请尝试使用一个hashmap。

如果要将两个不同的值相互关联它们也很有用。例如使用一个ROT13密码你可以将\A\N \B\M等关联起来。在大多数语言中写这将是漫长而无聊的但Clojure有一些可以为你生成它的函数让它变得_有趣_

| :point_left:上一页 | :book::book: |下一个:point_right: |
| 矢量 | 目录 |待补充|