热门标签 | 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



推荐阅读
  • Python正则表达式学习记录及常用方法
    本文记录了学习Python正则表达式的过程,介绍了re模块的常用方法re.search,并解释了rawstring的作用。正则表达式是一种方便检查字符串匹配模式的工具,通过本文的学习可以掌握Python中使用正则表达式的基本方法。 ... [详细]
  • 本文介绍了lua语言中闭包的特性及其在模式匹配、日期处理、编译和模块化等方面的应用。lua中的闭包是严格遵循词法定界的第一类值,函数可以作为变量自由传递,也可以作为参数传递给其他函数。这些特性使得lua语言具有极大的灵活性,为程序开发带来了便利。 ... [详细]
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • 本文介绍了一个Java猜拳小游戏的代码,通过使用Scanner类获取用户输入的拳的数字,并随机生成计算机的拳,然后判断胜负。该游戏可以选择剪刀、石头、布三种拳,通过比较两者的拳来决定胜负。 ... [详细]
  • 开发笔记:加密&json&StringIO模块&BytesIO模块
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了加密&json&StringIO模块&BytesIO模块相关的知识,希望对你有一定的参考价值。一、加密加密 ... [详细]
  • 本文主要解析了Open judge C16H问题中涉及到的Magical Balls的快速幂和逆元算法,并给出了问题的解析和解决方法。详细介绍了问题的背景和规则,并给出了相应的算法解析和实现步骤。通过本文的解析,读者可以更好地理解和解决Open judge C16H问题中的Magical Balls部分。 ... [详细]
  • 本文讨论了使用差分约束系统求解House Man跳跃问题的思路与方法。给定一组不同高度,要求从最低点跳跃到最高点,每次跳跃的距离不超过D,并且不能改变给定的顺序。通过建立差分约束系统,将问题转化为图的建立和查询距离的问题。文章详细介绍了建立约束条件的方法,并使用SPFA算法判环并输出结果。同时还讨论了建边方向和跳跃顺序的关系。 ... [详细]
  • C语言注释工具及快捷键,删除C语言注释工具的实现思路
    本文介绍了C语言中注释的两种方式以及注释的作用,提供了删除C语言注释的工具实现思路,并分享了C语言中注释的快捷键操作方法。 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • 展开全部下面的代码是创建一个立方体Thisexamplescreatesanddisplaysasimplebox.#Thefirstlineloadstheinit_disp ... [详细]
  • Java学习笔记之面向对象编程(OOP)
    本文介绍了Java学习笔记中的面向对象编程(OOP)内容,包括OOP的三大特性(封装、继承、多态)和五大原则(单一职责原则、开放封闭原则、里式替换原则、依赖倒置原则)。通过学习OOP,可以提高代码复用性、拓展性和安全性。 ... [详细]
  • Go Cobra命令行工具入门教程
    本文介绍了Go语言实现的命令行工具Cobra的基本概念、安装方法和入门实践。Cobra被广泛应用于各种项目中,如Kubernetes、Hugo和Github CLI等。通过使用Cobra,我们可以快速创建命令行工具,适用于写测试脚本和各种服务的Admin CLI。文章还通过一个简单的demo演示了Cobra的使用方法。 ... [详细]
  • Java序列化对象传给PHP的方法及原理解析
    本文介绍了Java序列化对象传给PHP的方法及原理,包括Java对象传递的方式、序列化的方式、PHP中的序列化用法介绍、Java是否能反序列化PHP的数据、Java序列化的原理以及解决Java序列化中的问题。同时还解释了序列化的概念和作用,以及代码执行序列化所需要的权限。最后指出,序列化会将对象实例的所有字段都进行序列化,使得数据能够被表示为实例的序列化数据,但只有能够解释该格式的代码才能够确定数据的内容。 ... [详细]
  • 本文介绍了UVALive6575题目Odd and Even Zeroes的解法,使用了数位dp和找规律的方法。阶乘的定义和性质被介绍,并给出了一些例子。其中,部分阶乘的尾零个数为奇数,部分为偶数。 ... [详细]
  • 前景:当UI一个查询条件为多项选择,或录入多个条件的时候,比如查询所有名称里面包含以下动态条件,需要模糊查询里面每一项时比如是这样一个数组条件:newstring[]{兴业银行, ... [详细]
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社区 版权所有