更新惯用Scala中的大型数据结构 [英] Updating large data structures in idiomatic Scala

查看:156
本文介绍了更新惯用Scala中的大型数据结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试Scala一段时间,并经常遇到建议来支持不可变的数据结构。



但是当你有一个数据结构,例如一个3D场景图,一个大型神经网络,或任何需要频繁更新的对象的任何东西(动画场景中的对象,训练神经网络,...),这似乎是


  1. 在运行时,由于您需要不断重新创建整个对象图,所以非常低效,而


  2. p>难以编程,因为当你有一些需要更新的对象的引用时,你不能仅仅调用它们上的setter,而是需要复制对象图并用更新的对象替换旧的对象。


在惯用Scala中如何处理这些事情?

解决方案

Scala是多范式的:OO和功能,可变和不可变。



复杂图是数据结构的一个例子,就像你已经确定,可能更容易在可变的上下文中使用。如果是这样,使数据结构变得可变。



惯用Scala是使用正确的范例来解决您的问题。


I've been experimenting with Scala for some time, and have often encountered the advice to favor immutable data structures.

But when you have a data structure like e.g. a 3D scene graph, a large neural network, or anything with quite a few objects that need frequent updates (animating the objects in the scene, training the neural net, ...), this seems to be

  1. horribly inefficient at runtime since you need to constantly recreate the whole object graph, and

  2. difficult to program since when you have a reference to some objects that need to be updated, you can't just call setters on them but you need copy the object graph and replace the old objects with the updated ones.

How are such things dealt with in idiomatic Scala?

解决方案

Scala is multi-paradigm: OO and functional, mutable and immutable.

Complex graphs are one example of a data structure that, as you have identified, may be easier to work with in a mutable context. If so, make the data structure mutable.

Idiomatic Scala is to use the right paradigm to solve your problem.

这篇关于更新惯用Scala中的大型数据结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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