从clojure中的哈希映射获取指定索引处的值

从clojure中的哈希映射获取指定索引处的值,clojure,Clojure,我正在用Clojure做第二个家庭作业,我很难从哈希映射中获取指定索引处的值。目前,我有一个函数来创建表示图形的哈希映射向量。然后,我索引到该向量中,以获得表示节点的特定哈希映射,特别是与其他节点的连接的节点;因此 homework2.core> (reg-ring-lattice 5 2) [#{1 4} #{0 2} #{1 3} #{4 2} #{0 3}] homework2.core> ((reg-ring-lattice 5 2) 2) #{1 3} 我的问题是

我正在用Clojure做第二个家庭作业,我很难从哈希映射中获取指定索引处的值。目前,我有一个函数来创建表示图形的哈希映射向量。然后,我索引到该向量中,以获得表示节点的特定哈希映射,特别是与其他节点的连接的节点;因此

homework2.core> (reg-ring-lattice 5 2)  
[#{1 4} #{0 2} #{1 3} #{4 2} #{0 3}] 
homework2.core> ((reg-ring-lattice 5 2) 2)
#{1 3} 
我的问题是迭代哈希映射本身中的值。我特别想从散列映射中检索
n
th索引。这是我试过的

homework2.core> ((reg-ring-lattice 5 2) 3) 
#{4 2}
homework2.core> (((reg-ring-lattice 5 2) 3) 0) 
nil
homework2.core> (get ((reg-ring-lattice 5 2) 3) 0) 
nil
我已经搜索过了,但目前我对Clojure及其关键词/术语的了解有限


第一个问题是,我是用它们的专有名称来称呼这些收藏吗。其次,我如何在我称之为哈希映射的内容中检索指定索引处的值?

这些不是哈希映射,而是哈希集,它们没有排序。在顺序/索引访问中,您将得到一个“随机”元素。例如

user=> (first #{1 2 3 4 5})
1
user=> (second #{1 2 3 4 5})
4

这些不是散列映射,而是散列集,它们没有排序。在顺序/索引访问中,您将得到一个“随机”元素。例如

user=> (first #{1 2 3 4 5})
1
user=> (second #{1 2 3 4 5})
4

哈希映射是字典、键/值对。它们显示在输出中,用大括号括起来->{}。集合类似于列表,但必须包含唯一的项。集合显示在输出中,周围是一个带花括号的八字叉->#{}。向量是包含索引序列中的项的结构。它们显示在输出中,并用方括号括起来->[]

看,还有

您的reg环晶格函数正在返回集合向量。像您这样索引到由数字组成的集合没有多大意义,所以让我来回顾一下您在示例代码块中所做的工作

;This output is a vector of sets
(reg-ring-lattice 5 2)
[#{1 4} #{0 2} #{1 3} #{4 2} #{0 3}]

;This output exploits the fact that vectors implement IFn (see the doc links)
; Surrounding the call in another set of parens makes another call the argument
; of which is '2'. You seem to have a good grasp on all this so far.
((reg-ring-lattice 5 2) 2) ; returns the item at index 2 in the vector
#{1 3}

;This is exactly the same as the above except for getting the item at index 3.
((reg-ring-lattice 5 2) 3)
#{4 2}

;This is what doesn't make sense. Sets are functions of their members. They
;aren't indexed like vectors are
(((reg-ring-lattice 5 2) 3) 0)
; What you are saying here is 'give me item 0, not index 0, out of the set
; returned from looking into the vector at index 3 or nil if not found'.
; It would work if your set had a 0 in it or if you did this:
(((reg-ring-lattice 5 2) 3) 4) ; -> returns 4
; but it's not too helpful because you already know what you're looking up.

;Instead, this is where you start treating the returned set like a list
(first ((reg-ring-lattice 5 2) 3)) ; -> returns 4
(second ((reg-ring-lattice 5 2) 3)) ; -> returns 2

对于集合,有一件事需要注意,如果您生成的是已排序的集合,您可以忽略这一点,因为它们可能不会被排序。上一次是第一次,下一次可能不是第一次。另一件要注意的是复制品。集合中的每个项目都必须是唯一的。不能有这样的集合->{4}。

哈希映射是字典、键/值对。它们显示在输出中,用大括号括起来->{}。集合类似于列表,但必须包含唯一的项。集合显示在输出中,周围是一个带花括号的八字叉->#{}。向量是包含索引序列中的项的结构。它们显示在输出中,并用方括号括起来->[]

看,还有

您的reg环晶格函数正在返回集合向量。像您这样索引到由数字组成的集合没有多大意义,所以让我来回顾一下您在示例代码块中所做的工作

;This output is a vector of sets
(reg-ring-lattice 5 2)
[#{1 4} #{0 2} #{1 3} #{4 2} #{0 3}]

;This output exploits the fact that vectors implement IFn (see the doc links)
; Surrounding the call in another set of parens makes another call the argument
; of which is '2'. You seem to have a good grasp on all this so far.
((reg-ring-lattice 5 2) 2) ; returns the item at index 2 in the vector
#{1 3}

;This is exactly the same as the above except for getting the item at index 3.
((reg-ring-lattice 5 2) 3)
#{4 2}

;This is what doesn't make sense. Sets are functions of their members. They
;aren't indexed like vectors are
(((reg-ring-lattice 5 2) 3) 0)
; What you are saying here is 'give me item 0, not index 0, out of the set
; returned from looking into the vector at index 3 or nil if not found'.
; It would work if your set had a 0 in it or if you did this:
(((reg-ring-lattice 5 2) 3) 4) ; -> returns 4
; but it's not too helpful because you already know what you're looking up.

;Instead, this is where you start treating the returned set like a list
(first ((reg-ring-lattice 5 2) 3)) ; -> returns 4
(second ((reg-ring-lattice 5 2) 3)) ; -> returns 2

对于集合,有一件事需要注意,如果您生成的是已排序的集合,您可以忽略这一点,因为它们可能不会被排序。上一次是第一次,下一次可能不是第一次。另一件要注意的是复制品。集合中的每个项目都必须是唯一的。你不能有这样的集合->{4}。

正如其他人所指出的,你使用的是集合向量,而不是地图向量

将集合作为函数调用将返回参数(如果它在集合中),否则
nil

(#{2 3 4} 1) ;nil
(#{2 3 4} 2); 2
您想从集合中检索
n
th元素:

(nth (seq #{2 3 4}) 1)
;3
订单可能不是您期望的:

(seq #{4 2 3})
; (2 3 4)
但是,为什么要使用
n
th元素呢?您可以通过调用集合上的
seq
来枚举集合的元素(如果您使用的是
map
reduce
等函数,则隐式地)。如果你告诉我们你想做什么,很可能有一种更惯用的方法。而
n
的速度很慢-它一个元素一个元素地告诉序列

顺便说一下,通过使用集合向量,您可以将自己绑定到节点标记为
0
n
的图上。集合地图通常是更好的选择:

{4 #{0 3}, 3 #{2 4}, 2 #{1 3}, 1 #{0 2}, 0 #{1 4}}
。。。而不是

[#{1 4} #{0 2} #{1 3} #{4 2} #{0 3}] 

正如其他人指出的,您使用的是集合向量,而不是地图向量

将集合作为函数调用将返回参数(如果它在集合中),否则
nil

(#{2 3 4} 1) ;nil
(#{2 3 4} 2); 2
您想从集合中检索
n
th元素:

(nth (seq #{2 3 4}) 1)
;3
订单可能不是您期望的:

(seq #{4 2 3})
; (2 3 4)
但是,为什么要使用
n
th元素呢?您可以通过调用集合上的
seq
来枚举集合的元素(如果您使用的是
map
reduce
等函数,则隐式地)。如果你告诉我们你想做什么,很可能有一种更惯用的方法。而
n
的速度很慢-它一个元素一个元素地告诉序列

顺便说一下,通过使用集合向量,您可以将自己绑定到节点标记为
0
n
的图上。集合地图通常是更好的选择:

{4 #{0 3}, 3 #{2 4}, 2 #{1 3}, 1 #{0 2}, 0 #{1 4}}
。。。而不是

[#{1 4} #{0 2} #{1 3} #{4 2} #{0 3}] 

虽然顺序从打印的更改为
seq
d,但如果我在I
seq
之后迭代列表,它们会保持顺序吗?@BumSkeeter任何给定的集合-无论何种类型-都将始终以相同的顺序枚举,这是它的
seq
:在集合和映射的情况下,不需要这样做,是提供元素来构造它的顺序。@BumSkeeter,您不应该依赖hashset或hashmap的顺序,因为添加新元素时顺序不稳定。如果您确实需要这些集合,但需要排序,则有
排序集
排序映射
。但在这种情况下,顺序是通过比较元素而不是插入顺序来确定的。@Thumbnail:不能在集合上进行第n次。第一个、第二个、下一个、最后一个,但不是第n个。@ASammich如您所见,该示例在集合的
seq
上调用
n
th元素。我已将后续的
n
元素更改为
n
th元素,以消除任何歧义。似乎奇怪的是,
first
second
last
有一个隐式的
seq
,而
nth
没有,但我们在这里看到了