Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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