-
-
Save guilleiguaran/d6dedeaf9b6f52d2ec4169ff06a028dc to your computer and use it in GitHub Desktop.
An example of class monoid in Ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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