GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — functionaljava.github.io/examples-java7.html at master · functionaljava/functionaljava.github.io Expand file tree/ Copy pathexamples-java7.htmlMore file actions 564 lines (527 loc) · 22.7 KB / Copy pathexamples-java7.htmlFile metadata and controls564 lines (527 loc) · 22.7 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 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 <!DOCTYPE html> <html lang="en"> <head> <title> Functional Java - Java 7 Examples </title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="author" content="Functional Java"> <meta name="description" content="Static blog generated with JBake"> <!-- Style --> <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootswatch/3.1.1/yeti/bootstrap.min.css"> <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/prettify/r298/prettify.css"> <link rel="stylesheet" href="//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css"> <link rel="stylesheet" href="/css/base.css"> <!-- HTML5 shim, for IE6-8 support of HTML5 elements --> <!--[if lt IE 9]> <script src="/js/html5shiv.js"></script> <![endif]--> <!-- Fav icon --> <link rel="shortcut icon" href="/img/favicon.ico" type="image/x-icon"> <link rel="icon" href="/img/favicon.ico" type="image/x-icon"> </head> <body> <nav class="navbar navbar-default navbar-fixed-top " role="navigation"> <div class="container"> <!-- Brand and toggle get grouped for better mobile display --> <div class="navbar-header"> <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1"> <span class="sr-only">Toggle navigation</span> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button> <a class="navbar-brand" href="/"><img src="/img/white-lambda6.png" height="20"/> Functional Java</a> </div> <!-- Collect the nav links, forms, and other content for toggling --> <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1"> <ul class="nav navbar-nav"> <li><a href="/docs.html">Docs</a></li> <li><a href="/community.html">Community</a></li> <li><a href="/download.html">Download</a></li> </ul> <!-- Right navigation --> <ul class="nav navbar-nav navbar-right"> <li><a href="/feed.xml" title="Rss"><i class="fa fa-rss"></i> Feed</a></li> </ul> <!-- Right navigation end --> </div><!-- /.navbar-collapse --> </div><!-- /.container-fluid --> </nav><!-- /.navbar --> <!-- Begin page content --> <div class="container"> <div class="row"> <div class="col-md-12"> <article> <h2>Java 7 Examples</h2> <p><div id="preamble"> <div class="sectionbody"> <div class="paragraph"> <p>These examples use standard Java 7 syntax where an instance of the <code>fj.F</code> interface is passed to emulate closures.</p> </div> <div class="paragraph"> <p>The example list is:</p> </div> <div class="ulist"> <ul> <li> <p><a href="#arrayExists">Array exists</a></p> </li> <li> <p><a href="#arrayFilter">Array filter</a></p> </li> <li> <p><a href="#arrayFoldLeft">Array foldLeft</a></p> </li> <li> <p><a href="#arrayForall">Array forall</a></p> </li> <li> <p><a href="#arrayMap">Array map</a></p> </li> <li> <p><a href="#listMap">List map</a></p> </li> <li> <p><a href="#optionBind">Option bind</a></p> </li> <li> <p><a href="#optionFilter">Option filter</a></p> </li> <li> <p><a href="#optionMap">Option map</a></p> </li> <li> <p><a href="#hlistAppend">HList append</a></p> </li> <li> <p><a href="#hlistFoldRight">HList fold right</a></p> </li> </ul> </div> </div> </div> <div class="sect1"> <h2 id="arrayExists">Array Exists</h2> <div class="sectionbody"> <div class="paragraph"> <p><a href="https://github.com/functionaljava/functionaljava/blob/master/demo/src/main/java/fj/demo/Array_exists.java">Source code</a></p> </div> <div class="paragraph"> <p>Checks for the existence of a String that has all lower case characters. In this case it is true (since, the case of "what"), but without this specific case, it is false. This example uses List.forall to test the characters of the String.</p> </div> <div class="listingblock"> <div class="content"> <pre class="prettyprint highlight"><code class="language-java" data-lang="java">import fj.F; import fj.data.Array; import static fj.data.Array.array; import static fj.data.List.fromString; import static fj.function.Characters.isLowerCase; public final class Array_exists { public static void main(final String[] args) { final Array<String> a = array("Hello", "There", "what", "DAY", "iS", "iT"); final boolean b = a.exists(new F<String, Boolean>() { public Boolean f(final String s) { return fromString(s).forall(isLowerCase); } }); System.out.println(b); // true ("what" provides the only example; try removing it) } }</code></pre> </div> </div> </div> </div> <div class="sect1"> <h2 id="arrayFilter">Array Filter</h2> <div class="sectionbody"> <div class="paragraph"> <p><a href="https://github.com/functionaljava/functionaljava/blob/master/demo/src/main/java/fj/demo/Array_filter.java">Source code</a></p> </div> <div class="paragraph"> <p>Removes elements from an array that do not meet a certain criteria. In this case, we are using an array of integers and the filter removes any odd numbers.</p> </div> <div class="listingblock"> <div class="content"> <pre class="prettyprint highlight"><code class="language-java" data-lang="java">import fj.data.Array; import static fj.data.Array.array; import static fj.Show.arrayShow; import static fj.Show.intShow; import static fj.function.Integers.even; public final class Array_filter { public static void main(final String[] args) { final Array<Integer> a = array(97, 44, 67, 3, 22, 90, 1, 77, 98, 1078, 6, 64, 6, 79, 42); final Array<Integer> b = a.filter(even); arrayShow(intShow).println(b); // {44,22,90,98,1078,6,64,6,42} } }</code></pre> </div> </div> </div> </div> <div class="sect1"> <h2 id="arrayFoldLeft">Array FoldLeft</h2> <div class="sectionbody"> <div class="paragraph"> <p><a href="https://github.com/functionaljava/functionaljava/blob/master/demo/src/main/java/fj/demo/Array_foldLeft.java">Source code</a></p> </div> <div class="paragraph"> <p>Reduces the list applying a function per element. In this case, the fold sums the elements with a starting value of 0. Since 0 + 97 + 44 + 67 + 3 + 22 + 90 + 1 + 77 + 98 + 1078 + 6 + 64 + 6 + 79 + 42 == 1774 the result of the fold is 1774.</p> </div> <div class="listingblock"> <div class="content"> <pre class="prettyprint highlight"><code class="language-java" data-lang="java">import fj.data.Array; import static fj.data.Array.array; import static fj.function.Integers.add; public final class Array_foldLeft { public static void main(final String[] args) { final Array<Integer> a = array(97, 44, 67, 3, 22, 90, 1, 77, 98, 1078, 6, 64, 6, 79, 42); final int b = a.foldLeft(add, 0); System.out.println(b); // 1774 } }</code></pre> </div> </div> </div> </div> <div class="sect1"> <h2 id="arrayForall">Array Forall</h2> <div class="sectionbody"> <div class="paragraph"> <p><a href="https://github.com/functionaljava/functionaljava/blob/master/demo/src/main/java/fj/demo/Array_forall.java">Source code</a></p> </div> <div class="paragraph"> <p>Checks that all Strings in the array have lower case characters. In this case, the check fails because of the case of "There", however, the removal of this case produces a result of true.</p> </div> <div class="listingblock"> <div class="content"> <pre class="prettyprint highlight"><code class="language-java" data-lang="java">import fj.F; import fj.data.Array; import static fj.data.Array.array; import static fj.data.List.fromString; import static fj.function.Characters.isLowerCase; public final class Array_forall { public static void main(final String[] args) { final Array<String> a = array("hello", "There", "what", "day", "is", "it"); final boolean b = a.forall(new F<String, Boolean>() { public Boolean f(final String s) { return fromString(s).forall(isLowerCase); } }); System.out.println(b); // false ("There" is a counter-example; try removing it) } }</code></pre> </div> </div> </div> </div> <div class="sect1"> <h2 id="arrayMap">Array map</h2> <div class="sectionbody"> <div class="paragraph"> <p>Maps a function across the array of integers. In this case, the function adds 42 to each element of the array to produce a new array.</p> </div> <div class="listingblock"> <div class="content"> <pre class="prettyprint highlight"><code class="language-java" data-lang="java">import fj.data.Array; import static fj.data.Array.array; import static fj.function.Integers.add; import static fj.Show.arrayShow; import static fj.Show.intShow; public final class Array_map { public static void main(final String[] args) { final Array<Integer> a = array(1, 2, 3); final Array<Integer> b = a.map(add.f(42)); arrayShow(intShow).println(b); // {43,44,45} } }</code></pre> </div> </div> </div> </div> <div class="sect1"> <h2 id="listMap">List map</h2> <div class="sectionbody"> <div class="paragraph"> <p><a href="https://github.com/functionaljava/functionaljava/blob/master/demo/src/main/java/fj/demo/List_map.java">Source code</a></p> </div> <div class="paragraph"> <p>Maps a function across a list of integers. This is similar to the Array map. This example adds 42 to each element of the list to produce a new list.</p> </div> <div class="listingblock"> <div class="content"> <pre class="prettyprint highlight"><code class="language-java" data-lang="java">import fj.data.List; import static fj.data.List.list; import static fj.function.Integers.add; import static fj.Show.intShow; import static fj.Show.listShow; public final class List_map { public static void main(final String[] args) { final List<Integer> a = list(1, 2, 3); final List<Integer> b = a.map(add.f(42)); listShow(intShow).println(b); // [43,44,45] } }</code></pre> </div> </div> </div> </div> <div class="sect1"> <h2 id="listSort">List sort</h2> <div class="sectionbody"> <div class="paragraph"> <p><a href="https://github.com/functionaljava/functionaljava/blob/master/demo/src/main/java/fj/demo/List_sort.java">Source code</a></p> </div> <div class="listingblock"> <div class="content"> <pre class="prettyprint highlight"><code class="language-java" data-lang="java">import fj.data.List; import static fj.data.List.list; import static fj.Ord.intOrd; import static fj.Show.intShow; import static fj.Show.listShow; public final class List_sort { public static void main(final String[] args) { final List<Integer> a = list(97, 44, 67, 3, 22, 90, 1, 77, 98, 1078, 6, 64, 6, 79, 42); final List<Integer> b = a.sort(intOrd); listShow(intShow).println(b); // [1,3,6,6,22,42,44,64,67,77,79,90,97,98,1078] } }</code></pre> </div> </div> </div> </div> <div class="sect1"> <h2 id="optionBind">Option Bind</h2> <div class="sectionbody"> <div class="paragraph"> <p><a href="https://github.com/functionaljava/functionaljava/blob/master/demo/src/main/java/fj/demo/Option_bind.java">[Source code</a>]</p> </div> <div class="paragraph"> <p>Binds a function across the optional value type. The function checks if the contained value is even and if it is multiples that value by 3 and returns that new value. If the contained value is odd (or if there is no value), then no value is returned (none).</p> </div> <div class="listingblock"> <div class="content"> <pre class="prettyprint highlight"><code class="language-java" data-lang="java">import fj.F; import fj.data.Option; import static fj.data.Option.none; import static fj.data.Option.some; import static fj.Show.intShow; import static fj.Show.optionShow; public final class Option_bind { public static void main(final String[] args) { final Option<Integer> o1 = some(7); final Option<Integer> o2 = some(8); final Option<Integer> o3 = none(); final Option<Integer> p1 = o1.bind(new F<Integer, Option<Integer>>() { public Option<Integer> f(final Integer i) { if(i % 2 == 0) return some(i * 3); else return none(); } }); final Option<Integer> p2 = o2.bind(new F<Integer, Option<Integer>>() { public Option<Integer> f(final Integer i) { if(i % 2 == 0) return some(i * 3); else return none(); } }); final Option<Integer> p3 = o3.bind(new F<Integer, Option<Integer>>() { public Option<Integer> f(final Integer i) { if(i % 2 == 0) return some(i * 3); else return none(); } }); optionShow(intShow).println(p1); // None optionShow(intShow).println(p2); // Some(24) optionShow(intShow).println(p3); // None } }</code></pre> </div> </div> </div> </div> <div class="sect1"> <h2 id="optionFilter">Option Filter</h2> <div class="sectionbody"> <div class="paragraph"> <p><a href="https://github.com/functionaljava/functionaljava/blob/master/demo/src/main/java/fj/demo/Option_filter.java">[Source code</a>]</p> </div> <div class="paragraph"> <p>Removes the value from the optional value if it does not match a given predicate. In this case the condition for preservation is that the contained value is an even number.</p> </div> <div class="listingblock"> <div class="content"> <pre class="prettyprint highlight"><code class="language-java" data-lang="java">import fj.data.Option; import static fj.data.Option.none; import static fj.data.Option.some; import static fj.function.Integers.even; import static fj.Show.intShow; import static fj.Show.optionShow; public final class Option_filter { public static void main(final String[] args) { final Option<Integer> o1 = some(7); final Option<Integer> o2 = none(); final Option<Integer> o3 = some(8); final Option<Integer> p1 = o1.filter(even); final Option<Integer> p2 = o2.filter(even); final Option<Integer> p3 = o3.filter(even); optionShow(intShow).println(p1); // None optionShow(intShow).println(p2); // None optionShow(intShow).println(p3); // Some(8) } }</code></pre> </div> </div> </div> </div> <div class="sect1"> <h2 id="optionMap">Option map</h2> <div class="sectionbody"> <div class="paragraph"> <p><a href="https://github.com/functionaljava/functionaljava/blob/master/demo/src/main/java/fj/demo/Option_map.java">Option map</a></p> </div> <div class="paragraph"> <p>Maps a function across the optional value type. The function adds 42 to any contained value.</p> </div> <div class="listingblock"> <div class="content"> <pre class="prettyprint highlight"><code class="language-java" data-lang="java">import fj.data.Option; import static fj.data.Option.none; import static fj.data.Option.some; import static fj.function.Integers.add; import static fj.Show.intShow; import static fj.Show.optionShow; public final class Option_map { public static void main(final String[] args) { final Option<Integer> o1 = some(7); final Option<Integer> o2 = none(); final Option<Integer> p1 = o1.map(add.f(42)); final Option<Integer> p2 = o2.map(add.f(42)); optionShow(intShow).println(p1); // Some(49) optionShow(intShow).println(p2); // None } }</code></pre> </div> </div> </div> </div> <div class="sect1"> <h2 id="hlistAppend">HList append</h2> <div class="sectionbody"> <div class="paragraph"> <p><a href="https://github.com/functionaljava/functionaljava/blob/master/demo/src/main/java/fj/demo/HList_append.java" ">Source code</a></p> </div> <div class="paragraph"> <p>Appends two heteregeneous lists. The type signature grows proportionally to the size of the list - an unfortunate consequence without type inference.</p> </div> <div class="listingblock"> <div class="content"> <pre class="prettyprint highlight"><code class="language-java" data-lang="java">import static fj.data.hlist.HList.HCons; import static fj.data.hlist.HList.HNil; import static fj.data.hlist.HList.HAppend.append; import static fj.data.hlist.HList.HAppend; import static fj.data.hlist.HList.nil; /** * Append two heterogeneous lists */ public class HList_append { public static void main(final String[] args) { // The two lists final HCons<String, HCons<Integer, HCons<Boolean, HNil>>> a = nil().extend(true).extend(3).extend("Foo"); final HCons<Double, HCons<String, HCons<Integer[], HNil>>> b = nil().extend(new Integer[]{1, 2}).extend("Bar").extend(4.0); // A lot of type annotation final HAppend<HNil, HCons<Double, HCons<String, HCons<Integer[], HNil>>>, HCons<Double, HCons<String, HCons<Integer[], HNil>>>> zero = append(); final HAppend<HCons<Boolean, HNil>, HCons<Double, HCons<String, HCons<Integer[], HNil>>>, HCons<Boolean, HCons<Double, HCons<String, HCons<Integer[], HNil>>>>> one = append(zero); final HAppend<HCons<Integer, HCons<Boolean, HNil>>, HCons<Double, HCons<String, HCons<Integer[], HNil>>>, HCons<Integer, HCons<Boolean, HCons<Double, HCons<String, HCons<Integer[], HNil>>>>>> two = append(one); final HAppend<HCons<String, HCons<Integer, HCons<Boolean, HNil>>>, HCons<Double, HCons<String, HCons<Integer[], HNil>>>, HCons<String, HCons<Integer, HCons<Boolean, HCons<Double, HCons<String, HCons<Integer[], HNil>>>>>>> three = append(two); // And all of that lets us append one list to the other. final HCons<String, HCons<Integer, HCons<Boolean, HCons<Double, HCons<String, HCons<Integer[], HNil>>>>>> x = three.append(a, b); // And we can access the components of the concatenated list in a type-safe manner System.out.println(x.head()); // Foo System.out.println(x.tail().tail().tail().tail().head()); // Bar } }</code></pre> </div> </div> </div> </div> <div class="sect1"> <h2 id="hlistFoldRight">HList foldRight</h2> <div class="sectionbody"> <div class="paragraph"> <p><a href="https://github.com/functionaljava/functionaljava/blob/master/demo/src/main/java/fj/demo/HList_foldRight.java" ">Source code</a></p> </div> <div class="paragraph"> <p>Fold across a type-safe heterogeneous list.</p> </div> <div class="listingblock"> <div class="content"> <pre class="prettyprint highlight"><code class="language-java" data-lang="java">import fj.F; import static fj.Function.identity; import fj.P2; import fj.Unit; import static fj.Unit.unit; import static java.lang.System.out; import fj.data.hlist.HList; import static fj.data.hlist.HList.single; import static fj.data.hlist.HList.Apply; import static fj.data.hlist.HList.HFoldr; import static fj.function.Integers.multiply; import static fj.function.Integers.add; import static fj.function.Strings.length; public class HList_foldRight { public static void main(final String[] args) { // A heterogeneous list of functions to compose final HList.HCons<F<String, Integer>, HList.HCons<F<Integer, Integer>, HList.HCons<F<Integer, Integer>, HList.HNil>>> functions = single(add.f(1)).extend(multiply.f(2)).extend(length); // A lot of type handwaving to convince Java that this is all going to work final Apply<Unit, P2<F<String, Integer>, F<Integer, Integer>>, F<String, Integer>> comp1 = Apply.comp(); final Apply<Unit, P2<F<Integer, Integer>, F<Integer, Integer>>, F<Integer, Integer>> comp0 = Apply.comp(); final HFoldr<Unit, F<Integer, Integer>, HList.HNil, F<Integer, Integer>> fold0 = HFoldr.hFoldr(); final HFoldr<Unit, F<Integer, Integer>, HList.HCons<F<String, Integer>, HList.HCons<F<Integer, Integer>, HList.HCons<F<Integer, Integer>, HList.HNil>>>, F<String, Integer>> fold2 = HFoldr.hFoldr(comp1, HFoldr.hFoldr(comp0, HFoldr.hFoldr(comp0, fold0))); final F<Integer, Integer> id = identity(); // Compose the list and apply the resulting function to a value. // Unit is used because composition has only one possible implementation. out.println(fold2.foldRight(unit(), id, functions).f("abc")); // 7 } }</code></pre> </div> </div> </div> </div></p> </article> </div> <!-- /.col-md-12 --> </div> <!-- /.row --> </div><!-- /.container --> <footer> <div class="container"> <hr> <div class="row"> <div class="col-xs-10"> <p class="text-muted credit">© 2014-2018 | Mixed with <a href="http://getbootstrap.com/">Bootstrap v3.1.1</a> | Baked with <a href="http://jbake.org">JBake v2.6.1</a> | <i title="Linux" class="fa fa-linux"></i></p> </div> <div class="col-xs-2 gotop"> <a href="#"><i class="fa fa-arrow-circle-up"> top</i></a> </div> </div> </div> </footer> <!-- Placed at the end of the document so the pages load faster --> <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <script src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script> <script src="//cdnjs.cloudflare.com/ajax/libs/gist-embed/1.6/gist-embed.min.js"></script> <script> (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) })(window,document,'script','//www.google-analytics.com/analytics.js','ga'); ga('create', 'UA-28753951-2', 'auto'); ga('send', 'pageview'); </script> <script src="//cdnjs.cloudflare.com/ajax/libs/prettify/r298/prettify.min.js"></script> <script type="text/javascript"> <!-- load prettify only when needed --> $(document).ready(function(){ var prettify = false; var classToAdd = 'prettyprint snippet'; $("pre > code").each(function() { $("pre > code").parent().addClass(classToAdd); prettify = true; }); if(prettify) { prettyPrint(); } }); </script> </body> </html> |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |