Snippets in category Functions
Projecting lists
Three functions showing how to implement projection for functional lists. First version uses naive recursion and the second one is tail-recursive using the accumulator parameter. The third version extends this with continuation passing.
43 people like this
Posted: 1 years ago by Tomas PetricekCurrying
Currying is about fixing arguments of functions from left to right. It's useful to configurate code and embed parameters that usually serve to define the context of a function execution (i.e. a database connection object). Symbolic functions can be used to reorder arguments if needed.
14 people like this
Posted: 1 years ago by Antonio CisterninoComposing 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".]
59 people like this
Posted: 1 years ago by NovoxA beautiful fixed-point finding function
We start with an initial value and then applying f repeatedly, until the value does not change anymore.
164 people like this
Posted: 1 years ago by Nick PalladinosCreating objects with events
This snippet shows how to create objects with events in F#. It shows both simple event (to be used from F#) and a .NET compatible event with specific delegate type.
53 people like this
Posted: 1 years ago by Tomas PetricekWorking with paths
Concatenating paths shouldn't be done just using string concatenation, because the directory separator may differ on various platforms. This snippet shows a simple custom operator for working with paths.
9 people like this
Posted: 1 years ago by Tomas PetricekMemoization and Tail Recursive Function
Hi, I expressed Memoization and Memoization Tail Recursive on the functions. I hope something useful.
12 people like this
Posted: 1 years ago by zeclinline pow
LanguagePrimitives help create inline functions
23 people like this
Posted: 1 years ago by Dmitri PavlenkovTree searching using Tail recursion with continuation
Sample code which demonstrate tree searching using : Tail recursion with continuation
9 people like this
Posted: 1 years ago by Ankur DhamaInline factorial samples with fix point
While reading Tomas Petricek's master thesis, came across FIX operator implementation. Decided to experiment with various implementations of factorial.
10 people like this
Posted: 1 years ago by Dmitri Pavlenkov
Find verbose .NET types using Reflection
Searches all (currently loaded) types using Reflection to find the types with longest and shortest names of members. Uses average length of all type member names as a metric.
8 people like this
Posted: 1 years ago by Tomas PetricekCurry / Uncurry
Helpers to convert functions that take a 2-tuple to curried functions and vice versa. Very helpfull for the "Zip"-functor together with operators - see example
30 people like this
Posted: 1 years ago by Carsten Königsum the nodes in a (not-binary) tree using continuations
you can easily find how to use continuations to iterate over a binary tree but what if the count of children for each node is not known at design time? It's not so obvious how to do this in order to get a tail-recursive method. This short snippet shows how to do this to sum the values of every leaf. The second part demonstrates a general approach for other operations than addition.
21 people like this
Posted: 1 years ago by Carsten KönigFun with polynoms and inline
for all those wanting to see the (rather unknown) statical interference of type-parameters (in contrast to generic type parameters) in action. I demonstrated this by having som e fun with basic algebra and polynoms
0 people like this
Posted: 1 years ago by Carsten KönigContinuation Monad with Call/CC
This is an implementation of the Continuation monad using a type, taking an exception handler, and allowing for Call/CC. This specific implementation is mostly Matt Podwysocki's. I have a similar implementation using a purely functional, exception-handler-less version in FSharp.Monad. Until now, I haven't been able to resolve the callCC operator.
3 people like this
Posted: 9 months ago by Ryan RileyCall/CC for Async
An implementation of call-with-current-continuation for Async.
2 people like this
Posted: 8 months ago by Ryan RileyMake a chain of functions
Function composition can be done by using >> operator. The snippet at http://fssnip.net/S is a wonderful sample. But that version generates a function which is not easy when you want to debug. This version is to use pipeline (|>) operator.
26 people like this
Posted: 5 months ago by Tao LiuRemove duplicate list elements
Remove duplicate elements of a list. Returns list with first instance of duplicate elements, without modification of order except subsequent duplicate elements are omitted.
1 people like this
Posted: 2 months ago by visProcessEnggSierpinski triangle, WPF
Draws a Sierpinski triangle using WPF
8 people like this
Posted: 2 months ago by Mathias Brandewinder