This is a simple implementation of a monadic transaction builder for Clojure-style atoms. Based on original code by Nick Palladinos.
15 people like thisPosted: 12 years ago by Eirik Tsarpalis
Given an ordered set S and an arbitrary set T, any function T -> S induces a natural ordering on T. We use this idea and the infrastructure provided by Tagged.Set to quickly construct sets over arbitrary types with custom comparison rules.
4 people like thisPosted: 12 years ago by Eirik Tsarpalis
When passing objects as arguments to F# functions, it is almost certain that a type annotation will be required. This may generate long and noisy code at times, but thankfully we can use active patterns to mitigate this annoyance.
14 people like thisPosted: 12 years ago by Eirik Tsarpalis
A bit of syntax eye candy for your string building code.
10 people like thisPosted: 12 years ago by Eirik Tsarpalis
The following is a simplistic implementation of global events, which is the precise dual of the dependency injection implementation found in http://fssnip.net/dg. This is just a proof of concept, unwise to use in real life.
0 people like thisPosted: 12 years ago by Eirik Tsarpalis
A pair of extension methods for SerializationInfo that make custom ISerializable implementations much more straightforward to write.
6 people like thisPosted: 12 years ago by Eirik Tsarpalis
This is an implementation of the Async.Choice combinator that composes a sequence of workflows into one whose result is that of the first workflow that returns a *valid* result. You can think of it as a nondeterministic version of (Seq.tryPick id). This implementation draws ideas from Tomas Petricek's choice implementation in http://fssnip.net/6D. UPDATE (11/2015): Following the discussion in https://fslang.uservoice.com/forums/245727-f-language/suggestions/10575069-add-async-choice-to-fsharp-core I have updated the snippet.
3 people like thisPosted: 12 years ago by Eirik Tsarpalis
An implementation of the inverse of Expr.Cast<_>, with erasure happening at reflection level.
3 people like thisPosted: 12 years ago by Eirik Tsarpalis
When interfacing .NET libraries that potentially yield null values, a lot of boilerplate code is required to ensure no NullReferenceExceptions are raised. This is an attempt to avoid such repetition with computation expressions.
5 people like thisPosted: 11 years ago by Eirik Tsarpalis
A small snippet that groups sequences of arbitrary unions by branch type.
0 people like thisPosted: 11 years ago by Eirik Tsarpalis
A simple implementation that protects the nested workflow from external cancellation. The external cancellation token is passed as an argument for cooperative cancellation. Kudos to Gian Ntzik for this one.
3 people like thisPosted: 11 years ago by Eirik Tsarpalis
Implemetation picked up from http://msdn.microsoft.com/en-us/library/system.runtime.serialization.objectmanager.aspx
3 people like thisPosted: 11 years ago by Eirik Tsarpalis
A proof of concept on how one could build an argument parsing scheme simply by declaring a discriminated union.
11 people like thisPosted: 11 years ago by Eirik Tsarpalis
A few active pattern combinators useful when pattern matching on lists is necessary.
2 people like thisPosted: 11 years ago by Eirik Tsarpalis
Asynchronously perform a computation until it completes or timeout is reached.
4 people like thisPosted: 11 years ago by Eirik Tsarpalis
A combinator that enables real imperative code in F#. Use with caution!
5 people like thisPosted: 11 years ago by Eirik Tsarpalis
This snippet is a direct adaptation of Nick Palladinos' Session Types found in http://fssnip.net/j5. The implementation eschews the need for explicit declaration of duality objects by encoding all relevant information in the session signature itself.
3 people like thisPosted: 11 years ago by Eirik Tsarpalis
A simple trick for defining positive infinity semantics.
3 people like thisPosted: 11 years ago by Eirik Tsarpalis
.Net exceptions carry with them lots of useful metadata that are not directly user modifiable. In certain applications, such as distributed or symbolic execution it might be reasonable to need to modify such metadata. The following pattern is a proposal on effectively decoupling an exception from its contextual metadata, as well as a way of defining exception hierarchies while avoiding all the ISerializable boilerplate.
1 people like thisPosted: 10 years ago by Eirik Tsarpalis
Knocked together a bare-minimum TCP wrapper for MailboxProcessor. Not intended for production.
7 people like thisPosted: 10 years ago by Eirik Tsarpalis
Demonstrates a possible encoding for GADTs in F#. It is type safe, uses no reflection and pattern matches can be declared outside of the definition itself. See also http://lambda-the-ultimate.org/node/1134
4 people like thisPosted: 10 years ago by Eirik Tsarpalis
Suggestion for a core library function.
2 people like thisPosted: 10 years ago by Eirik Tsarpalis
See also https://fslang.uservoice.com/forums/245727-f-language/suggestions/6536829-implement-ocaml-s-new-match-exception-syntax
3 people like thisPosted: 9 years ago by Eirik Tsarpalis
c.f. http://fssnip.net/qR In this example different syntactic structures result in different types.
0 people like thisPosted: 9 years ago by Eirik Tsarpalis
An alternative Async.RunSynchronously implementation that avoids the performance bug as recorded in https://github.com/Microsoft/visualfsharp/issues/581
2 people like thisPosted: 9 years ago by Eirik Tsarpalis
An example that showcases why AsyncBuilder.Bind() overloads for task inputs is unfit for F#
3 people like thisPosted: 8 years ago by Eirik Tsarpalis
Provides a better implementation of Async.AwaitTask where the actual exception of a failing task is passed to the async mechanism.
12 people like thisPosted: 7 years ago by Eirik Tsarpalis
TypeShape-driven FsCheck random generator generator with support for arbitrary POCOs
3 people like thisPosted: 7 years ago by Eirik Tsarpalis
Staged generic equality comparer using TypeShape.
1 people like thisPosted: 7 years ago by Eirik Tsarpalis
A fast generic Json serializer for F# types using TypeShape and FParsec
3 people like thisPosted: 7 years ago by Eirik Tsarpalis
A ksprintf implementation that optionally formats the string. Naive microbenchmarks are provided to demonstrate potential performance gains. See also https://github.com/eiriktsarpalis/TypeShape/blob/d782bcd7fd32b1899c1c95ba933da0ae5e0bb4eb/samples/ksprintf.fsx
1 people like thisPosted: 7 years ago by Eirik Tsarpalis
TypeShape-driven structural projection and isomorphism generation
0 people like thisPosted: 7 years ago by Eirik Tsarpalis
Improved version of http://fssnip.net/pm/title/Extending-units-of-measure-to-arbitrary-types that adopts the "type class" approach and sheds requirement for passing the witness type when tag/untagging UoM values.
3 people like thisPosted: 7 years ago by Eirik Tsarpalis
Lightweight syntax for creating tasks using computation expressions
8 people like thisPosted: 7 years ago by Eirik Tsarpalis
Staged Generic Programming with support for recursive types PoC
0 people like thisPosted: 7 years ago by Eirik Tsarpalis
A simple continuation monad that encodes extensible and type-safe effects.
4 people like thisPosted: 6 years ago by Eirik Tsarpalis
An approach to using annotated types in F# using overloaded methods.
3 people like thisPosted: 6 years ago by Eirik Tsarpalis
A simple function that sort of emulates the holes feature of Idris for F#
15 people like thisPosted: 6 years ago by Eirik Tsarpalis
Adaptation of https://bitbucket.org/rojepp/reflenses/overview using TypeShape
6 people like thisPosted: 5 years ago by Eirik Tsarpalis
Derived from http://okmij.org/ftp/tagless-final/JFP.pdf
1 people like thisPosted: 5 years ago by Eirik Tsarpalis
Uses tagless-final approach for defining abstracted, general purpose computation expression builders.
2 people like thisPosted: 5 years ago by Eirik Tsarpalis
Example that combines generic programming using tagless-final and typeshape for driving folding of arbitrary types.
1 people like thisPosted: 5 years ago by Eirik Tsarpalis
Uses Leibniz equality to generalize an example presented in https://skillsmatter.com/skillscasts/14430-f-sharpunctional-londoners-september
3 people like thisPosted: 5 years ago by Eirik Tsarpalis
Showcasing fssnip's support for the latest F# and .NET Core
5 people like thisPosted: 4 years ago by Eirik Tsarpalis
Uses SRTPs to define a general purpose adapter for awaiting arbitrary C# awaitables in F# async workflows.
3 people like thisPosted: 4 years ago by Eirik Tsarpalis
Any type signature has the form of a curried chain T0 -> T1 -> .... -> Tn, where Tn is not a function type. The codomain of a type is precisely Tn. This is a simple implementation that uses reflection to determine the codomain for arbitrary types.
4 people like thisPosted: 12 years ago by Eirik Tsarpalis
The generic model for stateful computation (S -> S x R) provides a convenient mechanism of threading stateful computation results since the functor λR . S -> S x R is monadic. But what happens if we want to thread state itself? Well, the mapping λS. S -> S x R is not even functorial! But it turns out it can become so with a bit of trickery. This snippet demonstrates a way to lift, project or inject stateful computations into ambient state monads.
8 people like thisPosted: 12 years ago by Eirik Tsarpalis
The following is an implementation of a general-purpose pretty printer for tables. Its generality is achieved by passing an upcast rule to an untyped record type as argument.
7 people like thisPosted: 12 years ago by Eirik Tsarpalis
This is a simple IoC implementation that allows registration with optional parametrization.
2 people like thisPosted: 12 years ago by Eirik Tsarpalis
failwith/failwithf are a useful operators, but they only raise exceptions of type SystemException. Here's a simple way to generalize them.
2 people like thisPosted: 12 years ago by Eirik Tsarpalis
A simple utility that allows making assertions using potentially complex pattern matches.
3 people like thisPosted: 12 years ago by Eirik Tsarpalis
Code by me and Nick Palladinos.
2 people like thisPosted: 12 years ago by Eirik Tsarpalis
Yet another immutable queue implementation
7 people like thisPosted: 12 years ago by Eirik Tsarpalis
This is an attempt to define a workflow that contains reversible stateful computations. In the event of an exception being raised, all insofar successful operations will fold back to their original state. The implementation uses the notion of reversible computation primitives, which are composed using a free monad that is interpreted with a trampoline.
7 people like thisPosted: 11 years ago by Eirik Tsarpalis
Writing functions that recursively generate lambdas from a given parameter is a useful practice, but things get trickier when you need the output functions themselves to be mutually recursive. This is my attempt at creating a fix-point combinator that transparently handles this requirement. It also gives memoization for free as a side-effect.
7 people like thisPosted: 11 years ago by Eirik Tsarpalis
A construct that caches the result of a computation/request up to a given expiration interval. Makes use of the atom implementation found in http://fssnip.net/bw
0 people like thisPosted: 11 years ago by Eirik Tsarpalis
Or who needs non-strict semantics when you have references?
3 people like thisPosted: 11 years ago by Eirik Tsarpalis
An abstract class that implements all the boilerplate required for custom comparison semantics.
1 people like thisPosted: 11 years ago by Eirik Tsarpalis
Asynchronously await task completion with timeout support.
8 people like thisPosted: 11 years ago by Eirik Tsarpalis
An attempt at defining Peano arithmetic for all numeric types
7 people like thisPosted: 11 years ago by Eirik Tsarpalis
A simple retry combinator with customizable retry policies.
1 people like thisPosted: 11 years ago by Eirik Tsarpalis
Continuing from Nick Palladinos' session types, this snippet is an implementation of indexed monads in F#, as well as a collection of examples that demonstrate its utility: session types, type-safe serialization and reversible workflows.
6 people like thisPosted: 11 years ago by Eirik Tsarpalis
Implements a technique found in http://stackoverflow.com/a/2085377. Useful for rethrowing exceptions in the context of computation expressions, not otherwise possible, as well as a few other applications.
5 people like thisPosted: 11 years ago by Eirik Tsarpalis
This is a pattern I knocked together to address the issue of global mutable state in the F# compiler.
3 people like thisPosted: 10 years ago by Eirik Tsarpalis
Demonstrates a possible encoding for GADTs in F#. It is type safe, uses no reflection and pattern matches can be declared outside of the GADT definition itself.
4 people like thisPosted: 10 years ago by Eirik Tsarpalis
Just a quick and dirty hack
2 people like thisPosted: 10 years ago by Eirik Tsarpalis
Continuing from https://twitter.com/JKPappas/status/558339587719045120, here's an approach on extending units of measure to arbitrary types.
18 people like thisPosted: 9 years ago by Eirik Tsarpalis
A long overdue counterexample of a pure bind/return computation expression that does not satisfy any of the monad laws, as promised to @silverSpoon. This uses binary trees under the hood, which define a binary operation but do not satisfy any unit or associativity laws. Binary trees can also encode the syntactic structure of a computation expression, which is being captured using a state-updating bind implementation.
3 people like thisPosted: 9 years ago by Eirik Tsarpalis
F# computation expressions do not allow monadic expressions inside `finally` clauses. If we allow ourselves a bit of ugliness this can be circumvented by using a combinator. Here's an implementation for asynchronous workflows and a small application.
3 people like thisPosted: 9 years ago by Eirik Tsarpalis
A first attempt at implementing symbolic exception stacktraces in computation expressions using reflection.
5 people like thisPosted: 8 years ago by Eirik Tsarpalis
Works with F# 4.0 and above
7 people like thisPosted: 8 years ago by Eirik Tsarpalis
Related to https://github.com/Microsoft/visualfsharp/issues/2127
1 people like thisPosted: 7 years ago by Eirik Tsarpalis
Staged generic hashcode generation using TypeShape.
0 people like thisPosted: 7 years ago by Eirik Tsarpalis
Generic Parser Generator for F# values using TypeShape and FParsec. Supports recursive types.
4 people like thisPosted: 7 years ago by Eirik Tsarpalis
A generic map using TypeShape
1 people like thisPosted: 7 years ago by Eirik Tsarpalis
A simple evaluator for F# quotations using TypeShape
2 people like thisPosted: 7 years ago by Eirik Tsarpalis
F# adaptation of a solution to the expression problem using object algebras, c.f. https://www.cs.utexas.edu/~wcook/Drafts/2012/ecoop2012.pdf
2 people like thisPosted: 7 years ago by Eirik Tsarpalis
Defines a generic `mutate : 'T -> 'T` function that structurally mutates every value in the object graph, without performing new allocations. Needless to say, this is intended for educational purposes only.
3 people like thisPosted: 7 years ago by Eirik Tsarpalis
A lightweight result type definition without requirement for new types or explicit conversions.
6 people like thisPosted: 7 years ago by Eirik Tsarpalis
Generic, structural IDisposable generator for algebraic data types.
2 people like thisPosted: 7 years ago by Eirik Tsarpalis
An approach to using annotated types in F# using SRTPs.
4 people like thisPosted: 6 years ago by Eirik Tsarpalis
An active pattern that protects from null reference exceptions stemming from pattern matches on values materialized by .NET libraries such as Newtonsoft.Json
3 people like thisPosted: 6 years ago by Eirik Tsarpalis
A simple abstraction for performing event sourced optimistic transactions
4 people like thisPosted: 6 years ago by Eirik Tsarpalis
Proof of concept combining HKT encodings à la Higher and object algebras to create a fully type-safe, performant and extensible apparatus for writing generic programs in F#
6 people like thisPosted: 5 years ago by Eirik Tsarpalis
Derived from http://okmij.org/ftp/tagless-final/JFP.pdf
3 people like thisPosted: 5 years ago by Eirik Tsarpalis
Lightweight HKT encoding using a tiny amount of SRTP constraint solving for assigning the encoding to underlying types
5 people like thisPosted: 5 years ago by Eirik Tsarpalis
Example that combines generic programming using tagless-final and typeshape for driving folding of arbitrary types.
5 people like thisPosted: 5 years ago by Eirik Tsarpalis
Optimizing F# quotations by applying Normalization-by-Evaluation
3 people like thisPosted: 4 years ago by Eirik Tsarpalis
A minimal async builder applicatives example. Also showcases fssnip support for F# preview language features.
8 people like thisPosted: 4 years ago by Eirik Tsarpalis