Skip to content

Commit 3a0dc04

Browse files
Simplified PartialFunction towards removing Value.get(), see #2557 (#2669)
1 parent 7c167af commit 3a0dc04

File tree

2 files changed

+0
-100
lines changed

2 files changed

+0
-100
lines changed

src/main/java/io/vavr/PartialFunction.java

Lines changed: 0 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020

2121
import io.vavr.control.Option;
2222

23-
import java.util.function.Function;
24-
2523
/**
2624
* Represents a partial function T -> R that is not necessarily defined for all input values of type T.
2725
* The caller is responsible for calling the method isDefinedAt() before this function is applied to the value.
@@ -43,62 +41,6 @@ public interface PartialFunction<T, R> extends Function1<T, R> {
4341
*/
4442
long serialVersionUID = 1L;
4543

46-
/**
47-
* Unlifts a {@code totalFunction} that returns an {@code Option} result into a partial function.
48-
* The total function should be side effect free because it might be invoked twice: when checking if the
49-
* unlifted partial function is defined at a value and when applying the partial function to a value.
50-
*
51-
* @param totalFunction the function returning an {@code Option} result.
52-
* @param <T> type of the function input, called <em>domain</em> of the function
53-
* @param <R> type of the function output, called <em>codomain</em> of the function
54-
* @return a partial function that is not necessarily defined for all input values of type T.
55-
*/
56-
static <T, R> PartialFunction<T, R> unlift(Function<? super T, ? extends Option<? extends R>> totalFunction) {
57-
return new PartialFunction<T, R>() {
58-
59-
private static final long serialVersionUID = 1L;
60-
61-
@Override
62-
public R apply(T t) {
63-
return totalFunction.apply(t).get();
64-
}
65-
66-
@Override
67-
public boolean isDefinedAt(T value) {
68-
return totalFunction.apply(value).isDefined();
69-
}
70-
71-
};
72-
}
73-
74-
/**
75-
* Factory method for creating a partial function that maps a given {@code Value} to its underlying value.
76-
* The partial function is defined for an input {@code Value} if and only if the input {@code Value} is not
77-
* empty. If the input {@code Value} is not empty, the partial function will return the underlying value of
78-
* the input {@code Value}.
79-
*
80-
* @param <T> type of the underlying value of the input {@code Value}.
81-
* @param <V> type of the function input, called <em>domain</em> of the function
82-
* @return a partial function that maps a {@code Value} to its underlying value.
83-
*/
84-
static <T, V extends Value<T>> PartialFunction<V, T> getIfDefined() {
85-
return new PartialFunction<V, T>() {
86-
87-
private static final long serialVersionUID = 1L;
88-
89-
@Override
90-
public T apply(V v) {
91-
return v.get();
92-
}
93-
94-
@Override
95-
public boolean isDefinedAt(V v) {
96-
return !v.isEmpty();
97-
}
98-
99-
};
100-
}
101-
10244
/**
10345
* Applies this function to the given argument and returns the result.
10446
*

src/test/java/io/vavr/PartialFunctionTest.java

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,9 @@
1919
package io.vavr;
2020

2121
import io.vavr.collection.HashMap;
22-
import io.vavr.collection.List;
23-
import io.vavr.control.Either;
2422
import io.vavr.control.Option;
2523
import org.junit.Test;
2624

27-
import java.util.function.Predicate;
28-
2925
import static org.assertj.core.api.Assertions.assertThat;
3026

3127
public class PartialFunctionTest {
@@ -42,42 +38,4 @@ public void shouldReturnNone() {
4238
assertThat(oneToOne).isEqualTo(Option.none());
4339
}
4440

45-
@Test
46-
public void shouldUnliftTotalFunctionReturningAnOption() {
47-
final Predicate<Number> isEven = n -> n.intValue() % 2 == 0;
48-
final Function1<Number, Option<String>> totalFunction = n -> isEven.test(n) ? Option.some("even") : Option.none();
49-
50-
final PartialFunction<Integer, CharSequence> partialFunction = PartialFunction.unlift(totalFunction);
51-
52-
assertThat(partialFunction.isDefinedAt(1)).isFalse();
53-
assertThat(partialFunction.isDefinedAt(2)).isTrue();
54-
assertThat(partialFunction.apply(2)).isEqualTo("even");
55-
}
56-
57-
@Test
58-
public void shouldNotBeDefinedAtLeft() {
59-
final Either<RuntimeException, Object> left = Either.left(new RuntimeException());
60-
61-
assertThat(PartialFunction.getIfDefined().isDefinedAt(left)).isFalse();
62-
}
63-
64-
@Test
65-
public void shouldBeDefinedAtRight() {
66-
Either<Object, Number> right = Either.right(42);
67-
68-
PartialFunction<Either<Object, Number>, Number> ifDefined = PartialFunction.getIfDefined();
69-
70-
assertThat(ifDefined.isDefinedAt(right)).isTrue();
71-
assertThat(ifDefined.apply(right)).isEqualTo(42);
72-
}
73-
74-
@Test
75-
public void shouldCollectSomeValuesAndIgnoreNone() {
76-
final List<Integer> evenNumbers = List.range(0, 10)
77-
.map(n -> n % 2 == 0 ? Option.some(n) : Option.<Integer>none())
78-
.collect(PartialFunction.getIfDefined());
79-
80-
assertThat(evenNumbers).containsExactly(0, 2, 4, 6, 8);
81-
}
82-
8341
}

0 commit comments

Comments
 (0)