Last active
November 26, 2018 01:28
-
-
Save twopoint718/6c93e1f886ecd7da1aef5f5b6687cba7 to your computer and use it in GitHub Desktop.
Reader implementation in ReasonML. Possible uses as an alternative to something like a Redux store?
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 person = { | |
name: string, | |
age: int, | |
}; | |
let name = person => person.name; | |
let age = person => person.age; | |
let doMyThing: Reader.t(person, string) = | |
Reader.( | |
asks(name) >>= name' => | |
asks(age) >>= age' => | |
pure(name' ++ " " ++ string_of_int(age')) | |
); | |
let chris = {name: "Chris", age: 37}; | |
let output = Reader.run(doMyThing, chris); | |
let _ = Js.log(output); |
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 t('r, 'a) = 'r => 'a; | |
let pure = (x, _) => x; | |
let map = (f, r, k) => f(r(k)); | |
let apply = (f, r, k) => { | |
let f' = f(k); | |
let r' = r(k); | |
f'(r'); | |
}; | |
let join = (f, x) => f(x, x); | |
let bind = (r, f) => join(map(f, r)); | |
let (<$>) = map; | |
let (<*>) = apply; | |
let (>>=) = bind; | |
let identity = x => x; | |
let ask = identity; | |
let asks = f => bind(ask, r => pure(f(r))); | |
let run = (r, init) => r(init); |
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 t('r, 'a); | |
let pure: 'a => t('r, 'a); | |
let map: ('a => 'b, t('r, 'a)) => t('r, 'b); | |
let apply: (t('r, 'a => 'b), t('r, 'a)) => t('r, 'b); | |
let bind: (t('r, 'a), 'a => t('r, 'b)) => t('r, 'b); | |
let join: t('r, t('r, 'a)) => t('r, 'a); | |
let (<$>): ('a => 'b, t('r, 'a)) => t('r, 'b); | |
let (<*>): (t('r, 'a => 'b), t('r, 'a)) => t('r, 'b); | |
let (>>=): (t('r, 'a), 'a => t('r, 'b)) => t('r, 'b); | |
let ask: t('r, 'r); | |
let asks: ('r => 'a) => t('r, 'a); | |
let run: (t('r, 'a), 'r) => 'a; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment