JavaScript中的不可变Hash和Array实现? [英] Immutable Hash and Array implementation in JavaScript?

查看:105
本文介绍了JavaScript中的不可变Hash和Array实现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JavaScript中是否存在简单的不可变哈希和数组实现?我不需要最快的速度,比克隆的更好的合理速度会很好.

Is there simple immutable hash and array implementation in javascript? I don't need best speed, a reasonable speed better than a clone would be good.

此外,如果有简单的Java或其他语言实现可以容易理解并移植到JavaScript,那也很好.

Also, if there are simple implementations in Java or some other languages that can be easily understood and ported to JavaScript, it would be also nice.

更新:

目标不仅是冻结哈希(或数组),而且要有效地执行更新操作-不可变哈希的更新应返回一个新的不可变哈希.而且它应该比通过克隆原始文件并更新它"更有效.

The goal isn't to just froze the hash (or array), but to make an efficient implementation of update operation - update of immutable hash should return a new immutable hash. And it should be more efficient than doing it by "clone original and update it".

本地JS类型的更新复杂度类似于O(1),克隆时的复杂度将为O(n),具有特殊的不可变数据结构(我要求的)它将是0(log(n))

Native JS types have complexity of update something like O(1), with cloning the complexity will be O(n), with special immutable data structures (what I asked for) it will be 0(log(n))

UPDATE2::JavaScript已经具有Array/Hash了:

UPDATE2: JavaScript already has Array / Hash :

是的,但是它们是可变的,我需要类似但不变的东西,基本上可以通过克隆hash2 = hash1.clone(); hash2[key] = value来非常简单地完成,但是它效率很低,有一些算法使它非常有效,而无需使用clone.

Yes, but they are mutable, I need something similar but immutable, basically it can be done very simply by cloning hash2 = hash1.clone(); hash2[key] = value but it's very inefficient, there are algorithms that made it very efficient, without using the clone.

hash1 = {}
hash2 = hash1.set('key', 'value2')
hash3 = hash1.set('key', 'value3)

console.log(hash1) // => {}
console.log(hash2) // => {key: 'value2'}
console.log(hash3) // => {key: 'value3'}

解决方案:

这不是实现不可变哈希的实现,但更像是解决我当前问题的技巧,也许它也可以对某人有所帮助.

It's not an implementation for immutable hash, but more like a hack for my current problem, maybe it also helps someone.

关于为什么我需要不可变数据结构的更多信息-我使用Node.js和某种内存数据库.一个请求可以读取数据库,而另一个请求可以读取数据库-更新可能要花费很多时间(调用远程服务)-因此,我无法阻止所有读取进程并等待更新完成,更新也可能失败并且应该回滚数据库.因此,我需要以某种方式隔离(ACID)对内存数据库的读写操作.

A little more about why I need immutable data structures - I use Node.js and sort of in-memory database. One request can read database, other update it - update can take a lot of time (calling remote services) - so I can't block all read processes and wait until update will be finished, also update may fail and database should be rolled back. So I need to somehow isolate (ACID) read and write operations to the in-memory database.

这就是为什么我需要不可变的数组和哈希-实现某种MVCC的原因.但是似乎有一种更简单的方法可以做到这一点.代替直接更新数据库-更新操作仅以向数组db.someArray添加42"的形式记录对数据库的更改(但不直接执行更改).

That's why I need immutable arrays and hashes - to implement sort of MVCC. But it seems there is a simpler way to do it. Instead of updating database directly - the update operation just records changes to database (but not perform it directly) - in form of "add 42 to array db.someArray".

最后-更新操作的产物将是一系列这样的更改命令,并且由于可以非常迅速地应用-我们可以阻止数据库来应用它.

In the end - the product of update operation will be an array of such change commands, and because it can be applied very quickly - we can block the database to apply it.

但是,仍然很有趣的一点是,用javascript实现不可变数据结构的实现,所以我将这个问题保留为开放状态.

But, still it will be interesting to see if there are implementation of immutable data structures in javascript, so I'll leave this question open.

推荐答案

我知道这个问题很老,但我认为像我这样搜索的人应该指向Facebook的

I know this question is old but I thought people that were searching like me should be pointed to Facebook's Immutable.js which offers many different types of immutable data structures in a very efficient way.

这篇关于JavaScript中的不可变Hash和Array实现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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