1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | public class StateMonad { public static <T, S> Function<S, Tuple2<T, S>> unit(T value) { return s -> Tuple.of(value, s); } public static <T1, T2, S> Function<S, Tuple2<T2, S>> bind(Function<S, Tuple2<T1, S>> input, Function<T1, Function<S, Tuple2<T2, S>>> transform) { return s -> { Tuple2<T1, S> result = input.apply(s); return transform.apply(result._1).apply(result._2); }; } public static void main(String[] args) { Function<String, Function<String, Function<State, Tuple2<String, State>>>> transform = prefix -> value -> s -> Tuple .of(prefix + value, new State(s.getValue() + value.length())); Function<State, Tuple2<String, State>> m1 = unit("Hello"); Function<State, Tuple2<String, State>> m2 = bind(m1, transform.apply("1")); Function<State, Tuple2<String, State>> m3 = bind(m2, transform.apply("2")); Tuple2<String, State> result = m3.apply(new State(0)); System.out.println(result); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | public class State { private final int value; public State(final int value) { this.value = value; } public int getValue() { return value; } @Override public String toString() { return "State{" + "value=" + value + '}'; } } |
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) | Powered by Discuz! 7.0.0 |