Last active
November 23, 2019 06:27
-
-
Save ojitha/8f9d5acddd91510050e5e63a5649284e to your computer and use it in GitHub Desktop.
This is show how the Java Monad is composed of.
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
| import java.util.ArrayList; | |
| import java.util.List; | |
| import java.util.function.Function; | |
| interface Functor<T, F extends Functor<?,?>> { | |
| <R> F map (Function<T, R> f); | |
| } | |
| class FList<T> implements Functor<T, FList<?>> { | |
| private final List<T> list; | |
| FList(List<T> value){ | |
| this.list = List.copyOf(value); | |
| } | |
| @Override | |
| public <R> FList<?> map(Function<T, R> f) { | |
| var result = new ArrayList<R>(this.list.size()); | |
| for (T t: list){ | |
| result.add(f.apply(t)); | |
| } | |
| return new FList<>(result); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment