为什么Set的键等于值? [英] Why the Sets have keys equals to values?

查看:139
本文介绍了为什么Set的键等于值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这本书

了解ECMAScript 6

Understanding ECMAScript 6

声明

集合中没有键. ECMAScript 6背后的人 标准本可以在forEach()的设置版本中创建回调函数 接受两个参数,但这会使它与另一个参数有所不同 二.相反,他们找到了一种使回调函数保持不变的方法,并且 接受三个参数:集合中的每个值都被视为键,并且 值.因此,第一个参数和第二个参数在 set上的forEach()可使此功能与其他forEach()保持一致 数组和地图上的方法.

Sets don’t have keys, however. The people behind the ECMAScript 6 standard could have made the callback function in the set version of forEach() accept two arguments, but that would have made it different from the other two. Instead, they found a way to keep the callback function the same and accept three arguments: each value in a set is considered to be the key and the value. As such, the frst and second argument are always the same in forEach() on sets to keep this functionality consistent with the other forEach() methods on arrays and maps.

为什么不像在数组中那样做键?
背后的动机是什么?

why not to make keys as in Arrays?
what is a motive behind that?

推荐答案

从某种意义上说,Set实际上具有索引(如果没有键的话).

In some sense, Sets actually do have indices, if not keys..

从逻辑上讲,一个集合甚至没有定义的迭代顺序,不用担心键.但是事实是,new Set([1, 2, 3])的迭代顺序与new Set([3, 1, 2])不同.

Logically, a set would not even have a defined iteration order, never mind keys. But the fact is, iteration of new Set([1, 2, 3]) is ordered differently from new Set([3, 1, 2]).

这是数学和编程之间的区别.

This is the difference between mathematics and programming.

更新:问题已更新,其中包含来自评论的讨论

Update: Question updated with discussion from comments

键/索引确实不应该存在.如果有合理的方法避免甚至发生这种情况,我想他们会的.但是在编程中,我们不能只说对于集合S中的所有s ...",我们需要能够遍历那些s.但是,除了此迭代,我们不应该再寻找其他东西. new Set([1, 2, 3])new Set([3, 1, 2])的作用应尽可能相似.

The key/index really shouldn't exist. If there was a reasonable way to avoid even having it, I think they would have. But in programming we can't just say "for all s in set S..." We need to be able to iterate over those s's. But we shouldn't look for any more than this iteration. As much as possible, new Set([1, 2, 3]) and new Set([3, 1, 2]) should act as much alike as possible.

想象一下,如果我们序列化一个Trie来将集合{'baby', 'bad', 'bank', 'box', 'dad', 'dance'}存储为(b(a(b(y)dn(k))o(x))d(a(dn(c(e))))).那根本没有索引.提供这些参数的唯一原因是在各种类型中保持forEach尽可能一致.集不适合其他人.就是这样.

Imagine if we serialized a Trie to store the set {'baby', 'bad', 'bank', 'box', 'dad', 'dance'} as (b(a(b(y)dn(k))o(x))d(a(dn(c(e))))). That has no indices at all. The only reason that these are supplied is to keep forEach as consistent as possible across the various types. Set doesn't fit as well as others. That's all.

换句话说,与假装迭代索引是Set接口的有意义部分相比,重用该值(或替代地始终传递一些不相关的标记)更有意义.由于程序员不能使用数学技术,因此订购是偶然的,实施细节,这是必需的.但这不是Set s基本接口的一部分.

In other words, it makes more sense to reuse the value (or alternatively to consistently pass some irrelevant token) than to pretend the iteration indices are meaningful parts of the Set interface. Ordering is accidental, implementation details, necessary since programmers cannot use mathematical techniques. But it is not part of the essential interface of Sets.

这篇关于为什么Set的键等于值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆