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
0
votes
1 answer

select and withcolumn both are not working with foldleft

Trying to explode given columns from nested schema. I am trying to achieve this with fold-left on the dataframe. Here I have handled only two scenarios If column type is struct then I am trying to get by select clause If column type is array then…
Rao
  • 153
  • 12
0
votes
1 answer

Join multiple dataframes in scala

I have two variables. One is a Dataframe and other is a List[Dataframe]. I wish to perform a join on these. At the moment I am using the following appoach: def joinDfList(SingleDataFrame: DataFrame, DataFrameList: List[DataFrame], groupByCols:…
0
votes
1 answer

How to make accumulator an Empty Set of Int in Scala foldLeft?

I currently have a Set in Scala of size greater than 1 : val x = BitSet(19, 49) and val z = HashMap(49 -> HashSet(5, 6, 9, 13, 3, 8, 4), 19 -> Set(5, 9, 14)) of type Map[Int,Set[Int]] I am trying to construct a Set[Int] having all values associated…
Usr654789
  • 47
  • 6
0
votes
2 answers

SCALA: Fold method with conditions

I am still learning the basics of Scala, therefore I am asking for your understanding. Is it any possible way to use fold method to print only names beginning with "A" Object Scala { val names: List[String] = List("Adam", "Mick", "Ann"); def…
0
votes
1 answer

How Lambda function works in Haskell

I am new to Haskell, while working on small programs I found little confusions about functioning of lambda functions. lastThat :: (a -> Bool) -> a -> [a] -> a lastThat f = foldl (\x acc -> if f x then x else acc) Executing lastThat (>0) 100…
0
votes
3 answers

How to make this Scheme function not tail recursive?

I can't figure out how can I make this tail recursive Scheme function not tail recursive anymore. Anyone can help me? (define (foldrecl f x u) (if (null? x) u (foldrecl f (cdr x) (f (car x) u))))
Sandpoki
  • 35
  • 4
0
votes
1 answer

Can someone explain this foldl example to me in laymens terms?

I am trying to understand exactly how this function operates and cannot figure it out in my brain. Please can you show the step-by-step process of the code? I've been sitting here trying to figure this example out in my head using a calculator on my…
Leafyshark
  • 395
  • 2
  • 5
  • 11
0
votes
3 answers

Expression of type B => List[B] doesn’t conform to expected type List[Int] when reverse a list in Scala

Working on a function to reverse a list in Scala using the foldLeft function: def reverseWithFold(ls: List[Int]): List[Int] = ls.foldLeft(List[Int]())((c, _) => _:::c) it got compiling errors: Expression of type List[B_] => List[Int]…
injoy
  • 3,993
  • 10
  • 40
  • 69
0
votes
1 answer

Haskell function composition using foldl

I have defined the following function in haskell: step :: [Int] -> [Char] -> [Int] step stack str | str == "*" = remaining ++ [x*y] | str == "+" = remaining ++ [x+y] | str == "-" = remaining ++ [x-y] | str == "/" = remaining ++…
0
votes
2 answers

Scala - Scanleft, returning values within iterations without using it in a next turn

I have a list what I would like to do is def someRandomMethod(...): ... = { val list = List(1, 2, 3, 4, 5) if(list.isEmpty) return list list.differentScanLeft(list.head)((a, b) => { a * b }) } which returns List(1, 2, 6, 12, 20)…
cosmir17
  • 57
  • 7
0
votes
1 answer

"Quote" showing up in output while appending with fold-left in Scheme

I'm attempting to concatenate lists in scheme using fold-left and append (for usage in a bigger function). However, I keep getting outputs that look like this: => (0 quote (1) quote (2)) This is my code: (fold-left (lambda(a b) (append a b)) '(0)…
16jacobj
  • 443
  • 1
  • 4
  • 11
0
votes
2 answers

Scala: map+filter instead of foldLeft

Is it possible to substitute the foldLeft function through some combination of map and filter in Scala? For example on this task. Input is a list of triples (student name, course, grade): val grades = List(("Hans", "db", 2.3), ("Maria", "prog1",…
Frumda Grayforce
  • 119
  • 1
  • 13
0
votes
2 answers

using scala foldLeft to add values from unique data structure

I am trying to learn Scala through making a Calculator. In this calculator, I have two methods in particular that do the parsing and computing. I also have two case classes that handle data. The 'unique' data structure is just a composed of an…
BodyBingers
  • 65
  • 3
  • 9
0
votes
2 answers

Scala FoldLeft function

I have below sample data: Day,JD,Month,Year,PRCP(in),SNOW(in),TAVE (F),TMAX (F),TMIN (F) 1,335,12,1895,0,0,12,26,-2 2,336,12,1895,0,0,-3,11,-16 . . . Now I need to calculate hottest day having maximm TMAX, now I have calculated it with reduceBy,…
Harshit Kakkar
  • 117
  • 2
  • 12
0
votes
0 answers

Summation of a List Using Foldl

I'm new to Haskell and I'm trying to implement the sine approximation using MacLaurin's series using foldl on Haskell. I've already generated the list, and now I'm trying to get the summation using fold. This is my code. I know I'm doing something…
zuzu
  • 401
  • 2
  • 6
  • 17