@@ -15,7 +15,7 @@ Here is an example of how to create a tuple holding a String and an Integer:
15
15
16
16
[source,java,indent=0]
17
17
----
18
- include::../../test/java/io/vavr/TupleDemo.java[tags=createTuple]
18
+ include::../../../ test/java/io/vavr/TupleDemo.java[tags=createTuple]
19
19
----
20
20
<1> A tuple is created via the static factory method `Tuple.of()`
21
21
<2> Get the 1st element of this tuple.
@@ -27,7 +27,7 @@ The component-wise map evaluates a function per element in the tuple, returning
27
27
28
28
[source,java,indent=0]
29
29
----
30
- include::../../test/java/io/vavr/TupleDemo.java[tags=bimapTuple]
30
+ include::../../../ test/java/io/vavr/TupleDemo.java[tags=bimapTuple]
31
31
----
32
32
33
33
==== Map a tuple using one mapper
@@ -36,7 +36,7 @@ It is also possible to map a tuple using one mapping function.
36
36
37
37
[source,java,indent=0]
38
38
----
39
- include::../../test/java/io/vavr/TupleDemo.java[tags=mapTuple]
39
+ include::../../../ test/java/io/vavr/TupleDemo.java[tags=mapTuple]
40
40
----
41
41
42
42
==== Transform a tuple
@@ -45,7 +45,7 @@ Transform creates a new type based on the tuple's contents.
45
45
46
46
[source,java,indent=0]
47
47
----
48
- include::../../test/java/io/vavr/TupleDemo.java[tags=transformTuple]
48
+ include::../../../ test/java/io/vavr/TupleDemo.java[tags=transformTuple]
49
49
----
50
50
51
51
=== Functions
@@ -55,21 +55,21 @@ The following lambda expression creates a function to sum two integers:
55
55
56
56
[source,java,indent=0]
57
57
----
58
- include::../../test/java/io/vavr/FunctionsDemo.java[tags=createFunctionWithLambda]
58
+ include::../../../ test/java/io/vavr/FunctionsDemo.java[tags=createFunctionWithLambda]
59
59
----
60
60
61
61
This is a shorthand for the following anonymous class definition:
62
62
63
63
[source,java,indent=0]
64
64
----
65
- include::../../test/java/io/vavr/FunctionsDemo.java[tags=createFunctionWithAnonymousClass]
65
+ include::../../../ test/java/io/vavr/FunctionsDemo.java[tags=createFunctionWithAnonymousClass]
66
66
----
67
67
68
68
You can also use the static factory method `Function3.of(...)` to a create a function from any method reference.
69
69
70
70
[source,java,indent=0]
71
71
----
72
- include::../../test/java/io/vavr/FunctionsDemo.java[tags=createFunctionWithFactoryMethod]
72
+ include::../../../ test/java/io/vavr/FunctionsDemo.java[tags=createFunctionWithFactoryMethod]
73
73
----
74
74
75
75
In fact Vavr functional interfaces are Java 8 functional interfaces on steroids. They also provide features like:
@@ -85,14 +85,14 @@ You can use either `andThen`:
85
85
86
86
[source,java,indent=0]
87
87
----
88
- include::../../test/java/io/vavr/FunctionsDemo.java[tags=composeFunctions1]
88
+ include::../../../ test/java/io/vavr/FunctionsDemo.java[tags=composeFunctions1]
89
89
----
90
90
91
91
or `compose`:
92
92
93
93
[source,java,indent=0]
94
94
----
95
- include::../../test/java/io/vavr/FunctionsDemo.java[tags=composeFunctions2]
95
+ include::../../../ test/java/io/vavr/FunctionsDemo.java[tags=composeFunctions2]
96
96
----
97
97
98
98
==== Lifting
@@ -102,14 +102,14 @@ The following method `divide` is a partial function that only accepts non-zero d
102
102
103
103
[source,java,indent=0]
104
104
----
105
- include::../../test/java/io/vavr/FunctionsDemo.java[tags=partialDivideFunction]
105
+ include::../../../ test/java/io/vavr/FunctionsDemo.java[tags=partialDivideFunction]
106
106
----
107
107
108
108
We use `lift` to turn `divide` into a total function that is defined for all inputs.
109
109
110
110
[source,java,indent=0]
111
111
----
112
- include::../../test/java/io/vavr/FunctionsDemo.java[tags=liftedDivideFunction]
112
+ include::../../../ test/java/io/vavr/FunctionsDemo.java[tags=liftedDivideFunction]
113
113
----
114
114
115
115
<1> A lifted function returns `None` instead of throwing an exception, if the function is invoked with disallowed input values.
@@ -119,15 +119,15 @@ The following method `sum` is a partial function that only accepts positive inpu
119
119
120
120
[source,java,indent=0]
121
121
----
122
- include::../../test/java/io/vavr/FunctionsDemo.java[tags=partialFunctionExample]
122
+ include::../../../ test/java/io/vavr/FunctionsDemo.java[tags=partialFunctionExample]
123
123
----
124
124
<1> The function `sum` throws an `IllegalArgumentException` for negative input values.
125
125
126
126
We may lift the `sum` method by providing the methods reference.
127
127
128
128
[source,java,indent=0]
129
129
----
130
- include::../../test/java/io/vavr/FunctionsDemo.java[tags=liftMethodReference]
130
+ include::../../../ test/java/io/vavr/FunctionsDemo.java[tags=liftMethodReference]
131
131
----
132
132
<1> The lifted function catches the `IllegalArgumentException` and maps it to `None`.
133
133
@@ -136,14 +136,14 @@ Partial application allows you to derive a new function from an existing one by
136
136
137
137
[source,java,indent=0]
138
138
----
139
- include::../../test/java/io/vavr/FunctionsDemo.java[tags=partialApplicationFunction]
139
+ include::../../../ test/java/io/vavr/FunctionsDemo.java[tags=partialApplicationFunction]
140
140
----
141
141
<1> The first parameter `a` is fixed to the value 2.
142
142
143
143
This can be demonstrated by fixing the first three parameters of a `Function5`, resulting in a `Function2`.
144
144
[source,java,indent=0]
145
145
----
146
- include::../../test/java/io/vavr/FunctionsDemo.java[tags=partialApplicationFunctionArity5]
146
+ include::../../../ test/java/io/vavr/FunctionsDemo.java[tags=partialApplicationFunctionArity5]
147
147
----
148
148
<1> The `a`, `b` and `c` parameters are fixed to the values 2, 3 and 1 respectively.
149
149
@@ -156,15 +156,15 @@ When a `Function2` is _curried_, the result is indistinguishable from the _parti
156
156
157
157
[source,java,indent=0]
158
158
----
159
- include::../../test/java/io/vavr/FunctionsDemo.java[tags=curryingFunction]
159
+ include::../../../ test/java/io/vavr/FunctionsDemo.java[tags=curryingFunction]
160
160
----
161
161
<1> The first parameter `a` is fixed to the value 2.
162
162
163
163
You might notice that, apart from the use of `.curried()`, this code is identical to the 2-arity given example in <<Partial application>>. With higher-arity functions, the difference becomes clear.
164
164
165
165
[source,java,indent=0]
166
166
----
167
- include::../../test/java/io/vavr/FunctionsDemo.java[tags=curryingFunctionArity3]
167
+ include::../../../ test/java/io/vavr/FunctionsDemo.java[tags=curryingFunctionArity3]
168
168
----
169
169
<1> Note the presence of additional functions in the parameters.
170
170
<2> Further calls to `apply` returns another `Function1`, apart from the final call.
@@ -175,7 +175,7 @@ The following example calculates a random number on the first invocation and ret
175
175
176
176
[source,java,indent=0]
177
177
----
178
- include::../../test/java/io/vavr/FunctionsDemo.java[tags=memoizedFunction]
178
+ include::../../../ test/java/io/vavr/FunctionsDemo.java[tags=memoizedFunction]
179
179
----
180
180
181
181
=== Values
@@ -197,7 +197,7 @@ Using `Optional`, this scenario is valid.
197
197
198
198
[source,java,indent=0]
199
199
----
200
- include::../../test/java/io/vavr/OptionDemo.java[tags=javaOptionalWithMappedNull]
200
+ include::../../../ test/java/io/vavr/OptionDemo.java[tags=javaOptionalWithMappedNull]
201
201
----
202
202
<1> The option is `Some("foo")`
203
203
<2> The resulting option becomes empty here
@@ -206,7 +206,7 @@ Using Vavr's `Option`, the same scenario will result in a `NullPointerException`
206
206
207
207
[source,java,indent=0]
208
208
----
209
- include::../../test/java/io/vavr/OptionDemo.java[tags=vavrOptionWithMappedNull]
209
+ include::../../../ test/java/io/vavr/OptionDemo.java[tags=vavrOptionWithMappedNull]
210
210
----
211
211
<1> The option is `Some("foo")`
212
212
<2> The resulting option is `Some(null)`
@@ -218,7 +218,7 @@ This may seem to make `Option` useless, but it actually forces you to pay attent
218
218
219
219
[source,java,indent=0]
220
220
----
221
- include::../../test/java/io/vavr/OptionDemo.java[tags=flatMapNullParameter]
221
+ include::../../../ test/java/io/vavr/OptionDemo.java[tags=flatMapNullParameter]
222
222
----
223
223
<1> The option is `Some("foo")`
224
224
<2> The resulting option is `Some(null)`
@@ -228,7 +228,7 @@ Alternatively, move the `.flatMap` to be co-located with the the possibly `null`
228
228
229
229
[source,java,indent=0]
230
230
----
231
- include::../../test/java/io/vavr/OptionDemo.java[tags=mapOptionParameter]
231
+ include::../../../ test/java/io/vavr/OptionDemo.java[tags=mapOptionParameter]
232
232
----
233
233
<1> The option is `Some("foo")`
234
234
<2> The resulting option is `None`
@@ -263,7 +263,7 @@ Lazy is a monadic container type which represents a lazy evaluated value. Compar
263
263
264
264
[source,java,indent=0]
265
265
----
266
- include::../../test/java/io/vavr/LazyDemo.java[tags=createLazy]
266
+ include::../../../ test/java/io/vavr/LazyDemo.java[tags=createLazy]
267
267
----
268
268
269
269
You may also create a real lazy value (works only with interfaces):
@@ -310,21 +310,21 @@ Example: We get the fields 'name' and 'age' from a web form and want to create e
310
310
311
311
[source,java,indent=0]
312
312
----
313
- include::../../test/java/io/vavr/ValidationDemo.java[tags=validatePerson]
313
+ include::../../../ test/java/io/vavr/ValidationDemo.java[tags=validatePerson]
314
314
----
315
315
316
316
A valid value is contained in a `Validation.Valid` instance, a list of validation errors is contained in a `Validation.Invalid` instance.
317
317
318
318
The following validator is used to combine different validation results to one `Validation` instance.
319
319
320
320
----
321
- include::../../test/java/io/vavr/ValidationDemo.java[tags=personValidator]
321
+ include::../../../ test/java/io/vavr/ValidationDemo.java[tags=personValidator]
322
322
----
323
323
324
324
If the validation succeeds, i.e. the input data is valid, then an instance of `Person` is created of the given fields `name` and `age`.
325
325
326
326
----
327
- include::../../test/java/io/vavr/ValidationDemo.java[tags=person]
327
+ include::../../../ test/java/io/vavr/ValidationDemo.java[tags=person]
328
328
----
329
329
330
330
=== Collections
0 commit comments