Snippets in category Functional Types
Filtering lists
Two functions showing how to filter functional lists using the specified predicate. First version uses naive recursion and the second one is tail-recursive using the accumulator parameter.
39 people like this
Posted: 1 years ago by Tomas PetricekProjecting 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 PetricekActive pattern for let binding inside patterns
The Let active pattern demonstrated by this snippet can be used to assign values to symbols in pattern matching. This is useful for writing complex pattern matching using match as we can handle multiple cases using a single clause.
49 people like this
Posted: 1 years ago by Tomas PetricekFind 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 PetricekFunctional wrappers for TryParse APIs
Exemplary convenience wrappers for some of the System.<Typename>.TryParse APIs, using the combined power of F#' return value deconstruction mechanism via pattern matching, active patterns and option types instead of "out/ref" parameters
15 people like this
Posted: 1 years ago by NovoxRe-creating arithmetic with DU
You never know when you might need this.
7 people like this
Posted: 1 years ago by Dmitri PavlenkovSimple Object orientation using Record types
Simple Object orientation using Record types. No complex - private public internal virtual abstract object oriented programming :)
5 people like this
Posted: 1 years ago by Ankur DhamaJaro-Winkler in F#
Jaro-Winkler is a fast and effective name matching algorithm. For more info see "A Comparison of String Distance Metrics for Name-Matching Tasks" http://www.isi.edu/info-agents/workshops/ijcai03/papers/Cohen-p.pdf or the Wikipedia article http://en.wikipedia.org/wiki/Jaro%E2%80%93Winkler_distance
21 people like this
Posted: 1 years ago by Rick MinerichCaching the function object created
Caching the function object created
13 people like this
Posted: 1 years ago by fholmUnion constructors can be used as functions
Union constructors can be used as functions
13 people like this
Posted: 1 years ago by fholmCreate Disposable
This pattern is helpful when you want to do something temporarily and then restore some state. This was inspired by the System.Disposable.Disposable() class defined in System.Core.dll distributed by the Reactive Extensions for .NET (Rx) library.
26 people like this
Posted: 1 years ago by Cesar MendozaSplit a list
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.
48 people like this
Posted: 1 years ago by Dmitri PavlenkovMember constraints on records
Demonstrates explicit member syntax as applied to record fields
19 people like this
Posted: 1 years ago by Tim Robinsonsum 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önigType-a-head search tree for strings
This is a search tree for strings I've built for work to back fast type-a-head for AJAX forms, it could be made million times more space efficient but there was no real need for it so.
3 people like this
Posted: 1 years ago by fholmIRC Jokes
Simple snippet that demonstrates recursively defined discriminated unions, the Y combinator (for encoding recursive functions) and recursive processing of tree-like structures
5 people like this
Posted: 1 years ago by Daniel Jackson
Tree using sequences
Tree implementation using sequences.
2 people like this
Posted: 1 years ago by Ankur DhamaRed-Black-Trees with insert
Found an very good article on RS-Trees in Haskell (see: http://www.eecs.usma.edu/webs/people/okasaki/jfp99.ps) It heavyly uses pattern recognition to translate those pesky balance-rules into short code. Bellowe is the simple rewrite of the haskell-implementation in F# - enjoy
4 people like this
Posted: 1 years ago by Carsten KönigWeighted Quick-Union with Path Compression
Implementation of Mutable Weighted Quick-Union with Path Compression in F#
2 people like this
Posted: 11 months ago by Rick MinerichImproved map declaration
Ruby like map declaration syntax
6 people like this
Posted: 10 months ago by Saagar AhluwaliamemoizeBy
Sometimes you might wish to memoize a function whose input doesn't have the equality and comparison constraints, or maybe the comparison of your given type is just too slow for what you need. To fix this, you simply provide a function which converts the input into something more fitting as an extra parameter.
7 people like this
Posted: 10 months ago by Rick MinerichWimbledon special!
Tennis scoring system (at the game level). Includes some pattern-matching examples including 'when' guards.
4 people like this
Posted: 10 months ago by Kit EasonSeq.reduceBallanced function
The function has the same type as Seq.reduce. Instead of reducing elements from the left to the right, it splits the input into two halves, reduces each half separately and then aggregates the results using the given function. This means that the values are aggregated into a ballanced tree, which can save stack space.
2 people like this
Posted: 9 months ago by Tomas PetricektoString and fromString for discriminated unions
General toString and fromString for discriminated unions using FSharp.Reflection
16 people like this
Posted: 5 months ago by Jonas AvelinMake 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 LiuUnion Editor for WinForms PropertyGrid
A quick-and-dirty editor for discriminated unions in a property grid. I use this for editing trees of F# records and unions. Note that any records must have a "Default" static property which returns a default instance.
3 people like this
Posted: 5 months ago by Rick MinerichDSL for financial contracts
Simple domain-specific language (DSL) for describing financial contracts in F#. A contract is represented using a discriminated union. Evaluating a contract gives the orders that may happen at a given date.
6 people like this
Posted: 5 months ago by Tomas PetricekSimple sql command helper
Minimalist assistant to read data / execute database command.
4 people like this
Posted: 3 months ago by S. KasperovichHTML File Type
Discriminated unions to represent a HTML file.(not completely)
2 people like this
Posted: 1 months ago by Gab_kmTesco in 70 lines of code
Domain model for the Tesco checkout implemented in F# using discriminated unions (in 20 lines of code) and console-based user interface for scanning products and calculating the total price.
3 people like this
Posted: 1 months ago by Tomas PetricekFinalizing Tesco purchase
The sample shows two different reprezentations of Tesco checkout. The first one stores scanned items - as a list of either purchase or cancel items - and the second stores final bill with product and total quantity. The snippet implements transformation that corresponds to finalizing the purchase.
3 people like this
Posted: 1 months ago by Tomas PetricekTraits, Mixins and Aspect-Oriented Programming in F#
A compositional type system built using generics and monads in F#. It is only a very limited, _toy_ project exploring traits, mixins and aspect-oriented programming.
11 people like this
Posted: 5 days ago by Zach Bray