@@ -101,9 +101,6 @@ <h2>Java 8 Examples</h2>
101
101
< li >
102
102
< p > < a href ="#optionMap "> Option map</ a > </ p >
103
103
</ li >
104
- < li >
105
- < p > < a href ="#ioWalkthrough "> IO Walkthrough</ a > </ p >
106
- </ li >
107
104
</ ul >
108
105
</ div >
109
106
</ div >
@@ -396,79 +393,7 @@ <h2 id="optionMap">Option Map</h2>
396
393
</ div >
397
394
</ div >
398
395
</ div >
399
- </ div >
400
- < div class ="sect1 ">
401
- < h2 id ="ioWalkthrough "> IO Walkthrough</ h2 >
402
- < div class ="sectionbody ">
403
- < div class ="paragraph ">
404
- < p > < a href ="https://github.com/functionaljava/functionaljava/blob/master/demo/src/main/java/fj/demo/IOWalkthrough.java "> Github Source</ a > </ p >
405
- </ div >
406
- < div class ="paragraph ">
407
- < p > Demonstrates how to work with the IO type.</ p >
408
- </ div >
409
- < div class ="listingblock ">
410
- < div class ="content ">
411
- < pre class ="prettyprint highlight "> < code class ="language-java " data-lang ="java ">
412
- // IO is just a container to defer a computation (lazy), with the intention to encapsulate computations that either
413
- // consume and/or produce side-effects
414
- // the computation is not (yet) executed on creation hence it can be treated like a value
415
-
416
- final IO<Unit> askName = () -> {
417
- System.out.println("Hi, what's your name?");
418
- return Unit.unit();
419
- };
420
-
421
- // fj.data.IOFunctions contains a lot of convenience functions regarding IO, the above example could be rewritten with IOFunctions.stdoutPrintln
422
- // we now create an IO value to prompt for the name if executed
423
-
424
- IO<Unit> promptName = IOFunctions.stdoutPrint("Name: ");
425
-
426
- // we can compose these two values with fj.data.IOFunctions.append, since they both are not interested in any runtime value
427
-
428
- IO<Unit> askAndPromptName = IOFunctions.append(askName, promptName);
429
-
430
- // now we create an IO value to read a line from stdin
431
-
432
- final IO<String> readName = () -> new BufferedReader(new InputStreamReader(System.in)).readLine();
433
-
434
- // this is the same as IOFunctions.stdinReadLine()
435
-
436
- // now we create a function which takes a string, upper cases it and creates an IO value that would print the upper cased string if executed
437
-
438
- final F<String, IO<Unit>> upperCaseAndPrint = F1Functions.<String, IO<Unit>, String>o(IOFunctions::stdoutPrintln).f(String::toUpperCase);
439
-
440
- // we now want to compose reading the name with printing it, for that we need to have access to the runtime value that is returned when the
441
- // IO value for read is executed, hence we use fj.data.IOFunctions.bind instead of fj.data.IOFunctions.append
442
-
443
- final IO<Unit> readAndPrintUpperCasedName = IOFunctions.bind(readName, upperCaseAndPrint);
444
-
445
- // so append is really just a specialised form of bind, ignoring the runtime value of the IO execution that was composed before us
446
-
447
- final IO<Unit> program = IOFunctions.bind(askAndPromptName, ignored -> readAndPrintUpperCasedName);
448
-
449
- // this is the same as writing IOFunctions.append(askAndPromptName, readAndPrintUpperCasedName)
450
-
451
- // we have recorded the entire program, but have not run anything yet
452
- // now we get to the small dirty part at the end of our program where we actually execute it
453
- // we can either choose to just call program.run(), which allows the execution to escape
454
- // or we use safe to receive an fj.data.Either with the potential exception on the left side
455
-
456
- toSafeValidation(program).run().on((IOException e) -> { e.printStackTrace(); return Unit.unit(); });
457
-
458
- // doing function composition like this can be quite cumbersome, since you will end up nesting parenthesis unless you flatten it out by
459
- // assigning the functions to variables like above, but you can use the fj.F1W syntax wrapper for composing single-argument functions and fj.data.IOW
460
- // for composing IO values instead, the entire program can be written like so:
461
-
462
- IOW.lift(stdoutPrintln("What's your name again?"))
463
- .append(stdoutPrint("Name: "))
464
- .append(stdinReadLine())
465
- .bind(F1W.lift((String s) -> s.toUpperCase()).andThen(IOFunctions::stdoutPrintln))
466
- .safe().run().on((IOException e) -> { e.printStackTrace(); return Unit.unit(); });
467
- </ code > </ pre >
468
- </ div >
469
- </ div >
470
- </ div >
471
- </ div > </ p >
396
+ </ div > </ p >
472
397
</ article >
473
398
</ div > <!-- /.col-md-12 -->
474
399
</ div > <!-- /.row -->
0 commit comments