在Scala中foldLeft和reduceLeft之间的区别

编程入门 行业动态 更新时间:2024-10-14 20:27:11
本文介绍了在Scala中foldLeft和reduceLeft之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我学到了 foldLeft 和 reduceLeft $ b $之间的基本区别b foldLeft:

  • 必须传递初始值

reduceLeft:

  • 集合作为初始值
  • 如果collection为空,则抛出异常

是否还有其他区别?

具有类似功能的两种方法的具体原因是什么?

在提供实际答案之前,有几件事要提到:

  • 你的问题与 left ,这是关于reduce和folding之间的区别
  • 差异不在于实现,只是看签名。 b $ b
  • 这个问题与Scala没有任何关系,特别是它而不是函数式编程的两个概念。

回到您的问题:

以下是 foldLeft (也可以是 foldRight (B:A)(B:A,B,A,B,C) )=> B):B

这里是 reduceLeft 这两个看起来非常相似, 。 reduceLeft 是 foldLeft 的一种特殊情况(顺便说明您有时可以

当您调用 reduceLeft 时,比如说 List [Int] 它会逐字地将整个整数列表减少为一个单值,这个值将是类型 Int (或超$ Int ,因此 [B>:A] )。

当您在 List [Int] 上调用 foldLeft 时,它会折叠整个列表(想象把一张纸卷成一个单一的值),但这个值并不一定与 Int 有关(因此 [B] )。

下面是一个例子:

def listWithSum(numbers:List [Int])= numbers.foldLeft((List [Int](),0)){(resultsTuple,currentInteger)=> (currentInteger :: resultsTuple._1,currentInteger + resultsTuple._2)}

此方法使用 List [Int] 并返回一个 Tuple2 [List [Int],Int] 或(List [Int] - > Int)。它计算总和并返回一个包含整数列表和总和的元组。顺便说一下,列表向后返回,因为我们使用 foldLeft 而不是 foldRight 。

I have learned the basic difference between foldLeft and reduceLeft

foldLeft:

  • initial value has to be passed

reduceLeft:

  • takes first element of the collection as initial value
  • throws exception if collection is empty

Is there any other difference ?

Any specific reason to have two methods with similar functionality?

解决方案

Few things to mention here, before giving the actual answer:

  • Your question doesn't have anything to do with left, it's rather about the difference between reducing and folding
  • The difference is not the implementation at all, just look at the signatures.
  • The question doesn't have anything to do with Scala in particular, it's rather about the two concepts of functional programming.

Back to your question:

Here is the signature of foldLeft (could also have been foldRight for the point I'm going to make):

def foldLeft [B] (z: B)(f: (B, A) => B): B

And here is the signature of reduceLeft (again the direction doesn't matter here)

def reduceLeft [B >: A] (f: (B, A) => B): B

These two look very similar and thus caused the confusion. reduceLeft is a special case of foldLeft (which by the way means that you sometimes can express the same thing by using either of them).

When you call reduceLeft say on a List[Int] it will literally reduce the whole list of integers into a single value, which is going to be of type Int (or a supertype of Int, hence [B >: A]).

When you call foldLeft say on a List[Int] it will fold the whole list (imagine rolling a piece of paper) into a single value, but this value doesn't have to be even related to Int (hence [B]).

Here is an example:

def listWithSum(numbers: List[Int]) = numbers.foldLeft((List[Int](), 0)) { (resultingTuple, currentInteger) => (currentInteger :: resultingTuple._1, currentInteger + resultingTuple._2) }

This method takes a List[Int] and returns a Tuple2[List[Int], Int] or (List[Int] -> Int). It calculates the sum and returns a tuple with a list of integers and it's sum. By the way the list is returned backwards, because we used foldLeft instead of foldRight.

更多推荐

在Scala中foldLeft和reduceLeft之间的区别

本文发布于:2023-11-25 15:14:44,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:区别   Scala   foldLeft   reduceLeft

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!