Skip to content

Instantly share code, notes, and snippets.

View ojitha's full-sized avatar

Ojitha ojitha

View GitHub Profile
@ojitha
ojitha / mondads simple.java
Last active November 23, 2019 06:27
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;
@ojitha
ojitha / Java stream parallel reduce.java
Last active November 23, 2019 06:27
This java example shows how to use the parallel stream to do the reduce function.
import java.math.BigInteger;
import java.util.Arrays;
import java.util.stream.IntStream;
//var r =IntStream.rangeClosed(1,5)
// .reduce(1,(x,y) -> x * y);
// //.forEach(System.out::println);
static BigInteger factorial(int n){
return IntStream.rangeClosed(1, n)
@ojitha
ojitha / Java stream exception.java
Last active November 23, 2019 06:29
This example shows how to wrap a Java lambdafunction to avoide any exception.
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Stream;
@FunctionalInterface
interface ExFunction<E, F> {
F apply (E e) throws Throwable;
static <E, F> Function<E, Optional<F>> wrap (ExFunction<E, F> op){
@ojitha
ojitha / Java Collectors groupby.java
Last active November 23, 2019 06:31
This example shows how to group employees by their departments. The intention of this example, to show the use of the <b>Collectors.groupingBy</b> functionality.
/*
This example shows how to group employees by their departments.
The intention of this example, to show the use of the <b>Collectors.groupingBy</b>
functionality.
*/
import java.util.List;
import java.util.stream.Collectors;
class Dept {