Skip to content

Instantly share code, notes, and snippets.

@palladin
palladin / gist:1583661
Created January 9, 2012 16:22
n-ary Seq.map
let (<*>) fs xs = Seq.map2 (fun f x -> f x) fs xs
let map3 f xs bs cs = Seq.map2 f xs bs <*> cs
let map4 f xs bs cs ds = map3 f xs bs cs <*> ds
map4 (fun x b c d -> x + b + c + d) [1;2] [1;2] [1;2] [1;2] // [4; 8]
@palladin
palladin / gist:1451796
Created December 9, 2011 14:46
Polyvariadic fixpoint
// http://okmij.org/ftp/Computation/fixed-point-combinators.html
let force (value : Lazy<_>) = value.Force()
let fix f = let rec x = lazy (f x) in x
let fix' (fs : list<Lazy<list<'a -> 'b>> -> 'a -> 'b>) : Lazy<list<'a -> 'b>> =
fix (fun r -> fs |> List.map (fun f -> f r))
let fe l x =
let [e; o] = force l
@palladin
palladin / gist:1293387
Created October 17, 2011 18:32
Monadic Retry
open System
open System.Threading
type ShouldRetry = ShouldRetry of (RetryCount * LastException -> bool * RetryDelay)
and RetryCount = int
and LastException = exn
and RetryDelay = TimeSpan
type RetryPolicy = RetryPolicy of ShouldRetry
type RetryPolicies() =
@palladin
palladin / gist:1180567
Created August 30, 2011 09:49
Scrap Your Boilerplate
// http://research.microsoft.com/enus/um/people/simonpj/papers/hmap/hmap.ps
// Type safe conversion functions
let cast<'T, 'R> (v : 'T) : 'R = v :> obj :?> 'R
let mkT<'T, 'R> (f : 'T -> 'T) : 'R -> 'R =
if typeof<'T> = typeof<'R> then (fun (v : 'R) -> v |> cast |> f |> cast) else id
let mkQ (r : 'R) (q : 'B -> 'R) (a : 'A) : 'R =
if typeof<'A> = typeof<'B> then
a |> cast |> q
else r
@palladin
palladin / gist:1134074
Created August 9, 2011 13:42
Async based MapReduce
open System
open System.Net
open Microsoft.FSharp.Control.WebExtensions
let noiseWords = [|"a"; "about"; "above"; "all"; "along"; "also"; "although"; "am"; "an"; "any"; "are"; "aren't"; "as"; "at";
"be"; "because"; "been"; "but"; "by"; "can"; "cannot"; "could"; "couldn't";
"did"; "didn't"; "do"; "does"; "doesn't"; "e.g."; "either"; "etc"; "etc."; "even"; "ever";
"for"; "from"; "further"; "get"; "gets"; "got"; "had"; "hardly"; "has"; "hasn't"; "having"; "he";
@palladin
palladin / gist:1106567
Created July 26, 2011 11:50
Erik Lippert's Comma Quibbling
// http://blogs.msdn.com/b/ericlippert/archive/2009/04/15/comma-quibbling.aspx
#time
#r "FSharp.PowerPack.dll"
open System
open System.Text
let format (words : seq<string>) =
let sb (value : string) = new StringBuilder(value)
@palladin
palladin / gist:1088015
Created July 17, 2011 20:16
LazyList
open System.Collections
open System.Collections.Generic
// LazyList dataType + Monoid Structure
type LazyList<'T> = Empty
| Cons of 'T * (unit -> LazyList<'T>)
| Delay of (unit -> LazyList<'T>)
| Combine of LazyList<'T> * LazyList<'T> with
interface IEnumerable<'T> with
member self.GetEnumerator() =
@palladin
palladin / gist:1088013
Created July 17, 2011 20:15
A beautiful fixed-point finding function
let rec iterate f value =
seq { yield value;
yield! iterate f (f value) }
let fixedPoint f initial =
iterate f initial
|> Seq.pairwise
|> Seq.pick (fun (first, second) ->
if first = second then Some first else None)
@palladin
palladin / gist:1088010
Created July 17, 2011 20:14
Hughes's CPSFuncList
type FuncList<'a> = 'a list -> 'a list
type CPSFuncList<'a> = FuncList<'a> -> FuncList<'a>
// Monoid comprehension
type CPSFuncListBuilder() =
member self.Combine (first : CPSFuncList<'a>, second : CPSFuncList<'a>) : CPSFuncList<'a> =
(fun k -> second (fun tail -> first (fun tail' -> k tail') tail))
member self.Zero() : CPSFuncList<'a> = (fun k tail -> k tail)
member self.Yield (value : 'a) : CPSFuncList<'a> = (fun k tail -> k (value :: tail))
member self.YieldFrom (value : CPSFuncList<'a>) : CPSFuncList<'a> = value
@palladin
palladin / gist:1088009
Created July 17, 2011 20:13
Hughes's FuncList
// for more info http://www.cs.tufts.edu/~nr/cs257/archive/john-hughes/lists.pdf
type FuncList<'a> = 'a list -> 'a list
// Monoid comprehension
type FuncListBuilder() =
member self.Combine (first : FuncList<'a>, second : FuncList<'a>) : FuncList<'a> = (first << second)
member self.Zero() : FuncList<'a> = id
member self.Yield (value : 'a) : FuncList<'a> = fun tail -> value :: tail
member self.YieldFrom (value : FuncList<'a>) : FuncList<'a> = value
member self.Delay ( f : unit -> FuncList<'a>) : FuncList<'a> = (fun tail -> f () tail)