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
1
vote
1 answer

SortedMap manipulation using foldleft

I have a code that converts the following datatypes: from: SortedMap[Long, SortedMap[String, Double]] to: SortedMap[String, Array[Double]] Can anyone please explain how this code does the above manipulation? val convertedDataWindow =…
user3370773
  • 435
  • 6
  • 11
1
vote
1 answer

write reverse in scala, using foldleft

here's an implementation: def reverse[A](l: List[A]): List[A] = foldLeft(l, List[A]())((acc,h) => Cons(h,acc)) I don't understand what is inderstood by the compiler with (acc,h); initially, the f function meets (ListA,l), which are 2 lists, so is…
lolveley
  • 1,659
  • 2
  • 18
  • 34
1
vote
2 answers

Scala - Execute arbitrary number of Futures sequentially but dependently

I'm trying to figure out the neatest way to execute a series of Futures in sequence, where one Future's execution depends on the previous. I'm trying to do this for an arbitrary number of futures. User case: I have retrieved a number of Ids from my…
healsjnr
  • 405
  • 3
  • 10
1
vote
2 answers

Type mismatch error with foldLeft method in Scala

I have a method that uses foldLeft in Scala. def bitSetToByte(b:collection.BitSet, sh:Int=0) = ((0 /: b) {(acc, input) => acc + (1 << (input - sh))}).toByte The method has two parameters for the anonymous function, so I replaced it with _ by…
prosseek
  • 182,215
  • 215
  • 566
  • 871
0
votes
1 answer

ocaml fold_left all elements with even index on left and elements with uneven index on right

I have a problem with the following assignment: The function fold_left can be used to implement many operations that scan through a list from left to right. It takes three arguments: a function f, an initial accumulator, and a list. For each…
MAMR
  • 1
0
votes
2 answers

reduceLeft (or, foldLeft) vs reduceRight (or, foldRight) in scala

I am learning scala from coursera. In the reduceLeft and reduceRight description this comes along: and then on the very next slide the teacher says this code pattern is abstracted out as reduceLeft My questions: I think the pattern in the first…
figs_and_nuts
  • 4,870
  • 2
  • 31
  • 56
0
votes
2 answers

What does e:B, f:(B,A)=>B) : B

I am confused about what this means. I understand currying but I can't seem to read the code altogether. def foldLeft [A,B](xs:List[A], e:B, f:(B,A)=>B): B
0
votes
0 answers

scala spark foldLeft on map with array

I have a configuration in a form of a map: val config = Map[String, Array[String]] = Map("column1" -> Array("field1"), "column2" -> Array("field1","field2")) I want to use foldLeft to apply this to a dataframe and dynamically filter nested fields…
Kylo
  • 109
  • 8
0
votes
1 answer

The problem of foldl (with multi level foldl) in haskell

The foldr version was fast than the foldl version: the foldr version: cartProdN9 :: [[a]] -> [[a]] cartProdN9 xss = foldr h1 [[]] xss where h1 xs yss = foldr g [] xs where g x zss = foldr f zss yss where f xs yss = (x:xs):yss…
jiamo
  • 1,406
  • 1
  • 17
  • 29
0
votes
2 answers

Haskell Functions (map,foldr, foldl)

I am struggling to think of a way to utilize these functions for this beginner level coding class that I am taking to learn functional programming in Haskell. The functions I have to write are shown below, asum is supposed to turn a list of integers…
0
votes
2 answers

How to use a foldLeft using a regex

I am trying to use a foldLeft to replace user names with a hidden value and print them out private def removeNamesFromErrorMessage(errorMessage: String): Unit = { val userNames = List("mary", "john") val errorMessage = "users mary and john…
SimonL
  • 3
  • 1
0
votes
1 answer

Scala Argonaut folding across a list?

I'm very new to Argonaut (which I'm forced to use since the codebase is older, using an old version of scalaz, and we have no intentions to update it since we're rewriting this outdated code from scratch), and I'm trying to find a way to dump some…
Sebastian
  • 715
  • 6
  • 13
0
votes
1 answer

FoldR using FoldL on finite lists

I have read here that on finite lists is possible to write foldR in term of foldL in the following way (using coq): Fixpoint fold {A : Type} {B : Type} (f : A -> B -> B) (v : B) (l : list A) := match l with | nil => v | x :: xs => f x (fold f…
Marco Mantovani
  • 111
  • 2
  • 7
0
votes
0 answers

Prettier way in scala to foldLeft a column?

I have an SQL which I am trying to code in spark scala using dataframes SELECT country, Substr(substring_col, 1, 3) AS Code, CASE WHEN Substr(substring_col, 1, 9) = '238208700' THEN 'columnName1' WHEN…
Gladiator
  • 354
  • 3
  • 19
0
votes
1 answer

Foldleft in Racket

I have a question regarding trying to define a recursively defined foldl function in Racket. Here is my approach: (define foldl (lambda (z c xs) (match xs (empty z) ((make-pair x xs) (foldl c (c z x) xs))))) Unfortunately, when I…