Skip to content

Commit 9c1b446

Browse files
committed
First attempt at functions that throw exceptions
1 parent d0dc441 commit 9c1b446

File tree

10 files changed

+120
-2
lines changed

10 files changed

+120
-2
lines changed

core/src/main/java/fj/F1Functions.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -824,4 +824,14 @@ static public <A, B> ArrayList<B> mapJ(final F<A, B> f, final ArrayList<A> as) {
824824
return new ArrayList<B>(iterableStream(as).map(f).toCollection());
825825
}
826826

827+
static public <A, B> F<A, Try<B>> toF1(final Try1<A, B> t) {
828+
return a -> {
829+
try {
830+
return Try.trySuccess(t.f(a));
831+
} catch (Exception e) {
832+
return Try.<B>tryFail(e);
833+
}
834+
};
835+
}
836+
827837
}

core/src/main/java/fj/F2Functions.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,4 +346,15 @@ public P3<Stream<Tree<C>>, C, Stream<Tree<C>>> f(final P3<Stream<Tree<A>>, A, St
346346
};
347347
}
348348

349+
350+
static public <A, B, C> F2<A, B, Try<C>> toF2(final Try2<A, B, C> t) {
351+
return (a, b) -> {
352+
try {
353+
return Try.trySuccess(t.f(a, b));
354+
} catch (Exception e) {
355+
return Try.<C>tryFail(e);
356+
}
357+
};
358+
}
359+
349360
}

core/src/main/java/fj/F3Functions.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,17 @@
44
* Created by MarkPerry on 6/04/2014.
55
*/
66
public class F3Functions {
7+
8+
9+
10+
static public <A, B, C, D> F3<A, B, C, Try<D>> toF3(final Try3<A, B, C, D> t) {
11+
return (a, b, c) -> {
12+
try {
13+
return Try.trySuccess(t.f(a, b, c));
14+
} catch (Exception e) {
15+
return Try.<D>tryFail(e);
16+
}
17+
};
18+
}
19+
720
}

core/src/main/java/fj/F4Functions.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,16 @@
44
* Created by MarkPerry on 6/04/2014.
55
*/
66
public class F4Functions {
7+
8+
9+
static public <A, B, C, D, E> F4<A, B, C, D, Try<E>> toF4(final Try4<A, B, C, D, E> t) {
10+
return (a, b, c, d) -> {
11+
try {
12+
return Try.trySuccess(t.f(a, b, c, d));
13+
} catch (Exception e) {
14+
return Try.<E>tryFail(e);
15+
}
16+
};
17+
}
18+
719
}

core/src/main/java/fj/Try.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package fj;
2+
3+
import fj.data.Either;
4+
import fj.data.Validation;
5+
6+
/**
7+
* Created by MarkPerry on 2/06/2014.
8+
*/
9+
public class Try<A> extends Validation<Exception, A> {
10+
11+
public static <B, C> Try<C> toTry(Validation<B, C> v) {
12+
return toTry(v.toEither());
13+
}
14+
15+
public static <B, C> Try<C> toTry(Either<B, C> e) {
16+
return new Try(e);
17+
}
18+
19+
public Try(Either<Exception, A> e) {
20+
super(e);
21+
}
22+
23+
public static <B> Try<B> trySuccess(B b) {
24+
return toTry(Validation.success(b));
25+
}
26+
27+
public static <B> Try<B> tryFail(Exception e) {
28+
return toTry(Validation.fail(e));
29+
}
30+
31+
32+
}

core/src/main/java/fj/Try1.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package fj;
2+
3+
/**
4+
* Created by MarkPerry on 2/06/2014.
5+
*/
6+
public interface Try1<A, B> {
7+
8+
B f(A a) throws Exception;
9+
10+
}

core/src/main/java/fj/Try2.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package fj;
2+
3+
/**
4+
* Created by MarkPerry on 2/06/2014.
5+
*/
6+
public interface Try2<A, B, C> {
7+
8+
C f(A a, B b) throws Exception;
9+
10+
}

core/src/main/java/fj/Try3.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package fj;
2+
3+
/**
4+
* Created by MarkPerry on 2/06/2014.
5+
*/
6+
public interface Try3<A, B, C, D> {
7+
8+
D f(A a, B b, C c) throws Exception;
9+
10+
}

core/src/main/java/fj/Try4.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package fj;
2+
3+
/**
4+
* Created by MarkPerry on 2/06/2014.
5+
*/
6+
public interface Try4<A, B, C, D, E> {
7+
8+
E f(A a, B b, C c, D d) throws Exception;
9+
10+
}

core/src/main/java/fj/data/Validation.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@
3030
*
3131
* @version %build.number%
3232
*/
33-
public final class Validation<E, T> implements Iterable<T> {
33+
public class Validation<E, T> implements Iterable<T> {
3434
private final Either<E, T> e;
3535

36-
private Validation(final Either<E, T> e) {
36+
protected Validation(final Either<E, T> e) {
3737
this.e = e;
3838
}
3939

0 commit comments

Comments
 (0)