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

Apply function (scanLeft) to partition to create new column in dataframe

I want to do a scanLeft type of operation on one column of a dataframe. Scanleft is not pararellizable, but in my case I only want to apply this function to the elements that are already in the same partition. Therefore operation can be exectued in…
Aneta
  • 31
  • 3
1
vote
1 answer

foldLeft on List of Tuples: strange unexpected results

I am having a list of tuples which elements I want to be summed: val t = Seq(1,2,3,4,5).map(el => (el,1)) t: Seq[(Int, Int)] = List((1,1), (2,1), (3,1), (4,1), (5,1)) t.foldLeft(0,0){ case ((a,b),(c,d)) => (a+b,c+d)} res3: (Int, Int) = (14,6) The…
Sergey Bushmanov
  • 23,310
  • 7
  • 53
  • 72
1
vote
2 answers

Haskell foldl and foldl1 produce "No instance for (Num [Char]) arising from the literal"

I think these two fold functions are the same, but only the second one works. The first one produce No instance for (Num [Char]) arising from the literal ‘12’ error. Why does the first one produce this error? foldl1 (\x y -> (show x) ++ (show y))…
MaxSha
  • 129
  • 1
  • 7
1
vote
1 answer

'diverging implicit expansion'-error on foldleft HList

I am trying to do a type-calculation to build some nested structure of collections but I get a 'diverging implicit expansion for type shapeless.ops.hlist.LeftFolder'-error when I try to use a Map-like collection with a List[T] as key or value type.…
user3508638
  • 195
  • 1
  • 7
1
vote
1 answer

Haskell: foldl Function applied as infix Function?

Is the function I give to foldl applied in an infix way? Example foldl (-) 0 [1,2,3] = 0-1-2-3 = -6 so more generally: foldl f x [a,b,c] is applied as: (((x `f` a) `f` b) `f` c) I know it's recursive, but can I think about it that way?
simplesystems
  • 839
  • 2
  • 14
  • 28
1
vote
3 answers

group by with foldleft scala

I have the following list in input: val listInput1 = List( "itemA,CATs,2,4", "itemA,CATS,3,1", "itemB,CATQ,4,5", "itemB,CATQ,4,6", "itemC,CARC,5,10") and I want to write a function in scala using groupBy and foldleft ( just…
scalacode
  • 1,096
  • 1
  • 16
  • 38
1
vote
3 answers

Scala - Remove all elements in a list/map of strings from a single String

Working on an internal website where the URL contains the source reference from other systems. This is a business requirement and cannot be changed. i.e.…
crowgers
  • 232
  • 3
  • 19
1
vote
3 answers

How to implement this foldl0 function without helper method?

I have following code: function foldr0(list, func) { if (list.length == 0) { return 0; } else { return func(list[0], foldr0(list.slice(1), func)); } } function foldl0(list, func) { if (list.length == 0) { return 0; } else { …
1
vote
1 answer

Understanding FoldLeft in ScalaZ

I'm reading some articles about ScalaZ and have a question about understanding it. In this article, we generalize the sum function, to abstract away the type to be summed. def sum[T](xs: List[T])(implicit m: Monoid[T]) = //... Where trait…
St.Antario
  • 26,175
  • 41
  • 130
  • 318
1
vote
2 answers

Haskell-- Trying to make my foldl function work

I have a function that converts [a, b, c, d, . . . ] to [(a,b), (c, d), . . .]. Now this works and here is the code for that: makeTuple :: [a] -> [(a,a)] makeTuple [] = [] makeTuple [a] = [] makeTuple (x:y:xs) = (x,y): (makeTuple xs) Now the next…
lou tej
  • 51
  • 2
  • 4
1
vote
2 answers

Elm: How to log inside a foldl

I have the following code: findPerson name peeps = List.foldl (\a b -> case b of Just _ -> b Nothing -> if a.name == name then Just a else Nothing ) Nothing peeps I would like to log the values of a…
Rich
  • 5,603
  • 9
  • 39
  • 61
1
vote
2 answers

Scala map with dependent variables

In scala I have a list of functions that return a value. The order in which the functions are executed are important since the argument of function n is the output of function n-1. This hints to use foldLeft, something like: val base: A val funcs:…
Tim
  • 2,000
  • 4
  • 27
  • 45
1
vote
2 answers

Better way for aggregation on list of case classes

I have list of case classes. Output requires aggregation on different parameters of case class. Looking for more optimized way to do it. Example: case class Students(city: String, college: String, group: String, name: String,…
jony
  • 119
  • 3
  • 13
1
vote
3 answers

Scala: How do I use foldLeft with a generic array?

I've this method: def isSorted[A](as: Array[A], ordered: (A, A) => Boolean): Boolean = { val sorted = for (it <- as.sliding(2)) yield { if (it.length == 2) ordered.apply(it(0), it(1)) else true } …
Abhijit Sarkar
  • 21,927
  • 20
  • 110
  • 219
1
vote
2 answers

folding a list of classes to a single, comma separated string (in Scala)

I have a need to take a list of case classes and convert them to a to a single, comma separated string (with no heading or tailing comma). case class State(name: String) def toLine(states: State*): String = { } so, toLine(State("one"),…
Ehud Kaldor
  • 753
  • 1
  • 7
  • 20