Skip to content

Instantly share code, notes, and snippets.

@guilleiguaran
Forked from KamilLelonek/custom_monoid.rb
Created February 22, 2019 20:25
Show Gist options
  • Save guilleiguaran/d6dedeaf9b6f52d2ec4169ff06a028dc to your computer and use it in GitHub Desktop.
Save guilleiguaran/d6dedeaf9b6f52d2ec4169ff06a028dc 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