Snippets in category Meta-Programming
Using Active patterns
Shows how to use an active pattern that will try to determine the format of an input value.
10 people like this
Posted: 1 years ago by Robert PickeringUsing Active patterns
Show's how to create a parametrized active pattern that will match based on a regular expression.
15 people like this
Posted: 1 years ago by Robert PickeringCompiling quotations
Demonstrates how to compose code at run time using F# quotations (both typed and untyped) and how to compile and run the quotation using F# PowerPack API.
40 people like this
Posted: 1 years ago by Tomas PetricekTraverse quotation
Shows how to use the 'ExprShape' module to recursively traverse an entire quotation and how to write quotation transformations. As an example, the snippet replaces all numeric constants in the quotation and then runs the transformed code.
13 people like this
Posted: 1 years ago by Tomas PetricekCreate generic IEnumerable from non-generic
Depends on Castle Dynamic Proxy 2. Returns an IEnumerable<T> from a System.Type and a System.Collections.IEnumerable. It is a hack I wrote simply because I couldn't find anything in the framework to do this.
19 people like this
Posted: 1 years ago by Dan FinchConvert a obj list to a typed list without generics
This is used for building things with reflection at runtime. As ConstructorInfo arguments require typed collections, it is necessary when parsing to reflected records to first build up contents and then afterward convert the collected obj[] to a 'a[]. This is finally cast back to obj so it can be used as a ConstructorInfo argument.
18 people like this
Posted: 1 years ago by Rick MinerichSupercompilation using quotations
This code sample shows how to create a function that raises number to a given power using supercompilation with quotations. For any given power, the function returns quoted expression for calculating the power using explicit multiplications, which is then evaluated.
16 people like this
Posted: 1 years ago by Dmitry SoshnikovActive 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 PetricekRegular expression active pattern
Active pattern returning list of captured groups.
7 people like this
Posted: 1 years ago by Daniel RobinsonFind 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 PetricekDisposable computation builder
Computation builder that provides easy way of constructing IDisposable objects. It supports elegant composition of disposable objects using 'do!' which can be used for example when working with 'IObservable' type.
27 people like this
Posted: 1 years ago by Tomas PetricekUgly hack to call F# functions as static methods
Ugly hack to call F# functions as static methods
3 people like this
Posted: 1 years ago by fholmDynamic operator using Dynamic Language Runtime
The snippet shows a simple implementation of the dynamic operator (?) that uses Dynamic Language Runtime and the C# implementation of dynamic operations. The snippet shows how to invoke instance methods with single argument.
25 people like this
Posted: 1 years ago by Tomas PetricekDynamic operator using Reflection
Demonstrates how to implement the dynamic operator (?) using .NET Reflection. The implementation supports calling constructors, propreties and methods using simple overload resolution (based on parameter count). It handles instance as well as static members.
41 people like this
Posted: 1 years ago by Tomas PetricekEnumerator computation builder
The snippet defines computation builder for working with IEnumerator. The bind operation (let!) reads next element from the enumerator, so the computation can be used for expressing things that cannot be written just using seq.
25 people like this
Posted: 1 years ago by Tomas PetricekHosting the DLR with IronPython or Ruby
Definition of the dynamic resolution operators for hosting the DLR. You can either use Ruby or Python, as the module isn't language specific.
3 people like this
Posted: 1 years ago by Rainer SchusterImperative computation builder
Defines an F# computation builder for encoding imperative computations. The 'return' construct returns immediately and terminates the rest of the computation. It is also possible to return value from a 'for' or 'while' loop.
6 people like this
Posted: 1 years ago by Tomas PetricekUse of Partial-Case with Parameters
Depicts use of one kind of active pattern matching; partial-case with parameters.
5 people like this
Posted: 1 years ago by Joel HuangSolution to the problem mentioned at : http://professor-fish.blogspot.com/2011/01/tiny-bit-of-denotational-semantics.html
Solution to the problem mentioned at : http://professor-fish.blogspot.com/2011/01/tiny-bit-of-denotational-semantics.html
1 people like this
Posted: 1 years ago by Ankur DhamaComputation Builder for Cartesian Products
Sample framework for computing Cartesian products using a computation builder.
7 people like this
Posted: 1 years ago by TechNeilogy
Active pattern for comparison
An active pattern for comparing two objects of the same type and implementing IComparable. Modeled on Standard ML comparison.
4 people like this
Posted: 11 months ago by petebuWimbledon 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 EasonLazyBuilder
I made LazyBuilder. The synthesis of the lazy function is troublesome. When the call of the Force() method increases, it is ugly. This solves the problem.
8 people like this
Posted: 10 months ago by zeclTiny IO Monad
Haskell-style IO in F#.
6 people like this
Posted: 10 months ago by igetaPermutation and Combination
Permutation and Combination using ListBuilder.
8 people like this
Posted: 9 months ago by zeclClumsy LoopBuilder
Clumsy LoopBuilder.It's mischievous trick.
4 people like this
Posted: 8 months ago by zeclDelimited Continuation Monad
Oleg's delimited continuation monad [1] and creating an external iterator from an internal iterator using it. [1] http://okmij.org/ftp/continuations/implementations.html#genuine-shift
3 people like this
Posted: 8 months ago by einblickerGet the F# module or type of the calling code.
This code snippet enables the retrieval of the entity (Module or F# Type) in which the calling code is executing. This can be used to find the module or type name.
2 people like this
Posted: 8 months ago by Huw SimpsonClojure-like lambda function syntax
This snippet implements a Clojure-like lambda function syntax using Code Quotations. However, this implementation is slow and, moreover, not type-safe. So I'm looking for a way to solve the issues.
4 people like this
Posted: 7 months ago by einblickerConvert the number of indentation spaces in a sourcecode.
This small script converts continuous 4 spaces to 2 spaces by using Regex. It gets the source code from a clip board text and save it to clip board. Since this program doesn't parse the source code, conversion is not perfect. But writing script which interacts with clipboard is so easy, you can automate your trivial coding work with a small effort. Using F#interactive with Clipboard is my favorite way of meta-programming.
1 people like this
Posted: 6 months ago by nagat01Non-deterministic computation builder
Computation builder for writing non-deterministic computations.
7 people like this
Posted: 6 months ago by Tomas PetricekActive pattern to extract HttpStatusCode from WebException
A simple active pattern that allows you to match WebExceptions by HttpStatusCode. I find it useful for quick scripts where I only need WebClient and don't have to get involved with WebRequest etc.
5 people like this
Posted: 5 months ago by Leaf GarlandTransform expressions into Excel formulae
Sometimes it is extremely useful to check some calculations with Excel. The snippet shows how F# expressions can be transformed into Excel formulae. The data is exported together with the formulae, e.g. a, b and sum function as input sets A1's value to a, B1's to b and C1's formula to "=$A$1+$B$1"
14 people like this
Posted: 4 months ago by Natallie BaikevichStackoverflow API script
A script that consumes the Stackoverflow API in a dynamic fashion
5 people like this
Posted: 3 months ago by Mauricio SchefferFactoring out FSharpChart
Using FSharpChart control. Just call Charting.Plot() to chart series or collections of series. No need to create forms, etc.
2 people like this
Posted: 2 months ago by Boris Koganreflection
reflection, not exactly efficient but good canvas to start exploring
5 people like this
Posted: 1 months ago by David KleinExpanding quotations
The snippet implements a function "expand" that takes a quotation and performs two operations. It replaces all calls to methods marked with ReflectedDefinition with the body of the method and it simplifies all expressions that can be reduced in call-by-name style (let binding & application of lambda).
1 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 PetricekSimple builder example: Nullable
Simple Computational expressions / monad / builder -example, using .NET Nullable as demo.
3 people like this
Posted: 28 days ago by Tuomas Hietanen