This file contains 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
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 |
This file contains 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
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); |
This file contains 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
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 |
This file contains 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
//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); |
This file contains 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
//currying | |
Function1<Integer, Function1<Integer, Function1<Integer, Integer>>> curried = fourArgFunction.curried().apply(2); | |
System.out.println(curried.apply(2).apply(1).apply(1)); | |
This file contains 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
//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)); | |
This file contains 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
//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)); |
This file contains 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
String number = Match(2).of( | |
Case($(1),"one"), | |
Case($(2),"two"), | |
Case($(),"default") | |
); |
This file contains 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
//conditional pattern | |
String conditional = Match(1).of( | |
Case($(is(1)),i -> "one" +i), | |
Case($(is(2)),i -> "two" + i), | |
Case($(),"default") | |
); |
This file contains 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
String conditional = Match(1).of( | |
Case($(is(1)),i -> "one" +i), | |
Case($(is(2)),i -> "two" + i), | |
Case($(),"default") | |
); |