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
3
votes
0 answers

foldLeft with Double

I' am trying to use foldLeft to calculate the sum of all Double in a Vector. Using reduce works fine but I just can't figure out why it doesn't with foldLeft scala> val prices = Vector(20.0, 9.5,8.4,9.1) prices:…
ccheneson
  • 49,072
  • 8
  • 63
  • 68
2
votes
0 answers

How to perform unknown functions on list of lists in Haskell with foldl/foldr/recursion?

I want to write something using foldl/foldr or recursion, what performs tasks one after another in the previously performed "state" with the next Job's function. It needs to work with any type of given variable/function. The time is irrelevant now,…
2
votes
1 answer

Using foldl, write a function that converts a list of integers to an integer number?

I'm trying to write a function dec2int that converts a list of integers to an integer number. My only constraint is that I must use foldl. The type signature for the function is: dec2int :: [Int] -> Int The function should work in this way: Input: …
Dave B
  • 23
  • 2
2
votes
2 answers

How to convert tail recursive method to more Scala-like function?

In my code, I very often need to process a list by performing operations on an internal model. For each processed element, the model is returned and then a 'new' model is used for the next element of the list. Usually, I implement this by using a…
John Doe
  • 113
  • 1
  • 2
  • 11
2
votes
1 answer

Scala new Map column from integer and string columns

Problem Statement: I have a dataframe with four columns: service (String), show (String), country_1 (Integer), & country_2 (Integer). My objective is to produce a dataframe that consists of just two columns: service (String) & information…
2
votes
2 answers

Arguments not needed when using foldl in haskell?

What I don't get is how it is possible to use foldl in this way in haskell. I do not understand how the argument ( in this case list) is carried over implicitly: addAll :: [Int] -> Int addAll = foldl (+) 0 -- This is how I could write foldl to…
Nilo
  • 41
  • 4
2
votes
1 answer

How to sum number of Ints and Number of Floats within a List - Scala

I need to calculate the number of integers and floats i have in a Map which is like Map[String, List[(Int, String, Float)]] The data comes from reading a file - the data inside for example looks kinda like (however there is a few more Routes):…
CycleNew
  • 31
  • 4
2
votes
1 answer

How to get a size of a tree-like custom object

I am trying to figure out how to get the size (num of "levels") of a custom tree-like data structure defined this way: case class PrefixTreeImpl[K, +V](value: Option[V], prefixTree: Map[K, PrefixTree[K, V]]) extends PrefixTree[K, V] As you can see…
Burn_jacks
  • 45
  • 5
2
votes
2 answers

Concatenate List of Strings, adding a separator in between using foldl Haskell

I have a function that is meant to combine strings in a list, adding a separator in between each and outputting a single string using foldl. Here is what I have and some expected behavior of the function -- It isn't working and I'm unsure why. -- |…
Scott Myers
  • 226
  • 9
  • 30
2
votes
2 answers

Foldl on a list of functions in Haskell

I'm trying to write a function, pipe that takes a list of mathematical functions where pipe [f1,...,fn] x should return f1(f2(...(fn x))) I've set it up such that: pipe :: [(a -> a)] -> (a -> a) pipe fs = foldLeft f base fs where f a x = …
Scott Myers
  • 226
  • 9
  • 30
2
votes
2 answers

foldLeft sum values with BigDecimal.ZERO start value

I have requirement where I want sum values of all employee salaries in list employeeList.foldLeft(java.math.BigDecimal.ZERO) { (accSal,emp) => accSal + getSalary(emp,designation,yearsOfExp) } Here for each employee I want to call a function…
mprad
  • 71
  • 1
  • 5
2
votes
1 answer

How to convert a fold left operator ":/" into the foldLeft function in scala?

The following code sums the values of the two maps into a single map. val merged = (map1 /: map2) { case (map, (k,v)) => map + ( k -> (v + map.getOrElse(k, 0)) ) } However I am unsuccessful in converting it using the foldLeft()()…
Dev A
  • 171
  • 2
  • 8
2
votes
4 answers

Implementing Haskell's `take` function using `foldl`

Implementing Haskell's take and drop functions using foldl. Any suggestions on how to implement take and drop functions using foldl ?? take x ls = foldl ??? drop x ls = foldl ??? i've tried these but it's showing errors: myFunc :: Int -> [a] ->…
amir ahmed
  • 21
  • 5
2
votes
2 answers

Spark Scala FoldLeft resulting in StackOverflow when run in the cluster

Im using the following code in order to re-shape a dataframe using its rows for this reshaping. The dataframe contains the date of when a product changes its ID, but in order to join it with a huge other dataframe containing the transactions, I need…
I want badges
  • 6,155
  • 5
  • 23
  • 38
2
votes
1 answer

How to break early when foldLeft over simple List?

This is a simplified version of my problem, I want to stop the fold return the value after the if condition is met in the fold where a.size == 7. class test1 { def test(a : List[Int]): Int = { val list = a.foldLeft(if(a.size == 7) 1000 else…
Gioan Nguyen
  • 37
  • 10