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

How to merge a sequence of arrays in scala

I have a value of type Seq[Array[Int]] and I want to end up with a single Array[Int]. I thought foldLeft would work, but surprisingly it doesn't: scala> val arr1 = Array(1,2,3) arr1: Array[Int] = Array(1, 2, 3) scala> val arr2 =…
doub1ejack
  • 10,627
  • 20
  • 66
  • 125
2
votes
0 answers

Imperative vs. Functional data structure tradeoffs

Is there some trade-off between these two examples of the same thing? My bias is to always use a functional style, but if I'm running billions of these foldLefts, is creating a new FunctionalA for every step in every list less efficient than just…
kmh
  • 1,516
  • 17
  • 33
2
votes
4 answers

How to write an efficient groupBy-size filter in Scala, can be approximate

Given a List[Int] in Scala, I wish to get the Set[Int] of all Ints which appear at least thresh times. I can do this using groupBy or foldLeft, then filter. For example: val thresh = 3 val myList =…
Giora Simchoni
  • 3,487
  • 3
  • 34
  • 72
2
votes
3 answers

Scala: Value :: is not a member of Int

I have following Scala code sample and I want to know why I get an Error on foldLeft but not with foldRight? val xs = List(1,2,3) val ys = List(4,5,6) (xs foldLeft ys) (_::_) // Error: Value :: is not a member of Int (xs foldRight ys) (_::_) //…
Felix Junghans
  • 152
  • 1
  • 12
2
votes
2 answers

scala parameterized type on foldLeft

given the following signature for a parameterized method def double[A <: Byte](in:List[A]): List[A] = { //double the values of the list using foldLeft //for ex. something like: in.foldLeft(List[A]())((r,c) => (2*c) :: r).reverse //but it…
cogitate
  • 77
  • 7
2
votes
3 answers

Deciding between use of foldleft() or Seq.newBuilder in Scala

I have some code where I need to add elements to a sequence while iterating over another one. Which way is the 'preferred' or rather the better way of doing that in scala and why?: Way 1: val builder = Seq.newBuilder[String] for(i <-…
Core_Dumped
  • 4,577
  • 11
  • 47
  • 71
1
vote
1 answer

How to tell the Coq kernel that a parameter of a lambda function is a subterm of another variable in the scope?

I'm stuck while trying to define a recursive function in Coq over a particular type of n-ary tree. The n-ary tree type I'm using is defined as a mutually inductive type as follows: Inductive tree : Type := | leaf : tree | node : childs -> tree with…
1
vote
1 answer

How to use condition inside foldLeft in spark Scala?

`Error : Type mismatch. Required: (sql.DataFrame, String) => sql.DataFrame, found: (sql.DataFrame, String) => Any I am trying to traverse through all the columns in dataframe. so I have used foldLeft.Need to replace the data based on the following…
1
vote
1 answer

Spark Scala FoldLeft Performance slowness

Hi I am trying to do a scdtype2 update in dataframe having 280 columns. val newYRecs = stgDF.columns .foldLeft(joinedDF) {(tempDF,colName) => tempDF.withColumn("new_" + colName,…
mehere
  • 1,487
  • 5
  • 28
  • 50
1
vote
1 answer

How to add every element of a list at the end of every element of another list in scala?

I would like to add element of a list at the end of every element of another list. I have : val Cars_tmp :List[String] = List("Cars|10|Paris|5|Type|New|", "Cars|15|Paris|3|Type|New|") => Result : List[String] = List("Cars|10|Paris|5|Type|New|",…
fleur
  • 29
  • 5
1
vote
3 answers

How to fold on x elements of list per fold

So, let us say we have some list like as follows: [1; 2; 3; 4; 5; 6], and let us say that I want to fold on 2 elements per call of the function. So, I would apply the function on (1, 2), (3, 4), and (5, 6) in order. Here was my attempt at a function…
Gigi Bayte 2
  • 838
  • 1
  • 8
  • 20
1
vote
1 answer

Haskell fold left over infinite list not applying lazy evaluation

From my understanding, as Haskell uses lazy evaluation, which allows operations on for example infinite lists to be evaluated in a finite amount of time. As a test, I defined the following function X Boolean Y Int f(X,Y) = (Y == 3) OR X Hence,…
jdfauw
  • 647
  • 3
  • 11
  • 22
1
vote
3 answers

How to return every nth even number in a list with foldLeft in Scala?

For a university project I have to implement a function called takeNthEven which finds the nth even number in a list with the aid of foldLeft. For example: takeNthEven(SinglyLinkedIntList(0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15,18,5,3),…
hispaniccoder
  • 337
  • 2
  • 4
  • 12
1
vote
2 answers

Kadane algorithm in Scala explanation

I am interested in learning how to implement the Kadane (maximum subarray sum) algorithm in scala with foldLeft function. I run through this example on stack overflow, however I am not sure I understand what the algorithm does exactly. This is how…
1
vote
2 answers

Foldl with lambda expresion

Hi I'm trying to sum a list of tuples into a tuple with the foldl function, I tryed it with using as parameter a lambda expresion but it's giving out a wrong value here the code: data Point = Point {x,y :: Float} sumPoint :: [Point] ->…
Kevin
  • 145
  • 7