This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Based on http://blogs.msdn.com/b/chrsmith/archive/2008/02/22/regular-expressions-via-active-patterns.aspx | |
open System | |
open System.Text.RegularExpressions | |
let (|Match|_|) (pat:string) (inp:string) = | |
let m = Regex.Match(inp, pat) in | |
if m.Success | |
then Some (List.tail [ for g in m.Groups -> g.Value ]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#r "FSharp.PowerPack.dll" | |
open System | |
// Generic container of 'T | |
// Also parameterized by 'TypeClass : (new : unit -> 'TypeClass) | |
// to implicit get a 'TypeClass instance (like passing the type class dictionary) | |
// The idea is to encode Type Classes with subtype polymorphism and OOP Classes | |
type Generic<'T, 'TypeClass when 'TypeClass : (new : unit -> 'TypeClass)> = interface end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Inspired by http://www.cs.utexas.edu/~wcook/Drafts/2006/MemoMixins.pdf | |
// State Monad combined with Continuation Monad (StateT Monad transformer) | |
type StateContMonad<'s, 'a, 'r> = StateContMonad of ('s -> ('s -> 'a -> 'r) -> 'r) | |
// Computation Builder | |
type StateContBuilder() = | |
member self.Return value = | |
StateContMonad (fun state k -> k state value) | |
member self.Bind(StateContMonad contStateMonad, f) = |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Haskell-inspired infinite sequences | |
#r "FSharp.Powerpack" | |
let rec repeat n = LazyList.consDelayed n (fun () -> repeat n) | |
repeat 1 // seq [1; 1; 1; 1; ...] | |
let rec integersFrom n = LazyList.consDelayed n (fun () -> LazyList.map ((+) 1) <| integersFrom n) | |
integersFrom 3 // seq [3; 4; 5; 6; ...] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let s : Printf.TextWriterFormat<_> = "let s : Printf.TextWriterFormat<_> = %c%s%c in | |
printf s (char 34) (s.Value) (char 34)" in printf s (char 34) (s.Value) (char 34) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// x = f(x) encoded in F# | |
let force (value : Lazy<_>) = value.Force() | |
let fix f = let rec x = lazy (f x) in x | |
// Examples | |
let fac = fix (fun f x -> if x = 0 then 1 else x * force f (x - 1) ) | |
let nums = fix (fun v -> seq { yield 0; yield! Seq.map ((+) 1) (force v) }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For more info | |
// http://www.springerlink.com/content/g74174vvl1861605/ | |
// http://www.haskell.org/haskellwiki/Circular_programming | |
// Helper functions | |
let force (value : Lazy<_>) = value.Force() | |
let lazyMap f l = lazy (f (force l)) | |
// Generic feedback loop function | |
let trace f input = |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Info: http://clojure.org/atoms | |
open System | |
open System.Threading | |
type Atom<'T when 'T : not struct>(value : 'T) = | |
let refCell = ref value | |
let rec swap f = | |
let currentValue = !refCell |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open System.Threading | |
type Atom<'T when 'T : not struct>(value : 'T) = | |
let refCell = ref value | |
let rec swap f = | |
let currentValue = !refCell | |
let result = Interlocked.CompareExchange<'T>(refCell, f currentValue, currentValue) | |
if obj.ReferenceEquals(result, currentValue) then () | |
else Thread.SpinWait 20; swap f |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Functional Unparsing http://www.brics.dk/RS/98/12/BRICS-RS-98-12.pdf | |
open System | |
open System.Data | |
open System.Data.SqlClient | |
// Type Decls | |
type SqlText = string | |
type Counter = int |