Last active
April 4, 2019 11:08
-
-
Save dewey92/365fce11a9a6347e45c58821a32f2681 to your computer and use it in GitHub Desktop.
Func Monoid
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 func = createMonoid({ | |
| indentity: x => x, | |
| // The code below can't run but conceptually valid | |
| // In Haskell or Purescript, it's easier to write Monoid instance | |
| semigroup: (f, g) => x => f(x).append(f(g)), | |
| }); | |
| // toStr :: Int -> String | |
| const toStr = x => parseInt(x) | |
| // replicateJihad :: Int -> String | |
| const replicateJihad = xs => 'Jihad'.repeat(xs) | |
| log(func.id) // x => x | |
| log(func.append(toStr, replicateJihad)(2)) // '2JihadJihad' | |
| log(func.append(replicateJihad, toStr)(2)) // 'JihadJihad2' | |
| log(func.append(replicateJihad, func.id)(2)) // 'JihadJihad' | |
| log(func.append(func.id, toStr)(2)) // '2' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment