My favorites | Sign in
Project Home Downloads Wiki Issues Source
READ-ONLY: This project has been archived. For more information see this post.
Search
for
  Advanced search   Search tips   Subscriptions

Issue 56 attachment: Main.java (3.4 KB)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import fj.*;
import fj.data.NonEmptyList;
import fj.data.Option;
import fj.data.Validation;

public class Main {

public static void main(String[] args) {

//
// 1) Option example.
//
// Option<A> is either Some A or None.
// Can be though of as a type-safe @Nullable.
//

// These come from an unsafe place like parsing stuff, etc.
final Option<String> maybeSomeString = Option.some("Apple");
final Option<Integer> maybeSomeInt = Option.some(5);

// not nice:
final Option<MyDTO> notNiceResult;
if (maybeSomeString.isSome() && maybeSomeInt.isSome()) {
notNiceResult = Option.some(new MyDTO(maybeSomeString.some(), maybeSomeInt.some()));
}
else {
notNiceResult = Option.none();
}

// nice:
// (FN.xM() lifts the F2<A,B,R> to F2<X<A>, X<B>, X<R>>, so it takes inputs in context,
// and also return the result in the context. Context is for example Option, List, ...
final Option<MyDTO> maybeResultDTO = MyDTO.CTOR.optionM().f(maybeSomeString, maybeSomeInt);

//
// 2) Validation example.
//
// Validation<E, A> is either a success with result A, or a failure
// with result E.
//
// Validation is like Option, but more verbose on the reason of error.
//
// Note that here we don't need to lift the function ourself, since Validation has
// convenience methods. If we didn't have convenience methods, we could still use the general accumapply() method
//
final Validation<String, String> validatedString = Validation.fail("could not parse the string");
final Validation<String, Integer> validatedInteger = Validation.fail("could not parse the int");

// if both are success, this is a success, otherwise it contains the accumulated errors
final Validation<NonEmptyList<String>, MyDTO> validatedResult =
validatedString.nel().accumulate(Semigroup.<String>nonEmptyListSemigroup(),
validatedInteger.nel(), MyDTO.CTOR);

// print the failure side, if failed
validatedResult.f().foreach(new Effect<NonEmptyList<String>>() {
@Override
public void e(NonEmptyList<String> strings) {
Show.nonEmptyListShow(Show.stringShow).println(strings); // prints "<could not parse the string,could not parse the int>"
}
});

//
// 3) Partial application (kind-of-factory) example.
//
final F<String,F<Integer,MyDTO>> curriedCtor = MyDTO.CTOR.curry();
final F<Integer, MyDTO> appleFactory = curriedCtor.f("Apple");
final MyDTO singleApple = appleFactory.f(1);
final MyDTO dualCoreApple = appleFactory.f(2);
}

public static class MyDTO {

public static final F2<String, Integer, MyDTO> CTOR = new F2<String, Integer, MyDTO>() {
@Override
public MyDTO f(String fruit, Integer count) {
return new MyDTO(fruit, count);
}
};

private final String fruit;
private final int count;

public MyDTO(String fruit, int count) {
this.fruit = fruit;
this.count = count;
}
}

}
Powered by Google Project Hosting