Created
May 6, 2018 03:15
-
-
Save masaeedu/41d9b767291548d95914f71d56017e02 to your computer and use it in GitHub Desktop.
n-ification
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
| const { pipe, compose, map, encase, K, range, pow } = require("sanctuary"); | |
| const ret = compose; | |
| const repeat = n => x => map(K(x))(range(0, n)); | |
| const nify = n => pipe(repeat(n - 1)(ret)); | |
| const fail = () => { | |
| throw new Error(); | |
| }; | |
| // N-ifying encase | |
| const ncs1 = nify(1)(encase); | |
| const ncs2 = nify(2)(encase); | |
| const ncs3 = nify(3)(encase); | |
| // ... | |
| ncs1(a => fail())(1); | |
| // Nothing | |
| ncs2(a => b => fail())(1)(2); | |
| // Nothing | |
| ncs3(a => b => c => fail())(1)(2)(3); | |
| // Nothing | |
| // ... | |
| // N-ifying compose | |
| // (y -> z) -> (a -> ... -> (x -> y)) | |
| // ^-- edit this | |
| // (a -> ... -> (x -> z)) | |
| // ^-- to this | |
| // by composing with (y -> z) | |
| const compose2 = f => nify(2)(compose(f)); | |
| const compose3 = f => nify(3)(compose(f)); | |
| const compose4 = f => nify(4)(compose(f)); | |
| // ... | |
| compose2(pow(2))(x => y => x + y)(1)(2); | |
| // 9 | |
| compose3(pow(2))(x => y => z => x + y + z)(1)(2)(3); | |
| // 36 | |
| // ... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment