Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.fpinjava.functions.exercise02_01;

interface BiFunctionInterface<T, U, V> {
V biApply(T t, U u);

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,51 @@

public class FunctionExamples {

public static final Function<Integer, Integer> triple = new Function<Integer, Integer>() {
public static final Function<Integer, Integer> triple() {
return
new Function<Integer, Integer>() {

@Override
public Integer apply(Integer arg) {
return arg * 3;
@Override
public Integer apply(Integer arg) {
return arg * 3;
}
};
}
};

public static final Function<Integer, Integer> square = new Function<Integer, Integer>() {
public static final Function<Integer, Integer> triple2 = arg -> arg * 3;

@Override
public Integer apply(Integer arg) {
return arg * arg;
public static final Function<Integer, Integer> square() {
return new Function<Integer, Integer>() {

@Override
public Integer apply(Integer arg) {
return arg * arg;
}
};
}

public static final Function<Integer, Integer> square2 = arg -> arg * arg;

public static final Function<Integer, Integer> compose(final Integer x, final Integer y) {
return arg -> x + y;
}


public static final Function<Integer, Integer> compose(final Function<Integer, Integer> x, final Function<Integer, Integer> y) {
return arg -> x.apply(y.apply(arg));
}
};

public static final Function<Integer, Integer> compose(final Function<Integer, Integer> f1,
final Function<Integer, Integer> f2) {
throw new RuntimeException("To be implemented.");
}

//public static final TriFunction<Function<>, Function, Function, Integer> compose2 = f1 -> f2 -> arg -> f1.apply(f2.apply(arg));
public static final TriFunction<Function<Integer, Integer>, Function<Integer, Integer>, Integer, Integer> compose3 = (f1, f2, arg) -> f1.apply(f2.apply(arg));

public static final TriFunction<Integer, Integer, Integer, Integer> compose4 = (f1, f2, arg) -> f1 + 0;


public static final Function<Integer, Integer> compose2(Function f1, Function f2) { return arg -> triple2.apply(square2.apply(arg));};

public static final Function<Integer, Integer> compose201(Function<Integer, Integer> f1, Function<Integer, Integer> f2) { return arg -> f1.apply(f2.apply(arg));};

public static final TriFunction<Function<Integer, Integer>, Function<Integer, Integer>, Integer, Integer> compose301 = (f1, f2, arg) -> f1.apply(f2.apply(arg));

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.fpinjava.functions.exercise02_01;

public class HigherComposeOptions <T, U, V> {

//using params
public static <T, U, V> Function<T, V> higherCompose1(Function<U, V> f1, Function<T, U> f2) {
return arg -> f1.apply(f2.apply(arg));
}

//using params of the lambda
public final TriFunction<Function<U, V>, Function<T, U>, T, V>
higherCompose3 = (f1, f2, arg) -> f1.apply(f2.apply(arg));


//currying
public static <T, U, V> Function<Function<U, V>, Function<Function<T, U>, Function<T, V>>> higherCompose3() {
return f1 -> f2 -> arg -> f1.apply(f2.apply(arg));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.fpinjava.functions.exercise02_01;

interface TriFunction<T, U, V, R>{
R triApply(T t, U u, V v);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

public class FunctionExamples {

public static final Function<Integer, Integer> triple = x -> x * 3;
public static final Function<Integer, Integer> triple = x -> x * 3;

public static final Function<Integer, Integer> square = x -> x * x;
public static final Function<Integer, Integer> square = x -> x * x;

public static final Function<Integer, Integer> compose(final Function<Integer, Integer> f1,
final Function<Integer, Integer> f2) {
throw new RuntimeException("To be implemented.");
}
public static final Function<Integer, Integer> compose(final Function<Integer, Integer> f1,
final Function<Integer, Integer> f2) {
return x -> f1.apply(f2.apply(x));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@

public class FunctionExamples {

public static final Function<Integer, Function<Integer, Integer>> add = null; // To be implemented
//why does x is highlighted by intelliJ as purple with under score?
public static final Function<Integer, Function<Integer, Integer>> add = x -> y -> x + y;
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why does x is highlighted by intelliJ as purple with under score?


public static final BinaryOperator add2 = null; // To be implemented
public static final BinaryOperator add2 = x -> y -> x + y;

public static final BinaryOperator mult = null; // To be implemented
public static final BinaryOperator mult = x -> y -> x * y;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public class FunctionExamples {
public static final Function<Integer, Integer> square = x -> x * x;

public static final Function<Function<Integer, Integer>, Function<Function<Integer, Integer>,
Function<Integer, Integer>>> compose = null; // To be implemented
Function<Integer, Integer>>> compose = x -> y -> z -> x.apply(y.apply(z));

public static final Function<Integer, Integer> f = compose.apply(square).apply(triple);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,44 @@ public interface Function<T, U> {

U apply(T arg);

static <T, U, V> Function<Function<U, V>, Function<Function<T, U>, Function<T, V>>> higherCompose() {
throw new RuntimeException("To be implemented.");


static <T, U, V> Function<Function<U, V>, Function<Function<T, U>, Function<T, V>>> higherCompose(){
return f1 -> f2 -> arg -> f1.apply(f2.apply(arg));
}


static Function<Function<String, Boolean>, Function<Function<Integer, String>, Function<Integer, Boolean>>> higherCompose4(){
return f1 -> f2 -> arg -> f1.apply(f2.apply(arg));
}

//F0 - Boolean T
//F1 - Integer U
//F2 - Boolean V
//Arg - Character W

static <T> Function<Function<T, T>, Function<T, T>> higherCompose2(){
return f1 -> arg -> f1.apply(arg);
}

static <T> Function<T, Integer> higherCompose3(){
final Function<T, Integer> tIntegerFunction = arg -> 0;
return tIntegerFunction;
}

// static <T, U, V> Function<> higherCompose2() {
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was much easier to write the lambda, but its harder to find the type.

// return x -> y -> z -> x.apply(y.apply(z));
// }
//
// static <T, U, V> Function<T, Function<>> higherCompose2() {
// return x -> y -> z -> x.apply(y.apply(z));
// }
//
// static <T, U, V> Function<Function<>, Function<>> higherCompose2() {
// return x -> y -> z -> x.apply(y.apply(z));
// }
//
// static <T, U, V> Function<Function<>, Function<Function<>, Function<>>> higherCompose2() {
// return x -> y -> z -> x.apply(y.apply(z));
// }
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ static <T, U, V> Function<Function<U, V>, Function<Function<T, U>, Function<T, V
}

static <T, U, V> Function<Function<T, U>, Function<Function<U, V>, Function<T, V>>> higherAndThen() {
throw new RuntimeException("To be implemented.");
return f -> g -> x -> g.apply(f.apply(x));
}
}