Skip to content

Instantly share code, notes, and snippets.

@justinhj
Created June 12, 2019 18:13
Show Gist options
  • Select an option

  • Save justinhj/83adc80b4fbffc44497dcbe1ccedf433 to your computer and use it in GitHub Desktop.

Select an option

Save justinhj/83adc80b4fbffc44497dcbe1ccedf433 to your computer and use it in GitHub Desktop.
Comonad
// Comonad is the dual of Monad
// example for non empty list, extract is the head of the list
// coflatMap gives you successive lists from the whole list, the tail, the tail of that
// and you must return a single value for each list...
@ Comonad[NonEmptyList].extract(NonEmptyList.of(1,2,3))
res26: Int = 1
@ Comonad[NonEmptyList].extract(NonEmptyList.of(1))
res27: Int = 1
@ Comonad[NonEmptyList].coflatMap(NonEmptyList.of(1,2,3,4,5,6,7,8,9,10)){_.head}
res28: NonEmptyList[Int] = NonEmptyList(1, List(2, 3, 4, 5, 6, 7, 8, 9, 10))
@ Comonad[NonEmptyList].coflatMap(NonEmptyList.of(1,2,3,4,5,6,7,8,9,10)){_.size}
res29: NonEmptyList[Int] = NonEmptyList(10, List(9, 8, 7, 6, 5, 4, 3, 2, 1))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment