Last active
May 17, 2018 11:55
-
-
Save soverby/ae549f9b63f4a1f6cae49ce1db5a8804 to your computer and use it in GitHub Desktop.
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
import java.util.Date; | |
import java.util.function.Function; | |
public class MultiParameterFunction { | |
// Bad | |
@FunctionalInterface | |
public interface TriFunction<T, U, V, R> { | |
public R customMethod(T t, U u, V v); | |
} | |
public static void main(String[] args) { | |
String stringParam = "Blah"; | |
Long longParam = new Long("32.0"); | |
Date date = new Date(); | |
// Don't do this: | |
TriFunction<String, Long, Date, String> someTriFunction = (stringp, longp, datep) -> | |
stringp.concat(longp.toString()).concat(datep.toString()); | |
someTriFunction.customMethod(stringParam, longParam, date); | |
// Do this instead. BTW this is an example of a higher order function also known as | |
// as the partially applied function pattern. | |
Function<String, Function<Long, Function<Date, String>>> betterFunction = | |
stringp -> longp -> datep -> stringp.concat(longp.toString()).concat(datep.toString()); | |
betterFunction.apply(stringParam).apply(longParam).apply(date); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment