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".]
83 people like thisPosted: 12 years ago by Novox
Three ways to split a list in half (but not necessarily in the middle). A forth version added that's very short and should be fast, as we only use List.fold. New champ found.
79 people like thisPosted: 11 years ago by Dmitri Pavlenkov
Seq.sumBy implemented in terms of Seq.fold
2 people like thisPosted: 11 years ago by ildjarn
Calculating the Earth Similarity Index of a planet. http://phl.upr.edu/projects/earth-similarity-index-esi
0 people like thisPosted: 10 years ago by Kit Eason
A random pex4fun puzzle to write a snippet to merge two strings.
2 people like thisPosted: 10 years ago by Joel Huang
Creates a map containing each item and its frequency as a key/value pair. Then sort by value in reverse order before printing each item.
1 people like thisPosted: 8 years ago by Bjørn Bæverfjord
Playing with simplified domain modules and event sourcing in F#
6 people like thisPosted: 8 years ago by mavnn
Fibonacci nth term with fold
6 people like thisPosted: 7 years ago by Andriy Tolstoy
A prototype of an associative fold operation when the function passed in arguments is a Semigroup(*) Operation. (*) Semigroup from Abstract Algebra.
2 people like thisPosted: 6 years ago by Fabio Galuppo
This snippet based on Binet's formula combined with fast power algorithm. Four multiplications run in parallel, thus processor with four cores recommended. Bitwise operators improve divisions and multiplications by pow of 2.
0 people like thisPosted: 3 years ago by Pavel Tatarintsev
Function, what calculates x^n by non-recursive basic exponentiation squaring algorithm. This method uses the bits of the exponent to determine computing powers. Generic parameter x suitable for any type what supports multiplication. I do not suppose existence of inverse operator like "/", thus parameter n must be a positive integer only. It is not difficult to define an extended variant for a type what supports division.
3 people like thisPosted: 2 years ago by Pavel Tatarintsev
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)
31 people like thisPosted: 11 years ago by Horacio Nuñez
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: http://stackoverflow.com/questions/5267055 )
13 people like thisPosted: 11 years ago by ildjarn
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 thisPosted: 11 years ago by Arjen Kopinga
fold by applying a list of function to a list of arg, last one behaving as usual
1 people like thisPosted: 10 years ago by nicolas2
Shuffling array using Seq.fold
6 people like thisPosted: 8 years ago by Karlkim Suwanmongkol
@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 thisPosted: 8 years ago by Chris Ballard
Idea from Guy L. Steele - Organizing Functional Code for Parallel Execution; or, foldl and foldr Considered Slightly Harmful - https://vimeo.com/6624203
9 people like thisPosted: 8 years ago by Tuomas Hietanen
Generic Type-level Fold for Boolean Algebra
0 people like thisPosted: 7 years ago by Nick Palladinos
A SIMD enhanced fold function that adds on to the Array type
2 people like thisPosted: 6 years ago by Jack Mott
This snippet is code that solves first Project Euler problem. It finds the sum of all the multiples of 3 or 5 below 1000. Please add other (more efficient, succinct or interesting) solutions to this snippet.
3 people like thisPosted: 3 years ago by Eugene Gavrin
Fold it!
1 people like thisPosted: 1 year ago by Evgeniy Andreev