tasks a-s
This commit is contained in:
parent
47bf37c096
commit
b83f433714
12433 changed files with 156208 additions and 123 deletions
|
|
@ -0,0 +1,43 @@
|
|||
import java.util.Arrays;
|
||||
import java.util.function.Function;
|
||||
|
||||
public class PartialApplication {
|
||||
// Original method fs(f, s).
|
||||
static Integer[] fs(Function<Integer, Integer> f, Integer[] s) {
|
||||
Integer[] r = new Integer[s.length];
|
||||
for (int i = 0; i < s.length; i++)
|
||||
r[i] = f.apply(s[i]);
|
||||
return r;
|
||||
}
|
||||
|
||||
// Curried method fs(f).apply(s),
|
||||
// necessary for partial application.
|
||||
static Function<Integer[], Integer[]> fs(Function<Integer, Integer> f) {
|
||||
return s -> fs(f, s);
|
||||
}
|
||||
|
||||
static Function<Integer, Integer> f1 = i -> i * 2;
|
||||
|
||||
static Function<Integer, Integer> f2 = i -> i * i;
|
||||
|
||||
static Function<Integer[], Integer[]> fsf1 = fs(f1); // Partial application.
|
||||
|
||||
static Function<Integer[], Integer[]> fsf2 = fs(f2);
|
||||
|
||||
public static void main(String[] args) {
|
||||
Integer[][] sequences = {
|
||||
{ 0, 1, 2, 3 },
|
||||
{ 2, 4, 6, 8 },
|
||||
};
|
||||
|
||||
for (Integer[] array : sequences) {
|
||||
System.out.printf(
|
||||
"array: %s\n" +
|
||||
" fsf1(array): %s\n" +
|
||||
" fsf2(array): %s\n",
|
||||
Arrays.toString(array),
|
||||
Arrays.toString(fsf1.apply(array)),
|
||||
Arrays.toString(fsf2.apply(array)));
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue