Last active
December 23, 2015 09:29
-
-
Save okram/6615306 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 com.tinkerpop.gremlin; | |
import java.util.function.Function; | |
/** | |
* @author Marko A. Rodriguez (http://markorodriguez.com) | |
*/ | |
public class Monad<S, E> { | |
private final Monad<?, S> start; | |
private final S value; | |
private final Function<S, E> function; | |
public Monad(Monad<?, S> start, Function<S, E> function) { | |
this.start = start; | |
this.value = null; | |
this.function = function; | |
} | |
public Monad(S value, Function<S, E> function) { | |
this.start = null; | |
this.value = value; | |
this.function = function; | |
} | |
public <T> Monad<E, T> map(Function<E, T> function) { | |
return new Monad<>(this, function); | |
} | |
public E get() { | |
return function.apply((this.value == null) ? this.start.get() : this.value); | |
} | |
} | |
/////////////////////////////////////////////// | |
public class MonadTest extends TestCase { | |
public void testMonad() { | |
System.out.println( | |
new Monad<String, Integer>("marko", String::length) | |
.map(x -> x.toString() + "1") | |
.map(x -> x.length()) | |
.map(x -> x.floatValue()).get()); | |
} | |
} | |
// 2.0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment