foldleft

Scala foldLeft while some conditions are true

限于喜欢 提交于 2021-02-04 17:38:05
问题 How to emulate following behavior in Scala? i.e. keep folding while some certain conditions on the accumulator are met. def foldLeftWhile[B](z: B, p: B => Boolean)(op: (B, A) => B): B For example scala> val seq = Seq(1, 2, 3, 4) seq: Seq[Int] = List(1, 2, 3, 4) scala> seq.foldLeftWhile(0, _ < 3) { (acc, e) => acc + e } res0: Int = 1 scala> seq.foldLeftWhile(0, _ < 7) { (acc, e) => acc + e } res1: Int = 6 UPDATES: Based on @Dima answer, I realized that my intention was a little bit side

Equivalent of Scala's foldLeft in Java 8

北城以北 提交于 2020-12-28 06:42:56
问题 What is the equivalent of of Scala's great foldLeft in Java 8? I was tempted to think it was reduce , but reduce has to return something of identical type to what it reduces on. Example: import java.util.List; public class Foo { // this method works pretty well public int sum(List<Integer> numbers) { return numbers.stream() .reduce(0, (acc, n) -> (acc + n)); } // this method makes the file not compile public String concatenate(List<Character> chars) { return chars.stream() .reduce(new

Equivalent of Scala's foldLeft in Java 8

江枫思渺然 提交于 2020-12-28 06:41:10
问题 What is the equivalent of of Scala's great foldLeft in Java 8? I was tempted to think it was reduce , but reduce has to return something of identical type to what it reduces on. Example: import java.util.List; public class Foo { // this method works pretty well public int sum(List<Integer> numbers) { return numbers.stream() .reduce(0, (acc, n) -> (acc + n)); } // this method makes the file not compile public String concatenate(List<Character> chars) { return chars.stream() .reduce(new

Equivalent of Scala's foldLeft in Java 8

旧时模样 提交于 2020-12-28 06:40:19
问题 What is the equivalent of of Scala's great foldLeft in Java 8? I was tempted to think it was reduce , but reduce has to return something of identical type to what it reduces on. Example: import java.util.List; public class Foo { // this method works pretty well public int sum(List<Integer> numbers) { return numbers.stream() .reduce(0, (acc, n) -> (acc + n)); } // this method makes the file not compile public String concatenate(List<Character> chars) { return chars.stream() .reduce(new

Equivalent of Scala's foldLeft in Java 8

我们两清 提交于 2020-12-28 06:39:53
问题 What is the equivalent of of Scala's great foldLeft in Java 8? I was tempted to think it was reduce , but reduce has to return something of identical type to what it reduces on. Example: import java.util.List; public class Foo { // this method works pretty well public int sum(List<Integer> numbers) { return numbers.stream() .reduce(0, (acc, n) -> (acc + n)); } // this method makes the file not compile public String concatenate(List<Character> chars) { return chars.stream() .reduce(new

Equivalent of Scala's foldLeft in Java 8

回眸只為那壹抹淺笑 提交于 2020-12-28 06:38:07
问题 What is the equivalent of of Scala's great foldLeft in Java 8? I was tempted to think it was reduce , but reduce has to return something of identical type to what it reduces on. Example: import java.util.List; public class Foo { // this method works pretty well public int sum(List<Integer> numbers) { return numbers.stream() .reduce(0, (acc, n) -> (acc + n)); } // this method makes the file not compile public String concatenate(List<Character> chars) { return chars.stream() .reduce(new