热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

clojure_ClojureHashmaps解释:如何从Hashmaps检索值并更新Hashmaps

clojureAhashmapisacollectionthatmapskeystovalues.Theyhavevariousnamesinotherlanguages–Pyth

clojure

A hashmap is a collection that maps keys to values. They have various names in other languages – Python refers to them as dictionaries, and Javascript’s objects essentially work like hashmaps.

哈希图是将键映射到值的集合。 它们在其他语言中有不同的名称-Python将它们称为字典,而Javascript的对象本质上就像哈希图一样工作。

A hashmap can, like many collections, be constructed in two ways. There is the constructor function:

像许多集合一样,哈希图可以通过两种方式构造。 有构造函数:

;; 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}

You can also define them using a hashmap literal. This is often more concise and clear. Using commas to separate key/value pairs in hashmaps is recommended, as it can make the boundaries more clear.

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

;; 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}

何时使用哈希图? (When to use a hashmap?)

A hashmap is useful when you want to give names to your variables. If you’re ever thinking to yourself, “What if I used an object…” before you snap out of it and realize you’re using Clojure, try using a hashmap.

当您想给变量命名时,哈希表很有用。 如果您曾经想过自己, “如果我使用一个对象……该怎么办……”,然后才意识到它正在使用Clojure,请尝试使用哈希图。

They are also useful if you want to associate two different values with each other. Take, for example, a ROT13 cipher – you could associate \A with \N, \B with \M, and so on.

如果要将两个不同的值相互关联,它们也很有用。 以ROT13密码为例–您可以将\A\N关联,将\B\M关联,依此类推。

This would be long and boring to write in most languages, but Clojure has some functions that can generate it for you and make it fun!

用大多数语言编写代码会很长而且很无聊,但是Clojure的某些功能可以为您生成它并使它变得有趣!

从哈希表检索关键字和值 (Keywords and retrieving values from hashmaps)

Hold up. What is this? :a? :b? :c? Those look odd. Those, you see, are keywords. They’re called key-words because they’re often used as keys in hashmaps.

耽误。 这是什么? :a ? :b ? :c ? 那些看起来很奇怪。 您看到的是关键字。 之所以将它们称为“ 关键字” ,是因为它们通常在哈希图中用作关键字。

Why are they often used as keys? Well, unlike strings, keywords can be used as functions to extract values from a hashmap; no need for get or nth!

为什么它们经常用作键? 好吧,与字符串不同,关键字可以用作从哈希图中提取值的函数。 不需要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!"

更新哈希图 (Update a hashmap)

You can update values inside a hashmap using assoc. This allows you to append new key/value pairs or change old ones.

您可以使用assoc更新哈希图中的值。 这使您可以追加新的键/值对或更改旧的键/值对。

(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}

将其他集合转换为哈希图 (Converting other collections to hashmaps)

Converting to a hashmap is tricky. To demonstrate, let’s try using it like vec or seq.

转换为哈希图非常棘手。 为了演示,让我们尝试像vecseq一样使用它。

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

The hash-map function thinks that we’re trying to create a hashmap with [:a 1 :b 2 :c 3] as one of the keys. Watch what happens if we give it the right number of arguments:

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"}

To convert a sequence to a hashmap, you’ll need to use and understand apply. Luckily, this is pretty simple: apply essentially destructures a collection before applying a function to it.

要将序列转换为哈希图,您需要使用并了解apply 。 幸运的是,这非常简单: apply本质上会在对集合应用函数之前对集合进行解构。

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

This is how you would convert a vector to a 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}

That should be everything you need to get started with hashmaps in Clojure. Now get out there and start hashing with the best of 'em.

那应该是开始使用Clojure的哈希图所需的一切。 现在走到那里,开始用最好的'em'哈希算法。

翻译自: https://www.freecodecamp.org/news/clojure-hashmaps-explained-how-to-retrieve-values-from-and-update-hashmaps/

clojure



推荐阅读
author-avatar
橄榄村
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有