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

Scala: dynamically joining data frames

I have data split into multiple files. I want to load and join the files. I'd like to build a dynamic function that 1. will join n data files into a single data frame 2. given the input of file location and join column (e.g., pk) I think this can…
Jake
  • 4,322
  • 6
  • 39
  • 83
0
votes
2 answers

Erlang: Foldl function parameters swapped

Why is the accumulator AccIn not the left parameter for Fun ? http://erlang.org/doc/man/lists.html#foldl-3 foldl(Fun, Acc0, List) -> Acc1 Fun = fun((Elem :: T, AccIn) -> AccOut) Acc0 = Acc1 = AccIn = AccOut = term() List = [T] T = term() I ask…
sa___
  • 363
  • 2
  • 12
0
votes
1 answer

find keystrokes for On Screen Keyboard scala

I am trying to solve a recent interview question using Scala.. You have an on screen keyboard which is a grid of 6 rows , 5 columns each. With alphabets from A to Z and blank space are arranged in the grid row first. You can use this on screen…
0
votes
2 answers

Scala using foldLeft to insert terminator between list of strings

I wrote my own recursive definition of a foldLeft and I'd like to use it with this function joinTerminateLeft which takes a list of strings and a terminator and creates a new string with those strings all separated by the terminator. For example…
mocode9
  • 229
  • 3
  • 16
0
votes
1 answer

Scala - performance comparison of aggregate vs foldLeft

My understanding is that /: is same as foldLeft and also that aggregate is a faster version of foldLeft if the list is converted into parallel collection using 'par'. If I am correct, why does the following code show that :/ and foldLeft are faster…
Manu Chadha
  • 15,555
  • 19
  • 91
  • 184
0
votes
3 answers

Using racket to search a list for only one "1"

I've found out how to use foldr and lambda to find the number of 1's in a list. But how to use an if condition or any other method to verify if the list has only one 1. (define (exactlyone L) (foldr (lambda (elem count) (if (equal? elem 1) (+…
anush95
  • 71
  • 1
  • 1
  • 5
0
votes
1 answer

Merging List of Tuple List in scala

I have a list of tuple list which have overlapping elements. val tupLis:Seq[(List[(Integer,Char)],Int)] = null//data I am trying to merge overlapping elements in the tuple list. Here is a code that i am working on which uses foldleft to merger…
Balaram26
  • 1,349
  • 3
  • 15
  • 32
0
votes
1 answer

Is it possible to update a variable in foldLeft function based on a condition?

I am trying to write scala code which gives maximum sum from contiguous sub-array from the given array. For example, val arr= Array(-2, -3, 4, -1, -2, 1, 5, -3). In this array I need to get maximum contiguous sub-array sum i.e 4+(-1)+(-2)+(1)+5 = 7.…
Mahesh
  • 178
  • 3
  • 14
0
votes
1 answer

Scala foldLeft too many parameters

I have a list of tuples called item, each index in the list contains 2 x Doubles e.g. item = ((1.0, 2.0), (3.0, 4.0), (10.0, 100.0)) I want to perform a calculation on each index within the list item and I'm trying to do it with foldLeft. This is…
monster
  • 1,762
  • 3
  • 20
  • 38
-1
votes
2 answers

Scala : Unable to match the pattern

I am new to Scala and I am trying to execute = following code in Scala: scala> case class FoldExp(firstname : String, lname : String, age : Int, sex : String) defined class FoldExp scala> object Foo{ | def apply(firstname : String, lname : String,…
Anon
  • 320
  • 2
  • 14
-1
votes
1 answer

Scala Fold left finding penultimate element

Can you explain me why the first definition is wrong compared to the next? Why writing ((r._2,c))._1 will fetch me penultimate element? Please give me a trace how elements are inserted in (r,c) or their significance. Here is the code: scala> def…
Sanket_patil
  • 301
  • 1
  • 10
-2
votes
1 answer

Conditional termination from foldLeft iteration

Limit number of iteration in foldLeft. I would like to terminate iteration of foldLeft based on certain condition. Condition is evaluated from different variable not from list element. I tried using takeWhile for limiting the iteration; however…
-2
votes
1 answer

How can I make non-decreasing list of lists from one list? Without using recursion, using fold_left/fold_right. OCaml

This is my idea of the question but I can't correctly type fold_left method. Example: nonDecreasing[1;4;3;2;5;6] == [[1;4];[3];[2;5;6]] let nonDecreasing list = match list with | [] -> help(a, b, c) = b | h::[] -> 2 (*I don't know,…
aagi
  • 19
  • 3
1 2 3 4 5 6
7