Skip to content

Instantly share code, notes, and snippets.

View deepakmehra10's full-sized avatar
🎯
Focusing

Deepak Mehra deepakmehra10

🎯
Focusing
View GitHub Profile
Lazy<String> name = Lazy.of(() -> "Deepak");
System.out.println(name.isEvaluated()); //false
System.out.println(name.get()); //Deepak
System.out.println(name.isEvaluated()); //True
System.out.println(name.get()); //Deepak, from cache
CheckedFunction2<Integer,Integer, Integer> divide = (a, b) -> a/b ;
Try<Integer> failure = Try.of(() -> divide.apply(5, 0));
System.out.println(failure);
Try<Integer> success = Try.of(() -> divide.apply(5, 1));
System.out.println(success);
Option<String> value = Option.some("Deepak");
Option<String> value1 = Option.none();
System.out.println(value.map(a -> a).getOrElse("Default name")); // Deepak
System.out.println(value1.map(a -> a).getOrElse("Default name")); // Default name
//memoization
Function2<Integer,Integer,Integer> multiply = (a,b) -> (a *b);
Function2<Integer, Integer, Integer> memoized = multiply.memoized();
Integer value = memoized.apply(2, 3);
System.out.println(value);
//currying
Function1<Integer, Function1<Integer, Function1<Integer, Integer>>> curried = fourArgFunction.curried().apply(2);
System.out.println(curried.apply(2).apply(1).apply(1));
//partial application
Function4<Integer, Integer, Integer, Integer, Integer> fourArgFunction = (a, b, c, d) -> a + b + c + d;
Function2<Integer,Integer,Integer> result = fourArgFunction.apply(4,5);
System.out.println(result.apply(1,2));
//lifting
Function2<Integer, Integer, Integer> divide = (first, second) -> first / second;
Function2<Integer, Integer, Option<Integer>> lift = Function2.lift(divide);
// None
System.out.println(lift.apply(1, 0));
//Some value
System.out.println(divide.apply(1, 1));
String number = Match(2).of(
Case($(1),"one"),
Case($(2),"two"),
Case($(),"default")
);
//conditional pattern
String conditional = Match(1).of(
Case($(is(1)),i -> "one" +i),
Case($(is(2)),i -> "two" + i),
Case($(),"default")
);
String conditional = Match(1).of(
Case($(is(1)),i -> "one" +i),
Case($(is(2)),i -> "two" + i),
Case($(),"default")
);