Skip to content

Instantly share code, notes, and snippets.

@dreadedsoftware
Created May 3, 2016 01:09
Show Gist options
  • Save dreadedsoftware/a3e07e9f36973f58f193aeb473252e0b to your computer and use it in GitHub Desktop.
Save dreadedsoftware/a3e07e9f36973f58f193aeb473252e0b to your computer and use it in GitHub Desktop.
//cannot do higher kinds so no type classes
//do not need higher kinds; type syntax takes care of it
//need higher kinds for useful free monad.
//no way around that?
module CategoryList
type Functor<'Type>() =
member this.map(l: list<'Type>)(f: 'Type -> 'Return) =
List.map f l
type Monad<'Type>(func: Functor<'Type>) =
member this.point(t: 'Type) = [t]
member this.flatMap(l: list<'Type>)(f: 'Type -> list<'Return>) =
List.concat(func.map l f)
module Category
let check() =
//instantiate monad
let func = CategoryList.Functor<int>()
let monad = CategoryList.Monad(func)
//define monadic chain functions
let flatMap l f = monad.flatMap f l
let map l f = func.map f l
//define helpers
let mapper (x:int) = [1 .. x]
let mapperStr (x:int) = x.ToString()
let hundred = [1 .. 100]//data
//(|>) language level monad support
let hundredMore = (hundred
|> flatMap mapper)
printfn "%A" hundredMore
let bigString = (hundredMore
|> flatMap mapper
|> map mapperStr)
printfn "%A" bigString
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment