Skip to content

Instantly share code, notes, and snippets.

@ojitha
Last active November 23, 2019 06:27
Show Gist options
  • Select an option

  • Save ojitha/8f9d5acddd91510050e5e63a5649284e to your computer and use it in GitHub Desktop.

Select an option

Save ojitha/8f9d5acddd91510050e5e63a5649284e to your computer and use it in GitHub Desktop.
This is show how the Java Monad is composed of.
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