forked from functionaljava/functionaljava.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfeatures.html
377 lines (358 loc) · 17.8 KB
/
features.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
<!DOCTYPE html>
<html lang="en">
<head>
<title>
Functional Java
- Features
</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>Features</h2>
<p><div class="sect1">
<h2 id="_first_class_functions">First-Class Functions</h2>
<div class="sectionbody">
<div class="paragraph">
<p>Functional Java provides generic interfaces and abstract classes that serve as first-class functions or closures, entirely within Java’s type system (i.e. without reflection or byte-code manipulation). The library centers around the F interface, which models a function from type A to type B.</p>
</div>
<div class="paragraph">
<p>Functions are written with anonymous class syntax:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-java" data-lang="java">// Regular Style
Integer timesTwo(Integer i) { return i * 2; }
// Functional Style
F timesTwo = new F() { public Integer f(Integer i) { return i * 2; } }</code></pre>
</div>
</div>
<div class="paragraph">
<p>First-class functions can be composed and passed as arguments to higher-order functions:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-java" data-lang="java">// Regular Style
Integer timesTwoPlusOne(Integer i) { return plusOne(timesTwo(i)); }
// Functional Style
F timesTwoPlusOne = plusOne.o(timesTwo);</code></pre>
</div>
</div>
<div class="paragraph">
<p>And mapped over collections:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-java" data-lang="java">//Regular Style
List oneUp = new ArrayList();
for (Integer i: ints) oneUp.add(plusOne(i));
// Functional Style
List oneUp = ints.map(plusOne);</code></pre>
</div>
</div>
<div class="paragraph">
<p>Functions up to arity-8 are supported, allowing elimination of nested control constructs:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-java" data-lang="java">// Regular Style
Integer product = 1;
for (Integer x: ints) product = x * product;
List products1 = new ArrayList();
for (int x = 0; x < ints.size(); x++) {
for (int y = 0; y <= x; y++) {
products.add(ints.get(x) * ints.get(y);
}
}
List products2 = new ArrayList();
for (Integer x: ints) {
for (Integer y: ints) {
products.add(x * y);
}
}
// Functional Style
Integer product = ints.foldLeft(1, multiply);
List products1 = ints.tails().apply(ints.map(multiply));
List products2 = ints.bind(ints, multiply);</code></pre>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_immutable_datastructures">Immutable Datastructures</h2>
<div class="sectionbody">
<div class="paragraph">
<p>Functional Java implements many immutable datastructures, such as</p>
</div>
<div class="ulist">
<ul>
<li> <p>Singly-linked lists (<code>fj.data.List</code>)</p> </li>
<li> <p>Non-strict, potentially infinite, singly-linked list (<code>fj.data.Stream</code>)</p> </li>
<li> <p>Non-strict, potentially infinite Strings (<code>fj.data.LazyString</code>)</p> </li>
<li> <p>A wrapper for arrays (<code>fj.data.Array</code>)</p> </li>
<li> <p>A wrapper for any Iterable type (<code>fj.data.IterableW</code>)</p> </li>
<li> <p>Immutable ordered sets (<code>fj.data.Set</code>)</p> </li>
<li> <p>Multi-way trees — a.k.a. rose trees, with support for infinite trees (<code>fj.data.Tree</code>)</p> </li>
<li> <p>Immutable Map, with single-traversal search-and-update (<code>fj.data.TreeMap</code>)</p> </li>
<li> <p>Type-safe heterogeneous lists (<code>fj.data.hlist</code>)</p> </li>
<li> <p>Pointed lists and trees (<code>fj.data.Zipper</code> and <code>fj.data.TreeZipper</code>)</p> </li>
</ul>
</div>
<div class="paragraph">
<p>These datatypes come with many powerful higher-order functions, such as map (for functors), bind (monads), apply and zipWith (applicative functors), and cobind (for comonads).</p>
</div>
<div class="paragraph">
<p>Efficient conversions to and from the standard Java Collections classes are provided, and <code>java.util.Iterable</code> is implemented where possible, for use with Java’s foreach syntax.</p>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_optional_values_type_safe_null">Optional Values (type-safe null)</h2>
<div class="sectionbody">
<div class="paragraph">
<p>The library provides a datatype for variables, parameters, and return values that may have no value, while remaining type-safe.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-java" data-lang="java">// Using null
String val = map.get(key);
if (val == null || val.equals("")) val = "Nothing";
return val;
// Using Option
return fromString(map.get(key)).orSome("Nothing");</code></pre>
</div>
</div>
<div class="paragraph">
<p>Optional values are iterable, so they play nicely with foreach syntax, and they can be composed in a variety of ways. The fj.Option class has a plethora of methods for manipulating optional values, including many higher-order functions.</p>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_product_types">Product Types</h2>
<div class="sectionbody">
<div class="paragraph">
<p>Joint union types (tuples) are products of other types. Products of arities 1-8 are provided (<code>fj.P1</code> - <code>fj.P8</code>). These are useful for when you want to return more than one value from a function, or when you want to accept several values when implementing an interface method that accepts only one argument. They can also be used to get products over other datatypes, such as lists (zip function).</p>
</div>
<div class="paragraph">
<p>Example:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-java" data-lang="java">import java.util.Map;
import static fj.P.*;
import fj.data.Option;
import fj.data.TreeMap;
import fj.P2;
// Regular Java
public Integer albuquerqueToLA(Map<String, Map<String, Integer>> map) {
Map<String, Integer> m = map.get("Albuquerque");
if (m != null)
return m.get("Los Angeles"); // May return null.
}
// Functional Java with product and option types.
public Option<Integer> albuquerqueToLA(TreeMap<P2<String, String>, Integer> map) {
return map.get(p("Albuquerque", "Los Angeles"));
}</code></pre>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_disjoint_union_types">Disjoint Union Types</h2>
<div class="sectionbody">
<div class="paragraph">
<p>By the same token, types can be added by disjoint union. Values of type <code>Either<A, B></code> contain a value of either type <code>A</code> or type <code>B</code>. This has many uses. As an argument type, it allows a single argument to depend on the type of value that is received (effectively overloading the method even if the interface is not designed to do that). As a return type, it allows you to return a value of one of two types depending on some condition. For example, to provide error handling where you are not allowed to throw `Exception`s:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-java" data-lang="java">// Meanwhile, inside an iterator implementation...
public Either next() {
String s = moreInput();
try {
return Either.right(Integer.valueOf(s));
} catch (Exception e) {
return Either.left(Fail.invalidInteger(s));
}
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>The <code>Either</code> class includes a lot of useful methods, including higher-order functions for mapping and binding over the left and right types, as well as Iterable implementations for both types.</p>
</div>
<div class="paragraph">
<p><a href="http://apocalisp.wordpress.com/2008/06/04/throwing-away-throws">See here for a more detailed explanation of using <code>Either</code> for handling errors.</a></p>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_higher_order_concurrency_abstractions">Higher-Order Concurrency Abstractions</h2>
<div class="sectionbody">
<div class="paragraph">
<p>Functional Java includes Parallel Strategies (fj.control.parallel.Strategy) for effectively decoupling concurrency patterns from algorithms. Strategy provides higher-order functions for mapping and binding over collections in parallel:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-java" data-lang="java">Strategy s = simpleThreadStrategy();
List ns = range(Integer.MIN_VALUE, Integer.MIN_VALUE + 10).map(negate).toList();
List bs = s.parMap(ns, isPrime);</code></pre>
</div>
</div>
<div class="paragraph">
<p>Also included is an implementation of the actor model (<code>fj.control.parallel.Actor</code> and <code>QueueActor</code>), and <code>Promise</code>, which is a composable and non-blocking version of <code>java.util.concurrent.Future</code>.</p>
</div>
<div class="paragraph">
<p><a href="http://apocalisp.wordpress.com/2008/06/30/parallel-list-transformations">A series of blog posts on the concurrency features can be found here.</a></p>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_abstractions">Abstractions</h2>
<div class="sectionbody">
<div class="paragraph">
<p>Functional Java provides abstractions for the following types:</p>
</div>
<div class="ulist">
<ul>
<li> <p>Basic Data Structures</p>
<div class="ulist">
<ul>
<li> <p>Functions with arity 1 to 8 (<code>fj.F</code>).</p> </li>
<li> <p>Functions with arity 0 to 8 that can produce exceptions (<code>fj.TryCatch</code>).</p> </li>
<li> <p>Products with arity 1 to 8 (<code>fj.P</code>).</p> </li>
<li> <p>Unit type (<code>fj.Unit</code>).</p> </li>
<li> <p>Optional value - <em>type-safe null</em> (<code>fj.data.Option</code>).</p> </li>
<li> <p>Disjoint union data type - <em>compositional exception handling</em> (<code>fj.data.Either</code>).</p> </li>
<li> <p>Validation - <em>right biased</em> compositional exception handling (<code>fj.data.Validation</code>).</p> </li>
<li> <p>Void - a logically uninhabited data type.</p> </li>
</ul>
</div> </li>
<li> <p>Immutable Collections</p>
<div class="ulist">
<ul>
<li> <p>Array wrapper (<code>fj.data.Array</code>).</p> </li>
<li> <p>Immutable, in-memory singly linked list (<code>fj.data.List</code>).</p> </li>
<li> <p>Immutable lazy singly linked list (<code>fj.data.Stream</code>).</p> </li>
<li> <p>A package (<code>fj.data.fingertrees</code>) providing 2-3 finger trees for a functional representation of persistent sequences, supporting access to the ends in amortized O(1) time.</p> </li>
<li> <p>Immutable priority queue based on finger trees (<code>fj.data.PriorityQueue</code>).</p> </li>
<li> <p>Type-safe heterogeneous list (<code>fj.data.hlist</code>) for lists of elements of differing types without sacrificing type-safety.</p> </li>
<li> <p>Immutable set implementation using a red/black tree (<code>fj.data.Set</code>).</p> </li>
<li> <p>Immutable multi-way tree - aka rose tree (<code>fj.data.Tree</code>).</p> </li>
<li> <p>Immutable tree-map using a red/black tree implementation (<code>fj.data.TreeMap</code>).</p> </li>
<li> <p>Difference lists, a highly performant list.</p> </li>
</ul>
</div> </li>
<li> <p>Other</p>
<div class="ulist">
<ul>
<li> <p>Monoid (<code>fj.Monoid</code>).</p> </li>
<li> <p>Semigroup (<code>fj.Semigroup</code>).</p> </li>
<li> <p>Natural number data type (<code>fj.data.Natural</code>).</p> </li>
<li> <p>Random number generator using a <em>linear congruential generator</em> (<code>fj.LcgRng</code>).</p> </li>
<li> <p>Reader, Writer and State monads (<code>fj.data.Reader</code>,<code>fj.data.Writer</code>, <code>fj.data.State</code>).</p> </li>
<li> <p>Input/Output monad for abstracting IO (<code>fj.IO</code>).</p> </li>
<li> <p>Monadic parser combinators for writing parsers by combining smaller parsers using composition.</p> </li>
<li> <p>Conversion of data types to/from standard Java types.</p> </li>
<li> <p>Conversion between FunctionalJava and Java 8 specific types.</p> </li>
<li> <p>Configurable equality and hash-code for HashMap and HashSet.</p> </li>
<li> <p>Zipper implementations for streams and trees.</p> </li>
<li> <p>Automated specification-based testing framework (<code>fj.test</code>).</p> </li>
<li> <p>Fully operational Actors for parallel computations (<code>fj.control.parallel</code>) and layered abstractions such as parallel-map, map-reduce, parallel-zip.</p> </li>
<li> <p>Optics for updating immutable data including lens, prism, iso, optional, traversal, getter, fold and setter. Inspired by the Scala Monocle library (<a href="https://github.com/julien-truffaut/Monocle" class="bare">https://github.com/julien-truffaut/Monocle</a>) and the Haskell lens library (<a href="https://github.com/ekmett/lens" class="bare">https://github.com/ekmett/lens</a>).</p> </li>
</ul>
</div> </li>
</ul>
</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>