Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Human noWeapon = new Human(null);
- Human noMag = new Human(new Weapon(null));
- Human hasAll = new Human(new Weapon(new Magazine(2)));
- Human noWeapon = new Human(null);
- Either <String, Human> either2 = new Right <String, Human>(noWeapon);
- Right <String, Human> either2_right = (Right<String, Human>) either2;
- either2_right.getRight().getWeapon().getMag().getCount();
- abstract class Either<A, B> { }
- class Left<A, B> extends Either<A, B> {
- public A left_value;
- public Left(A a)
- {
- left_value = a;
- }
- public A getLeft(){
- return this.left_value;
- }
- public <B2> Either<A,B2> flatMap(final Function<B,Either<A,B2>> f){
- return (Either<A,B2>)this;
- }
- public <B2> Either<A,B2> map(final Function<B,B2> f){
- return (Either<A,B2>)this;
- }
- }
- class Right<A, B> extends Either<A, B> {
- public B right_value;
- public Right(B b)
- {
- right_value = b;
- }
- public B getRight(){
- return this.right_value;
- }
- public <B2> Either<A,B2> flatMap(final Function<B,Either<A,B2>> f){
- return f.apply(right_value);
- }
- public <B2> Either<A,B2> map(final Function<B,B2> f){
- return new Right(f.apply(right_value));
- }
- }
- class Human {
- Weapon w;
- public Human(Weapon w)
- {
- this.w = w;
- }
- public Weapon getWeapon()
- {
- return w;
- }
- }
- class Weapon {
- Magazine m;
- public Weapon(Magazine m)
- {
- this.m = m;
- }
- public Magazine getMag()
- {
- return m;
- }
- }
- class Magazine {
- private int count;
- public Magazine(int c)
- {
- count = c;
- }
- public int getCount()
- {
- return count;
- }
- }
Add Comment
Please, Sign In to add comment