-
Notifications
You must be signed in to change notification settings - Fork 5
/
examples-bgga.html
379 lines (353 loc) · 14.9 KB
/
examples-bgga.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
<!DOCTYPE html>
<html lang="en">
<head>
<title>
Functional Java
- Java BGGA 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 BGGA Examples</h2>
<p><div id="preamble">
<div class="sectionbody">
<div class="paragraph">
<p>These examples use the original (now obsolete) BGGA proposed syntax. Corresponding examples are also available in <a href="examples-java8.html">Java 8</a> and <a href="examples-java7.html">Java 7</a>.</p>
</div>
<div class="paragraph">
<p>The BGGA closure prototype is required to compile these examples, which can be downloaded from <a href="http://javac.info/" class="bare">http://javac.info/</a>. Any JVM version 1.5 or higher is required to use the Functional Java library or run the compiled examples.</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>
</ul>
</div>
</div>
</div>
<div class="sect1">
<h2 id="arrayExists">Array Exists</h2>
<div class="sectionbody">
<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.data.Array;
import static fj.data.Array.array;
import static fj.data.List.fromString;
import static java.lang.Character.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({String s => fromString(s).forall({char c => isLowerCase(c)})});
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>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;
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({int i => i % 2 == 0});
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 Fold Left</h2>
<div class="sectionbody">
<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;
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.<Integer>foldLeft({int i => {int j => i + j}}, 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>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.data.Array;
import static fj.data.Array.array;
import static fj.data.List.fromString;
import static java.lang.Character.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({String s => fromString(s).forall({char c => isLowerCase(c)})});
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.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({int i => i + 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>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 static fj.data.List.list;
import fj.data.List;
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({int i => i + 42});
listShow(intShow).println(b); // [43,44,45]
}
}</code></pre>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="optionBind">Option Bind</h2>
<div class="sectionbody">
<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.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({int i => i % 2 == 0 ? some(i * 3) : Option.<Integer>none()});
final Option<Integer> p2 = o2.bind({int i => i % 2 == 0 ? some(i * 3) : Option.<Integer>none()});
final Option<Integer> p3 = o3.bind({int i => i % 2 == 0 ? some(i * 3) : Option.<Integer>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>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.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({int i => i % 2 == 0});
final Option<Integer> p2 = o2.filter({int i => i % 2 == 0});
final Option<Integer> p3 = o3.filter({int i => i % 2 == 0});
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>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.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({int i => i + 42});
final Option<Integer> p2 = o2.map({int i => i + 42});
optionShow(intShow).println(p1); // Some(49)
optionShow(intShow).println(p2); // None
}
}</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>