Skip to content

Instantly share code, notes, and snippets.

@justinhj
Created June 12, 2019 16:32
Show Gist options
  • Select an option

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

Select an option

Save justinhj/edaf1063d84167325466c863f5337ed1 to your computer and use it in GitHub Desktop.
Monoid composition
// Tuples, integers and maps all have monoid
// instances we can easily combine them
@ val data = "Hello Hello This is some data data data yo"
data: String = "Hello Hello This is some data data data yo"
@ def step(word: String) = (1, word.length, Map(word -> 1))
defined function step
@ def run(data: String) = data.split(" ").toList.map(step).combineAll
defined function run
@ run(data)
res54: (Int, Int, Map[String, Int]) = (9, 34, Map("is" -> 1, "This" -> 1, "data" -> 3, "yo" -> 1, "Hello" -> 2, "some" -> 1))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment