scala:如何避免突变?

发布于 2024-12-29 18:52:04 字数 75 浏览 2 评论 0原文

当人们需要积累一些数据时,这是很常见的。我习惯的方式是将数据块附加到数组中。但这在 scala 中是一种不好的做法,那么我该如何避免呢?

It is a commonplace when one needs to accumulate some data. The way I get used to do it is appending data chunks to array. But it's a bad practice in scala, so how can I avoid it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

简单爱 2025-01-05 18:52:04

嗯,有两种处理累积的通用方法:递归和折叠。让我们看一下每个非常简单的示例,以计算列表值的总和。

def sumRecursively(list: List[Int]): Int = {
  def recurse(list: List[Int], acc: Int): Int =
    if (list.isEmpty) acc
    else recurse(list.tail, acc + list.head)
  recurse(list, 0)
}

def sumFolding(list: List[Int]): Int =
  list.foldLeft(0){ case (acc, n) => acc + n }

这有很多变体,可以更好地处理一种或另一种情况。

Well, there are two generic ways of dealing with accumulation: recursion and folding. Let's look into very simple examples of each, to compute the sum of values of a list.

def sumRecursively(list: List[Int]): Int = {
  def recurse(list: List[Int], acc: Int): Int =
    if (list.isEmpty) acc
    else recurse(list.tail, acc + list.head)
  recurse(list, 0)
}

def sumFolding(list: List[Int]): Int =
  list.foldLeft(0){ case (acc, n) => acc + n }

There are many variations on this, which handle better one case or another.

我很OK 2025-01-05 18:52:04

事实上,事实并非如此。您可以在 scala 中使用 Vector,默认情况下它是 scala.collection.immutable 包的一部分。这将创建一个不可变的集合,每次追加到它时都会返回一个新的(不同的)实例。

更多信息:

http://www.scala-lang.org/ docu/files/collections-api/collections_15.html

Actually, it isn't. You can use a Vector in scala which is part of the scala.collection.immutable package by default. That will create an immutable collection that returns a new (different) instance every time you append to it.

More information:

http://www.scala-lang.org/docu/files/collections-api/collections_15.html

风筝有风,海豚有海 2025-01-05 18:52:04

对于大多数常见用途,“map”和“flatMap”操作用于在功能上生成数据结构。两者都从一个数据结构开始,对其中的每个元素应用一些操作,并返回与原始形状相同的新数据结构。它们的不同之处在于新数据结构的填充方式。这两者如此常见且如此强大,以至于 Scala 包含了一种特殊的语法,即 for 理解来支持它们。 for 理解表面上看起来类似于 Java 风格的 for 循环,但实际上编译为一系列 map 和 flatMap 调用(以及其他一些调用)。

在函数式编程中,通常将问题分解为从一种数据结构到另一种数据结构的转换,而不是明确描述构建和销毁数据结构所需的步骤。这需要一些时间来适应,特别是在弄清楚要开始使用什么数据结构时。一旦你掌握了它,这是一种非常强大的技术,可以清晰、精确地表达大量功能,并且几乎没有错误潜入的空间。

还值得注意的是,“map”和“flatMap”实际上都是另一个更强大的功能:“折叠”。 “fold”(出于技术原因,同时实现为“foldLeft”和“foldRight”)可用于构建数据结构和分解数据结构。

For most common uses, the "map" and "flatMap" operations are used to functionally generate data structures. Both start with one data structure, apply some operation to each element in it, and return a new data structure of the same shape as the original. They differ in just how the new data structure is populated. These two are so common and so powerful that Scala includes a special syntax, the for-comprehension, to support them. The for-comprehension looks superficially similar to a Java-style for-loop, but actually compiles to a series of map and flatMap calls (among a few others).

In functional programming, it is common to break down your problem into transformations like this from one data structure to another, rather than explicitly describing the steps necessary to build and destroy your data structures. This takes some getting used to, particularly in figuring out what data structure to start wit. Once you master it this is extremely powerful technique, allowing large chunks of functionality to be expressed clearly, precisely, and with little room for bugs to creep in.

It's also worth noting that both "map" and "flatMap" are actually special cases of another, more powerful function: "fold". "fold" (implemented as both "foldLeft" and "foldRight", for technical reasons) can be used to both build up data structures and break them down.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文