Created
September 15, 2021 21:32
-
-
Save ken-okabe/4b61a045c4f1c2fb6a23fc341efbe6cf to your computer and use it in GitHub Desktop.
This file contains 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
const identity = a => a; | |
const customOperator = op => f => set => | |
Object.defineProperty(set, op, { | |
value: function (a) { | |
return f(a)(this); | |
} | |
});//returns new set/object | |
Function.prototype |> | |
customOperator('.') | |
(g => f => x => x |> f |> g); | |
// function composition operator is '.' | |
const log = console.log; | |
"associativity------"|> log; | |
const f1 = a => a * 2; | |
const f2 = a => a + 1; | |
1|>f1|>f2 | |
|> log; //3 | |
1|>(x => x|> f1|> f2) | |
|> log; //3 | |
1|>(f1['.'](f2)) | |
|> log; //3 | |
"idientiry left-right-- m * f ----"|>log; | |
const e = identity; | |
const f = a => a * 2; | |
{ | |
// cleaner Monad laws | |
// Left/Right identity | |
"e.f = f = f.e" |> log; | |
//left e.f = f | |
const left = 1 |> | |
(e)['.'](f); | |
//center f | |
const center = 1 |> | |
f; | |
//right f = f.e | |
const right = 1 |> | |
(f)['.'](e); | |
left |> log; //2 | |
center |> log; //2 | |
right |>log; //2 | |
} | |
{ | |
// typical Monad laws shown in Haskell | |
// Left identity | |
"e(x) |> f == f(x)" |> log; | |
const x = 1; | |
e(x) |> f | |
|>log; //2 | |
f(x) | |
|>log; //2 | |
// Right identity | |
"m |> e == m" |> log; | |
const m = 2 | |
m |> e | |
|>log; //2 | |
m |> log; //2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment