Snippets tagged fold

  • Composing a list of functions

    Composition of functions in F# is easily achieved by using the >> operator. You can also chain an arbitary amount of functions (represented as a list or sequence) together by folding the list/seq with >>. [More formally: the set of endomorphisms 'a -> 'a forms a monoid with the binary, associative operator ">>" (or "<<") and the neutral element "id".]

    77 people like this

    Posted: 6 years ago by Novox

  • Extensions to the Fold function

    This snippet is helpfull in the following cases: 1) After a consolidation operation using the fold function we need to know how many elements have been processed. 2) A consolidation operation needs to use the index of each of the elements processed and we don't want to use the mapi function first. 3) A combination of the above. Since the following snippet just adds a wrapper to the existing Fold function we can repeat the approach for arrays and sequences (including the plinq ones)

    29 people like this

    Posted: 6 years ago by Horacio Nuñez

  • Chronological sequence window beginnings

    An abstraction of the following use case: Given a sequence of dates and max temperatures for each date, extract out the initial dates on which the temp is greater than a given threshold for n consecutive days. (Originally posted as an answer to this StackOverflow question: )

    12 people like this

    Posted: 6 years ago by ildjarn

  • Folding over prime factors

    Let's have some fun with higher order functions and instead of folding over a list, fold over the prime factors of a number. It can be optimized further by dividing out real primes instead of numbers of the form 6k+/-1, but it's not embarrassingly slow.

    2 people like this

    Posted: 5 years ago by Arjen Kopinga

  • Successive fold functions

    fold by applying a list of function to a list of arg, last one behaving as usual

    1 people like this

    Posted: 4 years ago by nicolas2

  • Array Shuffle

    Shuffling array using Seq.fold

    4 people like this

    Posted: 3 years ago by Karlkim Suwanmongkol

  • Convert booleans to bits using fold

    @Samuel - saw your post and thought one part was similar to bit shifting I was playing with yesterday, so rewrote the FromBooleans function using a fold instead of ref cells. Probably not as readable as your version though :)

    2 people like this

    Posted: 3 years ago by Chris Ballard

  • Parallel fold

    Idea from Guy L. Steele - Organizing Functional Code for Parallel Execution; or, foldl and foldr Considered Slightly Harmful -

    8 people like this

    Posted: 2 years ago by Tuomas Hietanen

  • Type-level Fold

    Generic Type-level Fold for Boolean Algebra

    0 people like this

    Posted: 1 years ago by Nick Palladinos

  • SIMD fold extension for Array

    A SIMD enhanced fold function that adds on to the Array type

    2 people like this

    Posted: 12 months ago by Jack Mott