Skip to content

Instantly share code, notes, and snippets.

@okram
Last active December 23, 2015 09:29
Show Gist options
  • Save okram/6615306 to your computer and use it in GitHub Desktop.
Save okram/6615306 to your computer and use it in GitHub Desktop.
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