Skip to content

Instantly share code, notes, and snippets.

View pirrmann's full-sized avatar

Pierre Irrmann pirrmann

View GitHub Profile
@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
@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 / 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 / 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 / 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