Questions tagged [fold]

In functional programming, a fold, also known variously as reduction, accumulation, or catamorphism, is a type of higher-order function that recursively applies a transformation to a data structure, "collapsing" it to a summary value

In functional programming, a fold, also known variously as , accumulation, or catamorphism, is a type of higher-order function that recursively applies a transformation to a data structure, "collapsing" it to a summary value.

1159 questions
0
votes
1 answer

Implementing `elem` with foldLeft

I'm working on Learn You a Haskell. On the "fold" section, I need to implement elem (given an element, find out if it's in the list - True or False). def myElem(a: Char, as: List[Char]): Boolean = as match { case Nil => false case x::Nil =>…
Kevin Meredith
  • 41,036
  • 63
  • 209
  • 384
0
votes
1 answer

How to use fold/reduce to achieve this in scala ?

I've a class case class PlaceHolder(time: Long, a: Int, b: Int) I've a list of PlaceHolder objects and I want to create one object which contains the sum of all the values of field a and b. I don't care about time. I believe it can be done with…
Soumya Simanta
  • 11,523
  • 24
  • 106
  • 161
0
votes
3 answers

Accessing the Accumulator & Element in `foldl`

In Scala's foldLeft, I know how to access the accumulator and element values in Scala, but not Haskell. I could use foldLeft to find out, between 1 and 100, how many numbers have a remainder of 1 when divided by 3: scala> List.range(1,…
Kevin Meredith
  • 41,036
  • 63
  • 209
  • 384
0
votes
2 answers

Looking for an FP algorithm to compose objects from dot-separated strings

I am trying to solve a specific problem using functional programming. My guess is that a fold should do the job, but so far the solution has eluded me. Starting from a dot-separated string like "a.b.c" I want to build a Javascript object which in JS…
Marco Faustinelli
  • 3,734
  • 5
  • 30
  • 49
0
votes
1 answer

SML: Use high-order function foldr to reverse a list

I try using foldr to reverse a list like the following fun rev(l) = foldr (a b => b ++ [a]) [] l; but I got error : stdIn:4.25 Error: syntax error found at DARROW Can anyone help to point out the mistake? Is the code correct? Thank you.
0
votes
2 answers

Folding values list with binary operators list

I'm trying to write a reduce function of type [a -> a -> a] -> [a] -> a which would fold a list of n values with a list of n - 1 binary operators, as in the following example: reduce [(+), (*), (-)] [2, 3, 5, 7] Running this example should return…
killy971
  • 650
  • 6
  • 16
0
votes
0 answers

Why does the insert method never get called?

This code compiles but the Environment.insert is never called. I'm sure there is an envrionmentForm there. //isauth version of save def save(SystemId: Long) = IsAuthenticated { username => implicit request => User.findByEmail(username).map {…
mbrambley
  • 245
  • 3
  • 10
0
votes
3 answers

debugging for a geometric series in python

def fold2(op, term, a, next, b, base): if a > b: return base else: return op (term(a), fold2(op, term, next(a), next, b, base)) def geometric_series(a, r, n): return fold2(lambda x,y: x+y, lambda x: a*(r**(n-1)), 1,…
user3234828
  • 61
  • 3
  • 8
0
votes
2 answers

haskell error: map is applied to too many arguments

i keep getting this error and i cant work out why sumSquares a = map (^2) a . foldr (+) 0 Im adding up the squares of a list of numbers.
user3105607
  • 359
  • 3
  • 7
  • 20
0
votes
3 answers

Underscore.js - foldl with intersection

I have an arbitrary length array of arrays. I want to compute the intersection. I tried doing this in two ways that I thought were equivalent, but they produced different output. What is the difference between: var a = [[1,2,3,4,5], [3, 4,5,6,7],…
joews
  • 29,767
  • 10
  • 79
  • 91
0
votes
1 answer

Member function in sml with foldl

I found this member function on the Internet which uses foldl fun memeber3 (x,xs)= foldl (fn (y,b)=>b orelse x=y) false; but when I run it with an element and a list it does not work, instead of true or false it reacts like I am…
Alexis S.
  • 5
  • 5
0
votes
1 answer

Using foldr to simplify a function (SML / NJ)

I have this function fun exist p M = foldr( fn(x,y) => x orelse y ) false (map p M) I need to write it using only the foldr function, but can only call foldr once. I'm confused on how I should handle this. Do i need to revise my anon function?
user2796815
  • 465
  • 2
  • 11
  • 18
0
votes
1 answer

Very basic task using foldr

I just used a very simple example used in some lecture notes with my ghci: foldr (:) [] 1 2 expecting the result [1,2] However, I get an error. I get an error everytime when I try to use ++ or : as the function given to foldr. Apparently I am…
newnewbie
  • 993
  • 2
  • 11
  • 26
0
votes
2 answers

SML - Incrementing a value in a tuple during foldl that needs to be returned

I'm having a problem while trying to increment my value of x inside the inner foldl call. I make x equal to shiftValue that's passed in and attempt to increment it whenever I find a #" " or #"*" in the inner foldl call, but the value of x that gets…
Andrew_CS
  • 2,542
  • 1
  • 18
  • 38
0
votes
2 answers

boost-mpl, fold and placeholders, select class from vector

I am trying to learn C++ template metaprogramming. Given a boost::mpl::vector of classes I want to compute the index of that class where a static member variable has a certain value. I found a solution which seems to work. However, in order to…
jomulu
  • 1
  • 1