Skip to content

Instantly share code, notes, and snippets.

@palladin
palladin / Overload.fsx
Last active September 25, 2021 17:52
Overload
type Term<'a>() = class end
type Tuples' = Tuples' with
static member inline (?<-) (_ : Tuples', _ : (Term<_> * Term<_>), _ : (Term<_> * Term<_>)) = 2
static member inline (?<-) (_ : Tuples', a:Term<_>, b : Term<_>) = 4
static member inline (?<-) (_ : Tuples', a:bool, b:bool) = 3
static member inline (?<-) (_ : Tuples', a:string, b:string) = 1
let inline call_2 (t:^t,a:^a,b:^b) : int =
(t ? (a) <- b )
@palladin
palladin / Overload.fsx
Last active September 25, 2021 17:52
Overload resolution and inline trick
type Term<'a>() = class end
type Tuples = Tuples with
static member inline (?<-) (_ : Tuples, _ : Term<_>, _ : Term<_>) = 0
static member inline (?<-) (_ : Tuples, (_:Term<_>,_:Term<_>),(_:Term<_>, _:Term<_>)) = 2
static member inline (?<-) (_ : Tuples, a:bool, b:bool) = 3
let inline call_2 (t:^t,a:^a,b:^a) : int =
(t ? (a) <- b )
@palladin
palladin / gist:c790cd72c639cb281880
Created March 26, 2015 13:41
Normalisation by evaluation
open System
open Microsoft.FSharp.Quotations
open Microsoft.FSharp.Quotations.Patterns
// Helper active patters for System.Type
let (|Arrow|_|) (t : Type) =
if t.IsGenericType && t.GetGenericTypeDefinition() = typedefof<_ -> _> then
let args = t.GetGenericArguments()
Some (args.[0], args.[1])
#include <iostream>
using namespace std;
int foo()
{
auto flatMap = [](auto f, auto s) { return [=](auto k) { return s([=](auto x) { f(x)(k); }); }; };
auto map = [](auto f, auto s) { return [=](auto k) { s([=](auto x) { k(f(x)); }); }; };
auto filter = [](auto p, auto s) { return [=](auto k) { s([=](auto x) { if(p(x)) k(x); }); }; };
auto sum = [](auto k) { int sum = 0; k([&](int x) { sum += x; }); return sum; };
@palladin
palladin / gist:671b514df45fed287e5f
Created January 24, 2015 15:21
A type-level SAT solver in F#
type Bool = interface end
and True = True with
interface Bool
and False = False with
interface Bool
type And = And with
static member inline (?<-) (True, And, True) = True
static member inline (?<-) (True, And, False) = False
@palladin
palladin / gist:bc278fc010e4d244ef7a
Last active September 25, 2021 17:52
Experiment
#time "on"
let inline readAllHashes nSkip nSentences =
sentencesFile
|> File.ReadLines
|> Stream.ofSeq
|> Stream.skip nSkip
|> Stream.take nSentences
|> Stream.map(fun line ->
let separatorIndex = line.IndexOf(' ')
@palladin
palladin / gist:6fac782a0ba4aa6b88b5
Last active September 25, 2021 17:51
Push/Pull Streams
type Stream<'T> = ('T -> unit) -> unit -> bool
let inline ofArray (source : 'T []) : Stream<'T> =
fun iterf ->
let i = ref 0
fun () ->
let flag = !i < source.Length
if not flag then
false
@palladin
palladin / gist:aac92fe83aba3027969c
Last active September 25, 2021 17:51
Func<...> vs FSharpFunc<...> (2-arg functions)
#time
let sum (f : 'T -> int -> int) (array : 'T[]) =
let mutable sum = 0
for i = 0 to array.Length - 1 do
sum <- sum + f array.[i] i
sum
type Grouping<'T> = { IndexRef : int ref; ArrayRef : 'T [] ref }
let rec groupingAdd (value : 'T) (grouping : Grouping<'T>) =
let indexRef = grouping.IndexRef
let arrayRef = grouping.ArrayRef
let array = !arrayRef
if Object.ReferenceEquals(array, null) then
groupingAdd value grouping
else
let index = Interlocked.Increment(indexRef)
@palladin
palladin / gist:66d93ff825402f1af15e
Created August 5, 2014 15:51
ParStream.groupBy performance test
#time
#r "bin/Release/Streams.Core.dll"
open Nessos.Streams.Core
let data = [|1..10000000|] |> Array.map (fun i -> int64 <| (i % 1000000))
#r "../../packages/FSharp.Collections.ParallelSeq.1.0/lib/net40/FSharp.Collections.ParallelSeq.dll"
open FSharp.Collections.ParallelSeq