Skip to content

Instantly share code, notes, and snippets.

View pirrmann's full-sized avatar

Pierre Irrmann pirrmann

View GitHub Profile
@pirrmann
pirrmann / Nat.fs
Last active December 25, 2015 21:59 — forked from akimboyko/Nat.fs
An implementation of basic non-negative integers, based on https://gist.github.com/akimboyko/7019648, but rewritten in a more F# idiomatic way
//An implementation of basic non-negative integers, based on https://gist.github.com/akimboyko/7019648,
//but rewritten in a more F# idiomatic way
module Naturals
open System
type Natural =
| Zero
| Succ of Natural
member x.IsZero' = x = Zero
@pirrmann
pirrmann / TestCasesBuilder.fs
Created November 21, 2014 17:59
Using a computation expression to produce test cases
module TestCasesBuilder
open Microsoft.FSharp.Reflection
type TestCasesBuilder() =
member x.Zero() = Seq.empty
member x.Yield<'T>(t:'T) =
let value =
if FSharpType.IsTuple(typeof<'T>)
then t |> FSharpValue.GetTupleFields
@pirrmann
pirrmann / RulesBuilder.fs
Last active August 29, 2015 14:10
Scoped rules builder
type ScopeAndRules = | ScopeAndRules of string * int list
type RulesBuilder() =
member x.Yield(v) = []
[<CustomOperation("scope", MaintainsVariableSpace=true)>]
member x.Scope(source, scope) = ScopeAndRules(scope, []) :: source
[<CustomOperation("rule", MaintainsVariableSpace=true)>]
member x.Rule(source, rule) =
match source with
| ScopeAndRules(scope, rules) :: tail -> ScopeAndRules(scope, rule :: rules) :: tail
@pirrmann
pirrmann / Constraints
Created April 9, 2015 09:05
Statically resolved member constraints
type SyntaxToken = string
type X =
{
SomeProperty: string
} with
member this.AddModifiers(tokens:SyntaxToken array) =
{ this with
SomeProperty = sprintf "%s + %s" (this.SomeProperty) (System.String.Join(",", tokens)) }
@pirrmann
pirrmann / RPN.fs
Last active January 15, 2016 13:04
Reverse Polish Notation
let (|Operator|_|) s =
match s with
| "+" -> Some (+)
| "-" -> Some (-)
| "*" -> Some (*)
| "/" -> Some (/)
| _ -> None
let eval state input =
match input, state with

Keybase proof

I hereby claim:

  • I am pirrmann on github.
  • I am pirrmann (https://keybase.io/pirrmann) on keybase.
  • I have a public key whose fingerprint is C754 AB85 AFA5 4BC4 12CA BDF5 7CE5 02AF 2D5C C545

To claim this, I am signing this object:

@pirrmann
pirrmann / ChaosBuilder.fsx
Last active April 1, 2016 06:58
ChaosBuilder
#r "packages/Unquote/lib/net45/Unquote.dll"
open FSharp.Quotations
open Swensen.Unquote
type IMutationSitePicker =
abstract member PickNextSite: bool
abstract member NotifyIgnoredSite: unit -> unit
abstract member NotifyMutation: unit -> unit
@pirrmann
pirrmann / Dynamic.fsx
Created September 19, 2016 13:00
F# Dynamic property get with null values
open FSharp.Interop.Dynamic
open Dynamitey
let dynamicPropertyGet<'TResult> (name:string) (target:obj) : 'TResult option =
let resultType = typeof<'TResult>
let (|NoConversion| Conversion|) t = if t = typeof<obj> then NoConversion else Conversion
let convert r = match resultType with | NoConversion -> r | _ -> dynImplicit(r)
match target with
@pirrmann
pirrmann / Parsing.fsx
Last active October 12, 2016 08:00
Titanic CSV loading
let file = System.IO.File.ReadAllText(__SOURCE_DIRECTORY__ + "/training.csv")
let parseLine line =
let makeWord = Seq.rev >> Seq.toArray >> System.String
let rec parseFields chars =
match chars with
| '\"' :: chars' -> parseEscaped [] chars'
| chars' -> parseUnescaped [] chars'
and parseEscaped acc chars = seq {
type Result<'T> =
| Success of 'T
| Failure of string
let bind f = function | Success a -> f a | Failure b -> Failure b
let map f = function | Success a -> Success (f a) | Failure b -> Failure b
let private combine2 fn2 fn1 arg =
match (fn1 arg, fn2 arg) with
| Success a, Success b -> Success (a, b)