Skip to content

Instantly share code, notes, and snippets.

@KamilLelonek
Last active February 22, 2019 20:25
Show Gist options
  • Save KamilLelonek/c0466b668a686de3e3d4 to your computer and use it in GitHub Desktop.
Save KamilLelonek/c0466b668a686de3e3d4 to your computer and use it in GitHub Desktop.
An example of class monoid in Ruby
[1] (pry) main: 0> class AdditionMonoid
[1] (pry) main: 0* def identity
[1] (pry) main: 0* 0
[1] (pry) main: 0* end
[1] (pry) main: 0*
[1] (pry) main: 0* def operation(a, b)
[1] (pry) main: 0* a + b
[1] (pry) main: 0* end
[1] (pry) main: 0* end
=> :operation
[2] (pry) main: 0> addition_monoid = AdditionMonoid.new
=> #<AdditionMonoid:0x007fa324c037c0>
[3] (pry) main: 0>
[1, 2, 3].reduce(addition_monoid.identity, &addition_monoid.method(:operation))
=> 6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment