Skip to content

Instantly share code, notes, and snippets.

Avatar

Pierre Irrmann pirrmann

View GitHub Profile
@pirrmann
pirrmann / RPN.fs
Last active Jan 15, 2016
Reverse Polish Notation
View RPN.fs
let (|Operator|_|) s =
match s with
| "+" -> Some (+)
| "-" -> Some (-)
| "*" -> Some (*)
| "/" -> Some (/)
| _ -> None
let eval state input =
match input, state with
@pirrmann
pirrmann / Constraints
Created Apr 9, 2015
Statically resolved member constraints
View 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 Aug 29, 2015
Scoped rules builder
View RulesBuilder.fs
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 Nov 21, 2014
Using a computation expression to produce test cases
View TestCasesBuilder.fs
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 Dec 25, 2015 — 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
View 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
module Naturals
open System
type Natural =
| Zero
| Succ of Natural
member x.IsZero' = x = Zero