Last active
          May 3, 2017 22:35 
        
      - 
      
- 
        Save vladymir/45a97480ee8eb946404629328d67783d to your computer and use it in GitHub Desktop. 
  
    
      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
    
  
  
    
  | package monoids; | |
| public abstract class Monoid<T> { | |
| public abstract T combine(T one, T other); | |
| public abstract T identity(); | |
| public T fold(Iterable<T> elements) { | |
| T result = identity(); | |
| for (T i : elements) { | |
| result = combine(result, i); | |
| } | |
| return result; | |
| } | |
| } | 
  
    
      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
    
  
  
    
  | package monoids; | |
| import java.util.Arrays; | |
| import java.util.List; | |
| public class NumberAddMonoid extends Monoid<Integer>{ | |
| @Override | |
| public Integer combine(Integer one, Integer other) { | |
| return one + other; | |
| } | |
| @Override | |
| public Integer identity() { | |
| return 0; | |
| } | |
| public static void main(String[] args) { | |
| NumberAddMonoid numMonoid = new NumberAddMonoid(); | |
| Integer[] ints = new Integer[] {1, 2, 3, 4, 5}; | |
| List<Integer> nums = Arrays.asList(ints); | |
| System.out.println(numMonoid.fold(nums)); | |
| } | |
| } | 
  
    
      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
    
  
  
    
  | package monoids; | |
| import java.util.ArrayList; | |
| import java.util.List; | |
| import java.util.Optional; | |
| import java.util.Random; | |
| public class OptionalMonoid<T> extends Monoid<Optional<T>> { | |
| final Monoid<T> identity; | |
| public OptionalMonoid(Monoid<T> identity) { | |
| this.identity = identity; | |
| } | |
| @Override | |
| public Optional<T> combine(Optional<T> one, Optional<T> other) { | |
| if(one.isPresent() && other.isPresent()) | |
| return Optional.of(identity.combine(one.get(), other.get())); | |
| else | |
| return one.isPresent() ? one : other; | |
| } | |
| @Override | |
| public Optional<T> identity() { | |
| return Optional.empty(); | |
| } | |
| public static OptionalMonoid<Integer> intOptMonoid() { | |
| return new OptionalMonoid<>(new NumberAddMonoid()); | |
| } | |
| public static void main(String[] args) { | |
| OptionalMonoid<Integer> optIntMonoid = OptionalMonoid.intOptMonoid(); | |
| Random rand = new Random(); | |
| List<Optional<Integer>> opts = new ArrayList<>(); | |
| for(int i=0; i<10; i++) { | |
| int randnumber = rand.nextInt(2); | |
| if(randnumber == 0) | |
| opts.add(Optional.ofNullable(10)); | |
| else | |
| opts.add(Optional.ofNullable(null)); | |
| } | |
| System.out.println(opts); | |
| System.out.println(optIntMonoid.fold(opts)); | |
| //System.out.println(optIntMonoid.combine(Optional.ofNullable(12), Optional.ofNullable(null))); | |
| } | |
| } | 
  
    
      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
    
  
  
    
  | 15 | 
  
    
      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
    
  
  
    
  | [ Optional[10], | |
| Optional[10], | |
| Optional[10], | |
| Optional[10], | |
| Optional.empty, | |
| Optional[10], | |
| Optional[10], | |
| Optional.empty, | |
| Optional[10], | |
| Optional[10] | |
| ] | |
| Optional[80] | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment