Sync
This commit is contained in:
parent
6f050a029e
commit
776bba907c
3887 changed files with 59894 additions and 7280 deletions
|
|
@ -1,19 +1,19 @@
|
|||
import std.stdio, std.algorithm, std.traits;
|
||||
|
||||
auto fs(alias f)(in int[] s) /*pure nothrow*/
|
||||
auto fs(alias f)(in int[] s) pure nothrow
|
||||
if (isCallable!f && ParameterTypeTuple!f.length == 1) {
|
||||
return map!f(s);
|
||||
return s.map!f;
|
||||
}
|
||||
|
||||
int f1(in int x) pure nothrow { return x * 2; }
|
||||
int f2(in int x) pure nothrow { return x ^^ 2; }
|
||||
|
||||
alias fs!f1 fsf1;
|
||||
alias fs!f2 fsf2;
|
||||
alias fsf1 = fs!f1;
|
||||
alias fsf2 = fs!f2;
|
||||
|
||||
void main() {
|
||||
foreach (d; [[0, 1, 2, 3], [2, 4, 6, 8]]) {
|
||||
writeln(fsf1(d));
|
||||
writeln(fsf2(d));
|
||||
foreach (const d; [[0, 1, 2, 3], [2, 4, 6, 8]]) {
|
||||
d.fsf1.writeln;
|
||||
d.fsf2.writeln;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,43 +1,68 @@
|
|||
import java.util.Arrays;
|
||||
import java.util.function.BiFunction;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.IntUnaryOperator;
|
||||
import java.util.function.UnaryOperator;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
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;
|
||||
}
|
||||
@FunctionalInterface
|
||||
public interface PartialApplication<INPUT1, INPUT2, OUTPUT> extends BiFunction<INPUT1, INPUT2, OUTPUT> {
|
||||
// Original method fs(f, s).
|
||||
public static int[] fs(IntUnaryOperator f, int[] s) {
|
||||
return Arrays.stream(s)
|
||||
.parallel()
|
||||
.map(f::applyAsInt)
|
||||
.toArray()
|
||||
;
|
||||
}
|
||||
|
||||
// Curried method fs(f).apply(s),
|
||||
// necessary for partial application.
|
||||
static Function<Integer[], Integer[]> fs(Function<Integer, Integer> f) {
|
||||
return s -> fs(f, s);
|
||||
}
|
||||
// Currying method f.apply(a).apply(b),
|
||||
// in lieu of f.apply(a, b),
|
||||
// necessary for partial application.
|
||||
public default Function<INPUT2, OUTPUT> apply(INPUT1 input1) {
|
||||
return input2 -> apply(input1, input2);
|
||||
}
|
||||
|
||||
static Function<Integer, Integer> f1 = i -> i * 2;
|
||||
// Original method fs turned into a partially-applicable function.
|
||||
public static final PartialApplication<IntUnaryOperator, int[], int[]> fs = PartialApplication::fs;
|
||||
|
||||
static Function<Integer, Integer> f2 = i -> i * i;
|
||||
public static final IntUnaryOperator f1 = i -> i + i;
|
||||
|
||||
static Function<Integer[], Integer[]> fsf1 = fs(f1); // Partial application.
|
||||
public static final IntUnaryOperator f2 = i -> i * i;
|
||||
|
||||
static Function<Integer[], Integer[]> fsf2 = fs(f2);
|
||||
public static final UnaryOperator<int[]> fsf1 = fs.apply(f1)::apply; // Partial application.
|
||||
|
||||
public static void main(String[] args) {
|
||||
Integer[][] sequences = {
|
||||
{ 0, 1, 2, 3 },
|
||||
{ 2, 4, 6, 8 },
|
||||
};
|
||||
public static final UnaryOperator<int[]> fsf2 = fs.apply(f2)::apply;
|
||||
|
||||
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)));
|
||||
}
|
||||
}
|
||||
public static void main(String... args) {
|
||||
int[][] sequences = {
|
||||
{0, 1, 2, 3},
|
||||
{2, 4, 6, 8},
|
||||
};
|
||||
|
||||
Arrays.stream(sequences)
|
||||
.parallel()
|
||||
.map(array ->
|
||||
Stream.of(
|
||||
array,
|
||||
fsf1.apply(array),
|
||||
fsf2.apply(array)
|
||||
)
|
||||
.parallel()
|
||||
.map(Arrays::toString)
|
||||
.toArray()
|
||||
)
|
||||
.map(array ->
|
||||
String.format(
|
||||
String.join("\n",
|
||||
"array: %s",
|
||||
" fsf1(array): %s",
|
||||
" fsf2(array): %s"
|
||||
),
|
||||
array
|
||||
)
|
||||
)
|
||||
.forEachOrdered(System.out::println)
|
||||
;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,31 @@
|
|||
:- object(partial_functions).
|
||||
|
||||
:- public(show/0).
|
||||
|
||||
show :-
|
||||
% create the partial functions
|
||||
create_partial_function(f1, PF1),
|
||||
create_partial_function(f2, PF2),
|
||||
% apply the partial functions
|
||||
Sequence1 = [0,1,2,3],
|
||||
call(PF1, Sequence1, PF1Sequence1), output_results(PF1, Sequence1, PF1Sequence1),
|
||||
call(PF2, Sequence1, PF2Sequence1), output_results(PF2, Sequence1, PF2Sequence1),
|
||||
Sequence2 = [2,4,6,8],
|
||||
call(PF1, Sequence2, PF1Sequence2), output_results(PF1, Sequence2, PF1Sequence2),
|
||||
call(PF2, Sequence2, PF2Sequence2), output_results(PF2, Sequence2, PF2Sequence2).
|
||||
|
||||
create_partial_function(Closure, fs(Closure)).
|
||||
|
||||
output_results(Function, Input, Output) :-
|
||||
write(Input), write(' -> '), write(Function), write(' -> '), write(Output), nl.
|
||||
|
||||
fs(Closure, Arg1, Arg2) :-
|
||||
meta::map(Closure, Arg1, Arg2).
|
||||
|
||||
f1(Value, Double) :-
|
||||
Double is 2*Value.
|
||||
|
||||
f2(Value, Square) :-
|
||||
Square is Value*Value.
|
||||
|
||||
:- end_object.
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
| ?- partial_functions::show.
|
||||
[0,1,2,3] -> fs(f1) -> [0,2,4,6]
|
||||
[0,1,2,3] -> fs(f2) -> [0,1,4,9]
|
||||
[2,4,6,8] -> fs(f1) -> [4,8,12,16]
|
||||
[2,4,6,8] -> fs(f2) -> [4,16,36,64]
|
||||
yes
|
||||
Loading…
Add table
Add a link
Reference in a new issue