Skip to content

Instantly share code, notes, and snippets.

View 1tgr's full-sized avatar
💬
Loading...

Tim Robinson 1tgr

💬
Loading...
View GitHub Profile
@1tgr
1tgr / internal_compiler_error.log
Created June 2, 2011 15:13
An F# compiler crash
C:\tim\temp>type temp.fs
namespace Tim
type SomeClass() =
member private t.Impl _ _ = ()
type SomeRecord = {
Inner : SomeClass
} with
member t.Generate arg =
type OptionMonad = | OptionMonad with
static member ret x = Some x
static member bind (x, f) = Option.bind f x
type ListMonad = | ListMonad with
static member ret x = [x]
static member bind (x, f) = List.collect f x
let inline ret (monad : ^M) (x : 'a) : 'b =
(^M : (static member ret : 'a -> 'b) (x))
let ಠ_ಠ () = printfn "ಠ_ಠ"
ಠ_ಠ ()
@1tgr
1tgr / update.fs
Created June 19, 2012 10:11
Given two sequences of items (identified by keys), generate adds/updates/deletes to make one sequence look like the other
module Utils =
let updateWith
(localKey : 'Local -> 'Key)
(remoteKey : 'Remote -> 'Key)
(add : 'State -> 'Local -> 'State)
(update : 'State -> 'Local -> 'Remote -> 'State)
(delete : 'State -> 'Remote -> 'State)
(state : 'State)
(local : #seq<'Local>)
@1tgr
1tgr / it.fsx
Created September 18, 2012 10:18
Inspect the last value evaluated in FSI
// Run this as: fsi --use:it.fsx --quiet
open System
open System.Reflection
open Microsoft.FSharp.Compiler.Interactive
let (getSavedItType : unit -> Type, getSavedIt : unit -> obj) =
let t = typeof<IEventLoop>
let t = t.Assembly.GetType(t.Namespace + ".RuntimeHelpers", true)
let bf = BindingFlags.Static ||| BindingFlags.NonPublic
let itTypeMethod = t.GetMethod(name = "GetSavedItType", bindingAttr = bf)
@1tgr
1tgr / dag.fsx
Created October 17, 2012 21:28
Demonstrates functions within a graph. Functions don't know about each other; they take dependencies on strongly-typed nodes. We can inspect the structure of the graph separately from evaluating it.
open System
open System.Collections.Generic
type IKey =
interface end
type IKey<'Value> =
inherit IKey
[<AbstractClass>]
@1tgr
1tgr / fact.fs
Created November 9, 2012 13:41
F# tail recursion optimization
let rec fact acc =
function
| 1 -> acc
| n -> fact (acc * n) (n - 1)
assert fact 1 6 == 720

Chaotic Sequence

Logistic Map definition

let logistic a x0 = Seq.unfold (fun xn -> Some(xn, a * xn * (1.0 - xn))) x0

For low a, the sequence is converging to a fixed point

Maze

A Unicode maze, inspired by 10 PRINT CHR$ (205.5 + RND (1)); : GOTO 10.

open System
let r = Random() in for j in 1 .. 25 do Console.WriteLine (String([| for i in 1 .. 80 -> char (0x2571 + r.Next(0, 2)) |]))
// Output
@1tgr
1tgr / inline.fs
Created February 15, 2013 15:08
Adventures in inlining
module Program
// Copy and paste from the FSharp.Core source code
let inline zeroCreateUnchecked (count:int) =
(# "newarr !0" type ('T) count : 'T array #)
let inline map fn a =
let b = zeroCreateUnchecked (Array.length a)
for i in 0 .. Array.length a - 1 do
b.[i] <- fn a.[i]