Last active
May 25, 2018 10:38
-
-
Save MarkusPfundstein/c66acecb7b3df2a16022048709433ec9 to your computer and use it in GitHub Desktop.
Product Comonad example
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 Prod = t => ({ | |
extract : () => t[1], | |
extend : f => Prod([t[0], f([t[0], t[1]])]), | |
map : f => Prod([t[0], f(t[1])]), | |
duplicate : () => Prod([t[0], Prod(t)]) | |
}); | |
Prod.of = function(t) { | |
return Prod(t); | |
}; | |
const Student = { | |
id: 5, | |
name: "markus", | |
surname: "pfundstein", | |
domain: "hotbox.com" | |
} | |
const result = | |
Prod.of([Student, Student]) | |
.map(({name, surname, domain}) => `${name}.${surname}@${domain}`) | |
.extend(([student, mail]) => `${student.id}, ${mail}`) | |
.extract(); | |
console.log(result); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is a good example to get a first intuition what a comonad is. However, there is a little mistake in your monadic
of
definition.of
is intended to put a value in a minimal context. For a 2-tuple the only sane definition isof = x => [x, x]
, which simplifiesProd.of([Student, Student])
intoProd.of(Student)
.