Scala:var List vs val MutableList [英] Scala: var List vs val MutableList

查看:127
本文介绍了Scala:var List vs val MutableList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Odersky等人的Scala书中,他们说使用列表。我没有读过书的封面,但所有的例子似乎使用val列表。我理解它也鼓励使用vals vars。但在大多数应用程序中,使用var List或val MutableList之间没有折衷。显然,当我们可以使用val列表。 但是是使用大量var列表(或var矢量等)的好习惯吗?



从C#。我有很多:

  public List< T& myList {get; private set;} 

集合,如果C#内置了不变性,很容易被声明为vals,因为在构建之后,集合本身从未改变,即使在它的生命周期中元素将被添加和从集合中减去。因此,声明一个var集合几乎感觉像是从不变性一步一步。



为了回应回答和评论,Scala的强大卖点之一是:许多好处,而不必像写Lisp或Haskell那样完全改变编写代码的方式。

解决方案


是否是使用大量var列表(或var向量
等)的好习惯?


我会说,更好的做法是使用不可变集合使用 var ,而不是使用可变的集合使用 val 。因为




  • 你对行为有更多的保证:如果你的对象有一个可变的列表,你永远不会知道是否有其他外部对象要更新它


  • 您限制了可变性的程度;


  • 通过简单地将var赋值给val,很容易使var不可变




在某些情况下,您可以使用不同的集合类型,例如具有广泛I / O的时间相关应用,最简单的解决方案是使用可变状态。在一些情况下,可变解决方案更加优雅。然而在大多数代码中,你根本不需要可变性。关键是使用具有更高阶函数而不是循环的集合,或者如果不存在合适的函数,则递归。这比它听起来更简单。你只需要花一些时间去了解List(和其他集合,大部分是相同的)的方法。最重要的是:



map :将您提供的函数应用于集合中的每个元素 - 循环和更新数组中的值



foldLeft :从集合中返回单个结果 - 更新累加器变量



for-yield 表达式:简化映射和过滤, / p>

最终,很多函数式编程是不可变性的结果,你不可能有一个没有另一个;然而,局部变量主要是一个实现细节:只要它不能从局部范围逃脱,有一点可变性没有什么问题。所以使用vars不可变集合,因为局部var不是将要导出的。


In Odersky et al's Scala book, they say use lists. I haven't read the book cover to cover but all the examples seem to use val List. As I understand it one also is encouraged to use vals over vars. But in most applications is there not a trade off between using a var List or a val MutableList?. Obviously we use a val List when we can. But is it good practice to be using a lot of var Lists (or var Vectors etc)?

I'm pretty new to Scala coming from C#. There I had a lot of:

public List<T> myList {get; private set;}

collections which could easily have been declared as vals if C# had immutability built in, because the collection itself never changed after construction, even though elements would be added and subtracted from the collection in its life time. So declaring a var collection almost feels like a step away from immutability.

In response to answers and comments, one of the strong selling points of Scala is: that it can have many benefits without having to completely change the way one writes code as is the case with say Lisp or Haskell.

解决方案

Is it good practice to be using a lot of var Lists (or var Vectors etc)?

I would say it's better practice to use var with immutable collections than it is to use val with mutable ones. Off the top of my head, because

  • You have more guarantees about behaviour: if your object has a mutable list, you never know if some other external object is going to update it

  • You limit the extent of mutability; methods returning a collection will yield an immutable one, so you only have mutablility within your one object

  • It's easy to immutabilize a var by simply assigning it to a val, whereas to make a mutable collection immutable you have to use a different collection type and rebuild it

In some circumstances, such as time-dependent applications with extensive I/O, the simplest solution is to use mutable state. And in some circumstances, a mutable solution is just more elegant. However in most code you don't need mutability at all. The key is to use collections with higher order functions instead of looping, or recursion if a suitable function doesn't exist. This is simpler than it sounds. You just need to spend some time getting to know the methods on List (and other collections, which are mostly the same). The most important ones are:

map: applies your supplied function to each element in the collection - use instead of looping and updating values in an array

foldLeft: returns a single result from a collection - use instead of looping and updating an accumulator variable

for-yield expressions: simplify your mapping and filtering especially for nested-loop type problems

Ultimately, much of functional programming is a consequence of immutability and you can't really have one without the other; however, local vars are mostly an implementation detail: there's nothing wrong with a bit of mutability so long as it cannot escape from the local scope. So use vars with immutable collections since the local vars are not what will be exported.

这篇关于Scala:var List vs val MutableList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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