Last active
July 5, 2016 04:54
-
-
Save waliahimanshu/4d062cce0d7b7bcc9a40801768045d3e to your computer and use it in GitHub Desktop.
RxJava Functions and high order functions (RxJava8)
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
package com.company; | |
import java.util.function.BiFunction; | |
import java.util.function.Function; | |
import java.util.function.Supplier; | |
public class Main { | |
public static void main(String[] args) { | |
//Reactive manifesto 1) event driven 2) scalable 3) reselient 4) responsive | |
// FunctionAsFirstClassCitizens();//function used as variables, instance variables | |
// FunctionPassedAsParameters(); | |
// HighOrderFunction(); a function that returns another function | |
} | |
private static void HighOrderFunction() { | |
Supplier<String> myFunc = CreateCombineAndTransform("Hello"," World",(s)-> s.toUpperCase()); | |
System.out.println(myFunc.get()); | |
} | |
private static Supplier<String> CreateCombineAndTransform( | |
final String a, | |
final String b, | |
final Function<String, String> transformer) { | |
return () -> { | |
String result = null; | |
if (transformer != null) { | |
result = transformer.apply(a) + transformer.apply(b); | |
} | |
return result; | |
}; | |
} | |
private static void FunctionPassedAsParameters() { | |
System.out.println(concatAndTransform("Hello", " World", | |
(s) -> { | |
return s.toUpperCase(); | |
})); | |
Function<String, String> functionToLower = (s) -> { | |
return s.toLowerCase(); | |
}; | |
System.out.println(concatAndTransform("Hello", " World", functionToLower)); | |
} | |
private static void FunctionAsFirstClassCitizens() { | |
BiFunction<String, String, String> concatFunction = (s, s2) -> { | |
return s + s2; | |
}; | |
System.out.println(concatFunction.apply("Hello", " World!")); | |
concatFunction = Main::concatString; | |
System.out.println(concatFunction.apply("What", " Is Going On")); | |
Main main = new Main(); | |
concatFunction = main::concatString2; | |
System.out.println(concatFunction.apply("Why", " God")); | |
} | |
private static String concatString(String a, String b) { | |
return a + b; | |
} | |
String concatString2(String s, String b) { | |
return s + b; | |
} | |
private static String concatAndTransform(String a, String b, Function<String, String> stringTrasnform) { | |
if (stringTrasnform != null) { | |
a = stringTrasnform.apply(a); | |
b = stringTrasnform.apply(b); | |
} | |
return a + b; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment