Skip to content

Instantly share code, notes, and snippets.

@evaporei
Created September 13, 2019 19:50
Show Gist options
  • Select an option

  • Save evaporei/cd258ce397e058a898941f78572ff7a6 to your computer and use it in GitHub Desktop.

Select an option

Save evaporei/cd258ce397e058a898941f78572ff7a6 to your computer and use it in GitHub Desktop.
Category Theory - Monoid Strings and Arrays
String.prototype.mappend = function (x: String): String {
return this + x
}
String.prototype.mempty = function (): String {
return ""
}
console.log(
'String mappend:',
String("category").mappend(" ").mappend("theory")
) // "category theory"
console.log('String mempty:', String().mempty()) // ""
Array.prototype.mappend = function (x: Array): Array {
return this.concat(x)
}
Array.prototype.mempty = function (): Array {
return []
}
console.log('Array mappend:', Array(1, 2).mappend([3])) // [1, 2, 3]
console.log('Array mempty:', Array().mempty()) // []
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment