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 (<*>) 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] |
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
// 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 |
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 | |
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() = |
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
// 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 |
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 | |
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"; |
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
// 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) |
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.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() = |
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 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) |
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
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 |
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.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) |