Skip to content

Instantly share code, notes, and snippets.

View eiriktsarpalis's full-sized avatar
🌴
On vacation

Eirik Tsarpalis eiriktsarpalis

🌴
On vacation
View GitHub Profile
@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
@eiriktsarpalis
eiriktsarpalis / gist:0d54b7c06909514bd378
Created December 28, 2014 15:01
Functional pipelines & F# member constraints
let inline map (alg : ^Alg) (f : ^t -> ^s) (ts : ^ts) : ^ss =
(^Alg : (member Map : (^t -> ^s) * ^ts -> ^ss) (alg, f, ts))
let inline filter (alg : ^Alg) (f : ^t -> bool) (ts : ^ts) : ^ts =
(^Alg : (member Filter : (^t -> bool) * ^ts -> ^ts) (alg, f, ts))
let inline reduce (alg : ^Alg) (f : ^t -> ^t -> ^t) (ts : ^ts) : ^t =
(^Alg : (member Reduce : (^t -> ^t -> ^t) * ^ts -> ^t) (alg, f, ts))
// type signature is unreadable, machine consumable only
@eiriktsarpalis
eiriktsarpalis / groupWhile.fs
Created November 17, 2014 23:38
groupwhile
type Stream<'T> = ('T -> unit) -> unit
module Stream =
let inline groupWhile (pred : 'T -> bool) (source : Stream<'T>) : Stream<'T []> =
fun k ->
let ra = new ResizeArray<'T> ()
let iter (t : 'T) =
if not <| pred t then
k <| ra.ToArray()
ra.Clear()