Last active
          February 22, 2019 20:25 
        
      - 
      
- 
        Save KamilLelonek/c0466b668a686de3e3d4 to your computer and use it in GitHub Desktop. 
    An example of class monoid in Ruby
  
        
  
    
      This file contains hidden or 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