Questions tagged [foldleft]

A common algorithm in functional languages that applies an operation to each member of a sequence, from left to right.

103 questions
35
votes
5 answers

Equivalent of Scala's foldLeft in Java 8

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…
GA1
  • 1,568
  • 2
  • 19
  • 30
16
votes
3 answers

Scala fold right and fold left

I am trying to learn functional programming and Scala, so I'm reading the "Functional Programming in Scala" by Chiusano and Bjarnason. I' m having trouble understanding what fold left and fold right methods do in case of a list. I've looked around…
jrsall92
  • 572
  • 1
  • 5
  • 19
16
votes
2 answers

Practical use of `foldl`

Today when I was working on one little script I used foldl instead of foldl'. I got stack overflow, so I imported Data.List (foldl') and was happy with this. And this is my default workflow with foldl. Just use foldl' when lazy version falls to…
d12frosted
  • 1,280
  • 12
  • 33
10
votes
6 answers

Scala foldLeft while some conditions are true

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] =…
ntviet18
  • 752
  • 1
  • 10
  • 26
6
votes
1 answer

Scala type error with Try[Int]

I have a problem with types that I don't understand. In the code below I have two methods half1 and half2 which are exactly the same except half1's return type is specified explicitly. Yet when I use the two methods in a foldLeft half causes a…
Christopher Helck
  • 1,130
  • 2
  • 8
  • 23
5
votes
2 answers

How does this foldl-based function work: "myreverse = foldl (flip (:)) []"?

I am learning haskell and I tried to write my own reverse function without using recursion. The solution is this function: myreverse = foldl (flip (:)) [] I'm trying to understand what happens during the evaluation of, say: myreverse [1..5] I…
basti12354
  • 2,490
  • 4
  • 24
  • 43
5
votes
2 answers

Scala: Is it possible to get partially applied function from leftfold?

I'm currently learning Scala, and I just wondered at fold-left. Since fold-left is curried, you should be able to get a partially applied function(PAF) with a first parameter as below. (0 /: List(1, 2, 3)) _ But actually, I've got an…
Tamajin
  • 155
  • 5
4
votes
1 answer

List in scala not getting updated

I am new to Scala Collections and currently I want to separate a given list of strings into a tuple of two lists (List[String], List[String]), which contains list of palindrome strings and rest of the of the input strings. For example, if input is…
PankajK
  • 187
  • 3
  • 14
4
votes
2 answers

How to find the position of a value with foldLeft from a List?

I have a List that contains, 1s and -1s. The goal I'm after is to find the position in the List when the total is -1. List[Int] = List(1, -1, 1, -1, 1, 1, -1, 1, -1, 1, -1, 1, 1, 1, 1, 1, -1, -1, -1, 1, -1, -1, 1, 1, -1, -1, 1, 1, -1, 1, 1, -1, 1,…
Native
  • 73
  • 1
  • 5
4
votes
2 answers

Understanding foldLeft with Map instead of List

I fould like to understand how foldLeft works for Maps. I do understand how it works if I have a List and call on it foldLeft with a zero-element and a function: val list1 = List(1,2,3) list1.foldLeft(0)((a,b) => a + b) Where I add the zero-element…
Make42
  • 12,236
  • 24
  • 79
  • 155
3
votes
1 answer

How can I count the number of times an element is greater than its successor?

I'm currently trying to solve the following exercise: Given a list of Ints, count the number of times, an element is greater than the element that comes after it. The exercise forces me not to use explicit recursions. Here are some example outputs…
J. G.
  • 57
  • 5
3
votes
2 answers

Defining foldl in terms of foldr

myFoldl :: (a -> b -> a) -> a -> [b] -> a myFoldl f z xs = foldr step id xs z where step x g a = g (f a x) I am currently reading a book on Haskell. And in it, it wrote its own version of the foldl function, but in terms of foldr. I do not…
3
votes
3 answers

Scala foldLeft with a List

I have the following code snippet: import scala.io.Source object test extends App { val lineIterator = Source.fromFile("test1.txt").getLines() val fileContent = lineIterator.foldLeft(List[String]())((list, currentLine) => { currentLine…
bajro
  • 1,199
  • 3
  • 20
  • 33
3
votes
1 answer

foldLeft on Map - Why does that work?

This is from a Coursera course, until now no one could help me. The following works, taken from a lecture. object polynomials { class Poly(terms0: Map[Int, Double]) { def this(bindings: (Int, Double)*) = this(bindings.toMap) val terms =…
Ely
  • 10,860
  • 4
  • 43
  • 64
3
votes
1 answer

How does foldl work?

Can anybody explain how foldl works? I understood that, for example, foldr (-) 0 [1,2,3] produces (1 - (2 - (3 - 0))), whereas foldl (-) 0 [1,2,3] produces (((0 - 1) - 2) - 3), but I have still some questions: 1st example (length of a list with…
1
2 3 4 5 6 7