Skip to content

Instantly share code, notes, and snippets.

View eiriktsarpalis's full-sized avatar

Eirik Tsarpalis eiriktsarpalis

View GitHub Profile
@eiriktsarpalis
eiriktsarpalis / for-cancellation.fsx
Created October 26, 2017 12:01
Encoding Cancellation on top of foreach loops
// One of the common annoyances when writing imperative code in F# is that the
// expression-based nature of the language makes it effectively impossible to
// break or return in the midst of a for-loop. This is an attempt to encode
// lightweight cancellation on top of native for loops. As usual, this is intended
// as a PoC and should not be considered seriously for production code.
module Seq =
open System.Collections
open System.Collections.Generic
@eiriktsarpalis
eiriktsarpalis / test.fsx
Created August 2, 2017 13:59
Class vs Struct Index Performance
#time "on"
open System
open System.Collections.Generic
let gdata = [|for i in 1 .. 1000000 -> Guid.NewGuid()|]
let sdata = gdata |> Array.map string
// Real: 00:00:17.774, CPU: 00:00:17.703, GC gen0: 378, gen1: 61, gen2: 1
let gmap = ref Map.empty<Guid, int>
@eiriktsarpalis
eiriktsarpalis / foo.fsx
Created March 17, 2017 12:42
Spot the antipattern
module Client =
let connectionString = Environment.GetEnvironmentVariable("CONN")
let session = new Session(connectionString)
let doA (a : A) = session.Send a
let doB (b : B) = session.Send b
type Client(connectionString : string) =
@eiriktsarpalis
eiriktsarpalis / cont.fsx
Last active December 22, 2020 00:32
A Continuation monad with stacktrace support in F# 4.1
open System
open System.Runtime.CompilerServices
type SymbolicException =
{
Source : Exception
Stacktrace : string list
}
module SymbolicException =
@eiriktsarpalis
eiriktsarpalis / continuation.json
Created May 14, 2016 09:58
Serialization of a relatively small continuation in MBrace
{
"Success": {
"_flags": "subtype",
"subtype": {
"Case": "GenericTypeInstance",
"GenericDefinition": {
"Case": "NamedType",
"Name": "MBrace.Core.Builders+Bind@331-1",
"Assembly": {
"Name": "MBrace.Core",
@eiriktsarpalis
eiriktsarpalis / SpecificCall2.fsx
Created February 24, 2016 10:24
SpecificCall on generic type methods
open System
open System.Reflection
open Microsoft.FSharp.Quotations
open Microsoft.FSharp.Quotations.Patterns
open Microsoft.FSharp.Quotations.DerivedPatterns
type MethodInfo with
/// Gets the underlying method definition
/// including the supplied declaring type and method type arguments
@eiriktsarpalis
eiriktsarpalis / constraint.fs
Last active March 28, 2016 11:48
Strange issue in F# constraint solving
type Test =
static member F<'T>(t : 'T option) = ()
static member F<'T>(e : 'T -> 'T) = ()
type Foo = { A : int }
type Bar = { A : int }
Test.F(fun (r:Foo) -> { r with A = 32 }) // type checks
Test.F(fun (r:Bar) -> { r with A = 32 }) // type checks
Test.F<Foo>(fun r -> { r with A = 32 }) // type error
type Cont<'T> = ('T -> unit) -> (exn -> unit) -> unit
let ret t = fun sc _ -> sc t
let (>>=) f g =
fun sc ec ->
let sc' (t : 'T) =
match (try Choice1Of2 (g t) with e -> Choice2Of2 e) with
| Choice1Of2 g -> g sc ec
| Choice2Of2 e -> ec e
@eiriktsarpalis
eiriktsarpalis / async-perf-bug.fsx
Last active April 7, 2017 18:06
Async performance bug
let run x = Async.RunSynchronously x
let runParallel (workflow : Async<'T>) =
[| for i in 1 .. 100 -> workflow |]
|> Async.Parallel
|> run
let a = async { return 42 }
let b = async { return run (async { return 42 })}
@eiriktsarpalis
eiriktsarpalis / newtype.fs
Last active August 29, 2015 14:14
NewType in F#
// current type abbreviations in F#
type A = Guid
type B = Guid
let foo (x : A) = x.ToString()
let b = B.NewGuid()
foo(b) // type checks
// proposed newtype abbreviations
newtype A = Guid