Skip to content

Instantly share code, notes, and snippets.

@palladin
palladin / gist:6157192
Created August 5, 2013 16:15
Session Types in F#
// http://www.eecs.harvard.edu/~tov/pubs/haskell-session-types/session08.pdf
// Basic Operations
[<AbstractClass>]
type Ops = class end
type Eps = class inherit Ops end
type Send<'T, 'Rest when 'Rest :> Ops> = class inherit Ops end
type Recv<'T, 'Rest when 'Rest :> Ops> = class inherit Ops end
type Choose<'Left, 'Right when 'Left :> Ops and 'Right :> Ops> = class inherit Ops end
@palladin
palladin / gist:6200023
Created August 10, 2013 11:01
Delimited continuations
type Cont<'T, 'S, 'R> = Cont of (('T -> 'S) -> 'R)
type ContBuilder() =
member self.Return x = Cont (fun k -> k x)
member self.Bind (c : Cont<_, _, _>, f : _ -> Cont<_, _, _>) =
Cont (fun k -> let (Cont contf) = c in contf (fun v -> let (Cont contf') = f v in contf' k))
let delim = new ContBuilder()
let run (c : Cont<_, _, _>) = let (Cont contf) = c in contf id
@palladin
palladin / gist:6270421
Created August 19, 2013 15:28
Effects and Handlers
// http://homepages.inf.ed.ac.uk/slindley/papers/handlers-draft-march2013.pdf
// Quote from the paper
// Effect handler operation clauses generalise exception handler clauses
// by adding a continuation argument, providing support for arbitrary effects. An operation
// clause is an exception clause if it ignores its continuation argument.
type Cont<'T, 'R> = Cont of ((('T -> 'R) * (exn -> 'R)) -> 'R)
type ContBuilder() =
member self.Return x = Cont (fun (k, _) -> k x)
@palladin
palladin / gist:8046053
Last active September 25, 2021 17:51
Mandelbrot set with LINQ
// http://msmvps.com/blogs/jon_skeet/archive/2008/02/26/visualising-the-mandelbrot-set-with-linq-yet-again.aspx
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Imaging;
using System.Diagnostics;
using LinqOptimizer.CSharp;
@palladin
palladin / gist:8561783
Created January 22, 2014 16:22
My Favorite Active Pattern
let (|Named|Array|Ptr|Param|) (t : System.Type) =
if t.IsGenericType
then Named(t.GetGenericTypeDefinition(), t.GetGenericArguments())
elif t.IsGenericParameter
then Param(t.GenericParameterPosition)
elif not t.HasElementType
then Named(t, [| |])
elif t.IsArray
then Array(t.GetElementType(), t.GetArrayRank())
elif t.IsByRef
@palladin
palladin / gist:8577661
Last active September 25, 2021 17:51
F# style LINQ programming with tuples
public static class EnumerableEx
{
public static IEnumerable<R> Select<T1, T2, R>(this IEnumerable<Tuple<T1, T2>> source, Func<T1, T2, R> f)
{
return source.Select(t => f(t.Item1, t.Item2));
}
}
Enumerable.Range(1, 10)
.Select(x => Tuple.Create(x, x))
@palladin
palladin / Bug.fsx
Created March 7, 2014 15:02
Line 7 - Error 1 'base' values may only be used to make direct calls to the base implementations of overridden members Script1.fsx 9 30 Miscellaneous Files
type MyClassBase1<'T> () =
abstract member function1 : unit -> unit
default u.function1 () = ()
and MyClassDerived1 () =
inherit MyClassBase1<int>()
override u.function1 () = base.function1()
@palladin
palladin / gist:10106955
Created April 8, 2014 10:15
Mono Tail-call test
// mono --optimize=tailc
let fs = [1..1000000] |> List.map (fun i -> (fun k j -> k (i + j)))
let f = List.fold (fun f' f -> f f') id fs
f 1 // StackOverflow
@palladin
palladin / gist:500dac7d4bd5419a3320
Created May 23, 2014 12:30
FsControl ZipIndex
#r "../packages/FsControl.1.0.8/lib/net40/FsControl.Core.dll"
open FsControl.Core.TypeMethods
open FsControl.Core.Types
let inline result (x:'a): 'Functor'a = Inline.instance Applicative.Pure x
let inline (>>=) (x:'Monad'a) (f:'a->'Monad'b) :'Monad'b = Inline.instance (Monad.Bind, x) f
let inline traverse (f:'a->'Applicative'b) (t:'Traversable'a) :'Applicative'Traversable'b = Inline.instance (Traversable.Traverse , t) f
type MonadBuilder() =
member inline b.Return(x) = result x
@palladin
palladin / gist:c2964748e45a0a59a15b
Last active September 25, 2021 17:52
Simple CPS composition
let inline f k = (fun x -> k (x + 1))
let inline g k = (fun x -> k (x + 2))
(f << g) id 1 // 4