-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
579 lines (549 loc) · 19.7 KB
/
index.html
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>reveal.js</title>
<link rel="stylesheet" href="css/reveal.css">
<link rel="stylesheet" href="css/theme/moon.css">
<!-- Theme used for syntax highlighting of code -->
<link rel="stylesheet" href="lib/css/zenburn.css">
<!-- Printing and PDF exports -->
<script>
var link = document.createElement( 'link' );
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = window.location.search.match( /print-pdf/gi ) ? 'css/print/pdf.css' : 'css/print/paper.css';
document.getElementsByTagName( 'head' )[0].appendChild( link );
</script>
</head>
<body>
<div class="reveal">
<div class="slides">
<section>
<section>
<h2>And Java becomes more functional With Javaslang!</h2>
<img src="assets/img/javaslang-logo.png" class="no-borders">
</section>
<section>
<h2>And Java becomes more functional with <span style="text-decoration:line-through;">Javaslang</span>!</h2>
<div class="img-strike">
<img src="assets/img/javaslang-logo.png" class="no-borders">
</div>
</section>
<section>
<h2>And Java becomes more functional with Vavr.io!</h2>
<img src="assets/img/vavr-logo.png" class="no-borders">
</section>
</section>
<section>
<section data-background-image="./assets/img/lambda.svg" data-background-size="60%">
<h2>Functional Programming</h2>
<h4>What does it mean? </h4>
</section>
<section>
<h2>No more side effects!</h2>
<ul>
<li> Functions are first-class citizens </li>
<li> Immutability </li>
<li> Expressiveness </li>
<li> Safety </li>
</ul>
</section>
<section>
<h2>Referential Transparency</h2>
<div class="">
<pre>
<code class="java">int globalValue = 0;
// No Referential Transparency
int rq(int x) {
globalValue++;
return x + globalValue;
}
//Referential Transparency
int rt(int x) {
return x + 1;
}
</code>
</pre>
</div>
</section>
<section>
<h2>Referential Transparency</h2>
<h3>Other example</h3>
<div class="">
<pre>
<code class="java">// No Referential Transparency
Math.random();
//Referential Transparency
Math.max(5, 42);
</code>
</pre>
</div>
</section>
</section>
<section>
<h2>Java 8 & Functional Programming</h2>
<ul>
<li>Lambda</li>
<li>Stream</li>
<li>Optional</li>
<li>Functional Interface</li>
<li>Function composition</li>
<li>Collections interfaces review</li>
</ul>
</section>
<section data-background-image="./assets/img/frustration.gif">
<h2>There's still stuff missing</h2>
</section>
<section>
<h2>Who I am ?</h2>
<img src="./assets/img/avatar.png" height="300" class="no-borders" style="float: left">
<ul style="margin-top: 30px">
<li>Twitter : <a href="https://www.twitter.com/glours">@glours</a></li>
<li><a href="www.saagie.io">Saagie</a> 'Frozen' Team Leader</li>
<li>Java and Javascript developer</li>
<li>Team member of Devoxx France</li>
</ul>
</section>
<section>
<section>
<h2>So ...</h2>
<img src="./assets/img/vavr-logo.png">
</section>
<section data-background-color="white">
<img src="./assets/img/javaslang-overview.png" class="no-borders">
</section>
</section>
<section>
<section>
<h2>Immutable Collections</h2>
</section>
<section>
<h2>It's already in JDK 8 !</h2>
<h5>Humm....</h5>
<pre><code class="java">public List<Integer> unmodifiableListJdk8() {
List<Integer> jdkList = IntStream.range(0,20)
.boxed()
.collect(Collectors.toList());
return Collections.unmodifiableList(jdkList);
}
@Test
public void should_verify_behaviour_of_jdk_8_unmodifiable_list() throws Exception {
List<Integer> unmodifiableList = this.examples.unmodifiableListJdk8();
unmodifiableList.add(21);
assertThat(unmodifiableList).hasSize(21);
}
</code></pre>
</section>
<section data-background-color="white">
<h2>List, Array, Stream ...</h2>
<img src="./assets/img/collections-seq.png" class="no-borders" alt="" width="1000">
</section>
<section>
<h2>Java 8 example</h2>
<pre><code class="java">java.util.List<User> filterUserWithValidEmail() {
return this.usersJdk8
.stream()
.filter(user -> {
try {
return user.isEmailValid();
} catch (InvalidFormatException e) {
return false;
}
})
.collect(Collectors.toList());
} </code></pre>
</section>
<section>
<h2>With Vavr</h2>
<pre><code class="java">public List<User> filterUserWithValidAddressVavr() {
return this.usersVavr
.filter(user -> Try.of(() -> user
.isAddressValid())
.getOrElse(false));
}
</code></pre>
</section>
<section>
<h2>An other Java 8 example</h2>
<pre><code class="java">public java.util.List<Address> filterInvalidAddressFromRouenJdk8() {
return this.usersJdk8.stream()
.filter(User::isInvalidAddressFromRouen)
.map(User::getAddress)
.collect(Collectors.toList());
}
</code></pre>
</section>
<section>
<h2>And With Vavr</h2>
<pre><code class="java">public List<Address> filterInvalidAddressFromRouenVavr() {
return this.usersVavr
.filter(User::isInvalidAddressFromRouen)
.map(User::getAddress);
}
</code></pre>
</section>
<section>
<h2>Stream consistency</h2>
<p>Java 8</p>
<pre><code class="java">public java.util.stream.Stream<String> mapUserToLowerCaseUserNameWithJdk8Steam() {
java.util.stream.Stream<User> userNameStream = java.util.stream.Stream.of(generateUser(7), generateUser(15));
userNameStream.map(user -> user.getUserName()
.toUpperCase());
return userNameStream
.map(user -> user.getUserName().toLowerCase());
}
java.lang.IllegalStateException: stream has already been operated upon or closed
at java.util.stream.AbstractPipeline.<init>(AbstractPipeline.java:203)
at java.util.stream.ReferencePipeline.<init>(ReferencePipeline.java:94)
at java.util.stream.ReferencePipeline$StatelessOp.<init>(ReferencePipeline.java:618)
at java.util.stream.ReferencePipeline$3.<init>(ReferencePipeline.java:187)
at java.util.stream.ReferencePipeline.map(ReferencePipeline.java:186)</code></pre>
</section>
<section>
<h2>Stream consistency</h2>
<p>Vavr</p>
<pre><code class="java">public Stream<String> mapUserToLowerCaseUserNameWithVavrStream() {
Stream<User> userNameStream = Stream.of(generateUser(7), generateUser(15));
userNameStream.map(user -> user.getUserName()
.toUpperCase());
return userNameStream
.map(user -> user.getUserName().toLowerCase());
}
</code></pre>
</section>
<section data-background-color="white">
<h2>Map, Set ...</h2>
<img src="./assets/img/collections-set-map.png" class="no-borders" width="1000">
</section>
<section>
<h2>Tuple</h2>
<img src="./assets/img/tuple-javadoc.png" class="no-borders">
</section>
<section>
<h2>Java 8 example</h2>
<pre><code class="java">public java.util.Map<String, User> filterMapOfUserWithValidAddressJdk8() {
java.util.Map<String, User> usersMap = this.usersToMapJdk8();
return usersMap.entrySet().stream()
.filter(entry -> {
try {
return entry.getValue().isAddressValid();
} catch (InvalidFormatException e) {
return false;
}
})
.collect(Collectors.toMap((entry -> entry.getKey()), entry -> entry.getValue()));
}</code></pre>
</section>
<section>
<h2>With Vavr</h2>
<pre><code class="java">public Map<String, User> filterMapOfUserWithValidAddressVavr() {
Map<String, User> userMap = this.usersToMap();
return userMap
.filter(tuple -> Try.of(() ->
tuple._2.isAddressValid()).getOrElse(false));
}
</code></pre>
</section>
</section>
<section>
<section>
<h2>Value Types</h2>
<ul>
<li>Option</li>
<li>Try</li>
<li>Either</li>
<li>Validation</li>
<li>(Lazy, Future, Promise, Match)</li>
</ul>
</section>
<section>
<h2>Option</h2>
<h5>1 Interface</h5>
<img src="./assets/img/javaslang-none.png" alt="Javaslang None Object definition" class="no-borders">
<img src="./assets/img/javaslang-some.png" alt="Javaslang Some Object definition" class="no-borders">
</section>
<section>
<h2>Java 8 Optional</h2>
<pre><code class="java">public Optional<Address> optionalOfUserAddress(String userName) {
if( !this.usersJdk8.containsKey(userName)) {
return Optional.empty();
}
return Optional.of(this.usersJdk8.get(userName).getAddress());
}
public Optional<Address> optionalOfNullUsageForUserAddress(String userName) {
return Optional.ofNullable(this.usersJdk8.get(userName))
.map(user -> user.getAddress());
}
</code></pre>
</section>
<section>
<h2>Vavr Option</h2>
<pre><code class="java">public Option<Address> optionOfUserAddress(String userName) {
return this.usersVavr.get(userName)
.map(user -> user.getAddress());
}
/* Because Map.get(key) return an Option in Vavr
Option<V>get(K var1);
*/
</code></pre>
</section>
<section>
<h2>Try</h2>
<h5>May return an exception or a successful result</h5>
<pre><code>public Try tryOfUserAddress(String userName) {
return Try.of(() -> this.usersVavr.get(userName)
.get().getAddressIfValid());
}</code></pre>
</section>
<section>
<h2>Either</h2>
<h5>Represents a value of 2 possible types</h5>
<h5>Either is either Right or Left</h5>
<h5>by convention Right refers to the nominal case</h5>
</section>
<section>
<h2>Either Example</h2>
<pre><code> public Either<String, User> eitherOfUser(String userName) {
return this.usersVavr.get(userName)
.toRight("Not Found");
}</code></pre>
</section>
<section>
<h2>Validation</h2>
<h5>Errors accumulation</h5>
<h5>Processes all validations, no circuit breaking when an error is found</h5>
</section>
<section>
<h2>Example available on github</h2>
<a href="https://github.com/glours/vavr-examples">GitHub: https://goo.gl/BHcYRJ</a>
</section>
</section>
<section>
<section>
<h2>Functions</h2>
<h5>What about the first-class citizens ?</h5>
</section>
<section>
<h2>Java 8 comes with Function and BiFunction</h2>
<h2>Vavr provides functional interfaces up to 8 parameters</h2>
</section>
<section>
<blockquote>In fact Vavr functional interfaces are Java 8 functional interfaces on steroids</blockquote>
<a href="http://www.vavr.io/vavr-docs/#_functions">Vavr documentation</a>
</section>
<section>
<h2>Composition</h2>
<h5>Application of one function as the result of another to produce a new one</h5>
</section>
<section>
<h2>Composition example</h2>
<pre><code class="java">private Function1<Option<User>, Option<String>> lastName
= user -> user.map(exist -> exist.getLastName());
private Function1<Option<String>, Option<String>> toUpperCase
= value -> value.map(string -> string.toUpperCase());
private Function1<Option<User>, Option<String>> lastNameInUpperCase
= lastName.andThen(toUpperCase);
public String userLastNameToUpperCase(String userName) {
return lastNameInUpperCase
.apply(usersJavaslang.get(userName))
.getOrElse("User Not Found");
}
</code></pre>
</section>
<section>
<h2>Lifting</h2>
<h5>Turning a side effect function to a total function</h5>
</section>
<section>
<h2>Lifting example</h2>
<pre><code class="java">private Function1<Option<User>, Address> getAddressWithSideEffect
= user -< user.get().getAddress();
private Function1<Option<User>, Option<Address>> safeGetAddress
= Function1.lift(getAddressWithSideEffect);
public Address sideEffectGetAddress(String userName) {
return getAddressWithSideEffect
.apply(usersJavaslang.get(userName));
}
public Option<Address> safeGetAddress(String userName) {
return safeGetAddress
.apply(this.usersVavr.get(userName));
}
</code></pre>
</section>
<section>
<h2>Partial Application</h2>
<h5>Deriving a new function from an existing one by fixing some parameters</h5>
</section>
<section>
<h2>Partial Application example</h2>
<pre><code class="java">private Function4 <Integer,Integer,Integer,Integer,Integer> sum
= (a, b, c, d ) -> a + b + c + d;
private Function1<Integer, Integer> partialApplicationFunc
= sum.apply(1,2,3);
public int partialApplication(int val) {
return partialApplicationFunc.apply(val);
}
</code></pre>
</section>
<section>
<h2>Currying</h2>
<h5>Deriving a new function from an existing by fixing 1 parameter and returning a new function with arity of 1</h5>
</section>
<section>
<h2>Currying example</h2>
<pre><code class="java">private Function1<Integer, Function1<Integer, Function1<Integer, Integer>>> curriedFunc
= sum.curried().apply(5);
public int currying(int val1, int val2, int val3) {
return curriedFunc.apply(val1).apply(val2).apply(val3);
}
</code></pre>
</section>
<section>
<h2>Memoization</h2>
<h5>Kind of cache system</h5>
<h5>A function is executed only once and then returns the result from the cache</h5>
</section>
<section>
<h2>Memoization example</h2>
<pre><code class="java">private Function0>Double< memoizedRandom = Function0.of(Math::random).memoized();
public double memoize() {
return memoizedRandom.apply();
}
@Test
public void should_use_memoization_to_add() throws Exception {
double firstCall = this.examples.memoize();
assertThat(List.range(0, 20)
.map(val -> this.examples.memoize()))
.allMatch(val -> val == firstCall);
}
</code></pre>
</section>
</section>
<section>
<section>
<h2>Pattern Matching and Property checking</h2>
</section>
<section>
<h2>Pattern Matching, the Vavr way</h2>
<ul>
<li>$() - wildcard pattern</li>
<li>$(value) - equals pattern</li>
<lig>$(predicate) - conditional pattern</lig>
</ul>
</section>
<section>
<h2>Syntactic Sugar</h2>
<ul>
<li>Predicate <pre><code>Case(is(1), "one")</code></pre></li>
<li>Multiple conditions <pre><code>Case(isIn("-h", "--help"), ...)</code></pre></li>
<lig>User-Defined Patterns, Guards ...</lig>
</ul>
</section>
<section>
<h2>Pattern Matching example</h2>
<pre><code class="java">Function0<Option<String>> usageDocumentation = () -> Option.of("usage: JavaslangDemo [options] \n") ;
Function1<Option<String>, Option<String>> versionDocumentation = previous -> previous.map(prev -> prev + "-v, --version : Display version Information");
Function0<Option<String>> helpDocumentation = usageDocumentation.andThen(versionDocumentation);
Function2<Option<String>, Option<String>, Option<String>> invalidCommand = (previous, arg) -> previous.map(prev -> "Unable to parse command line option : " + arg.getOrElse("") +"\n" + prev);
public static void main( String[] args )
{
Option<String> arg = Array.of(args).headOption();
String commandDescription = API.Match(arg.getOrElse("")).of(
Case(isIn("-h", "--help"), helpDocumentation.apply()),
Case(isIn("-v", "--vesion"), versionDocumentation.apply(Option.none()) ),
Case($(), invalidCommand.apply(helpDocumentation.apply(), arg))
).getOrElse("Error when parsing argument");
System.out.println(commandDescription);
}
</code></pre>
</section>
<section>
<h2>Another example</h2>
<pre><code class="java">public List<Either<Address, Address>> patternMatchingList() {
List<User> users = this.usersVavr.map(tuple -> tuple._2).toList();
return users.map(user ->
Match(validateUser(Option.of(user))).of(
Case(Valid($()), validUser -> Either.right(validUser.getAddress())),
Case(Invalid($()), errorList -> Either.left(user.getAddress()))
));
}
</code></pre>
</section>
<section>
<h2>Property Checking</h2>
</section>
<section>
<h2>Property Checking example</h2>
<pre><code class="java">@Test
public void should_l33t_string() throws Exception {
Arbitrary<String> leetCharEto3 = Arbitrary.string(Gen.frequency(
Tuple.of(1, Gen.choose('A','Z')),
Tuple.of(1, Gen.choose('a','z'))
))
.filter(s -> s.length() > 10)
.filter(s -> s.matches("\\w*[eE]+\\w*"));
Function1<String, String> transformETo3 =
s -> s.replaceAll("[eE]", "3");
CheckedFunction1<String, Boolean> checkTransformETo3 =
s -> transformETo3.apply(s)
.matches("\\w*[^eE]+\\w*")
&& transformETo3.apply(s).contains("3");
Property.def("Each e character must be replace by a 3")
.forAll(leetCharEto3)
.suchThat(checkTransformETo3)
.check()
.assertIsSatisfied();
}
</code></pre>
</section>
</section>
<section>
<h2>Vavr Modules</h2>
<img src="./assets/img/vavr-modules.png" alt="" class="no-borders">
</section>
<section>
<h2>Vavr Links</h2>
<p><a href="https://www.vavr.io/vavr-docs/">Documentation: https://goo.gl/dMKKjN</a></p>
<p><a href="http://www.javadoc.io/doc/io.javaslang/javaslang/2.0.6">Javadoc: https://goo.gl/PHQ81g</a></p>
<p><a href="https://github.com/vavr-io/vavr">Source code : https://goo.gl/p3ivLM</a></p>
</section>
<section data-background-image="./assets/img/thank_you.gif">
<h1>Thank You</h1>
</section>
<section data-background-image="./assets/img/mandatory.gif">
<h1>Questions ?</h1>
<p><a href="https://github.com/glours/vavr-examples">source code: https://goo.gl/BHcYRJ </a></p>
</section>
</div>
</div>
<script src="lib/js/head.min.js"></script>
<script src="js/reveal.js"></script>
<script>
// More info https://github.com/hakimel/reveal.js#configuration
Reveal.initialize({
history: true,
controls: true,
progress: true,
slideNumber: false,
keyboard: true,
overview: true,
center: true,
touch: true,
hideAddressBar: true,
transition: 'slide', // none/fade/slide/convex/concave/zoom
transitionSpeed: 'default',
backgroundTransition: 'fade', // none/fade/slide/convex/concave/zoom
viewDistance: 3,
// More info https://github.com/hakimel/reveal.js#dependencies
dependencies: [
{ src: 'plugin/markdown/marked.js' },
{ src: 'plugin/markdown/markdown.js' },
{ src: 'plugin/notes/notes.js', async: true },
{ src: 'plugin/highlight/highlight.js', async: true, callback: function() { hljs.initHighlightingOnLoad(); } }
]
});
</script>
</body>
</html>