Skip to content

Instantly share code, notes, and snippets.

@dewey92
Last active April 4, 2019 11:08
Show Gist options
  • Select an option

  • Save dewey92/365fce11a9a6347e45c58821a32f2681 to your computer and use it in GitHub Desktop.

Select an option

Save dewey92/365fce11a9a6347e45c58821a32f2681 to your computer and use it in GitHub Desktop.
Func Monoid
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