Skip to content

Commit a863d6f

Browse files
committed
Updated features with Google site examples
1 parent 9e2c7a4 commit a863d6f

File tree

1 file changed

+152
-0
lines changed

1 file changed

+152
-0
lines changed

‎src/jbake/content/features.adoc

+152
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,158 @@
33
:jbake-tags:
44
:jbake-status: published
55

6+
== First-Class Functions
7+
8+
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.
9+
10+
Functions are written with anonymous class syntax:
11+
12+
[source,java]
13+
----
14+
// Regular Style
15+
Integer timesTwo(Integer i) { return i * 2; }
16+
// Functional Style
17+
F timesTwo = new F() { public Integer f(Integer i) { return i * 2; } }
18+
----
19+
20+
First-class functions can be composed and passed as arguments to higher-order functions:
21+
22+
[source,java]
23+
----
24+
// Regular Style
25+
Integer timesTwoPlusOne(Integer i) { return plusOne(timesTwo(i)); }
26+
// Functional Style
27+
F timesTwoPlusOne = plusOne.o(timesTwo);
28+
----
29+
30+
And mapped over collections:
31+
32+
[source,java]
33+
----
34+
//Regular Style
35+
List oneUp = new ArrayList();
36+
for (Integer i: ints) oneUp.add(plusOne(i));
37+
// Functional Style
38+
List oneUp = ints.map(plusOne);
39+
----
40+
41+
Functions up to arity-8 are supported, allowing elimination of nested control constructs:
42+
43+
[source,java]
44+
----
45+
// Regular Style
46+
Integer product = 1;
47+
for (Integer x: ints) product = x * product;
48+
List products1 = new ArrayList();
49+
for (int x = 0; x < ints.size(); x++) {
50+
for (int y = 0; y <= x; y++) {
51+
products.add(ints.get(x) * ints.get(y);
52+
}
53+
}
54+
List products2 = new ArrayList();
55+
for (Integer x: ints) {
56+
for (Integer y: ints) {
57+
products.add(x * y);
58+
}
59+
}
60+
// Functional Style
61+
Integer product = ints.foldLeft(1, multiply);
62+
List products1 = ints.tails().apply(ints.map(multiply));
63+
List products2 = ints.bind(ints, multiply);
64+
----
65+
66+
== Immutable Datastructures
67+
68+
Functional Java implements many immutable datastructures, such as
69+
70+
* Singly-linked lists (`fj.data.List`)
71+
* Non-strict, potentially infinite, singly-linked list (`fj.data.Stream`)
72+
* Non-strict, potentially infinite Strings (`fj.data.LazyString`)
73+
* A wrapper for arrays (`fj.data.Array`)
74+
* A wrapper for any Iterable type (`fj.data.IterableW`)
75+
* Immutable ordered sets (`fj.data.Set`)
76+
* Multi-way trees -- a.k.a. rose trees, with support for infinite trees (`fj.data.Tree`)
77+
* Immutable Map, with single-traversal search-and-update (`fj.data.TreeMap`)
78+
* Type-safe heterogeneous lists (`fj.data.hlist`)
79+
* Pointed lists and trees (`fj.data.Zipper` and `fj.data.TreeZipper`)
80+
81+
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).
82+
83+
Efficient conversions to and from the standard Java Collections classes are provided, and `java.util.Iterable` is implemented where possible, for use with Java's foreach syntax.
84+
85+
== Optional Values (type-safe null)
86+
87+
The library provides a datatype for variables, parameters, and return values that may have no value, while remaining type-safe.
88+
89+
[source,java]
90+
----
91+
// Using null
92+
String val = map.get(key);
93+
if (val == null || val.equals("")) val = "Nothing";
94+
return val;
95+
// Using Option
96+
return fromString(map.get(key)).orSome("Nothing");
97+
----
98+
99+
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.
100+
101+
== Product Types
102+
103+
Joint union types (tuples) are products of other types. Products of arities 1-8 are provided (`fj.P1` - `fj.P8`). 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).
104+
105+
Example:
106+
107+
[source,java]
108+
----
109+
// Regular Java
110+
public Integer albuquerqueToLA(Map> map) {
111+
Map m = map.get("Albuquerque");
112+
if (m != null) return m.get("Los Angeles"); // May return null.
113+
}
114+
// Functional Java with product and option types.
115+
public Option albuquerqueToLA(TreeMap, Integer>() map) {
116+
return m.get(p("Albuquerque", "Los Angeles"));
117+
}
118+
----
119+
120+
== Disjoint Union Types
121+
122+
By the same token, types can be added by disjoint union. Values of type `Either<A, B>` contain a value of either type `A` or type `B`. 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:
123+
124+
[source,java]
125+
----
126+
// Meanwhile, inside an iterator implementation...
127+
public Either next() {
128+
String s = moreInput();
129+
try {
130+
return Either.right(Integer.valueOf(s));
131+
} catch (Exception e) {
132+
return Either.left(Fail.invalidInteger(s));
133+
}
134+
}
135+
----
136+
137+
The `Either` 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.
138+
139+
http://apocalisp.wordpress.com/2008/06/04/throwing-away-throws[See here for a more detailed explanation of using `Either` for handling errors.]
140+
141+
== Higher-Order Concurrency Abstractions
142+
143+
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:
144+
145+
[source,java]
146+
----
147+
Strategy s = simpleThreadStrategy();
148+
List ns = range(Integer.MIN_VALUE, Integer.MIN_VALUE + 10).map(negate).toList();
149+
List bs = s.parMap(ns, isPrime);
150+
----
151+
152+
Also included is an implementation of the actor model (`fj.control.parallel.Actor` and `QueueActor`), and `Promise`, which is a composable and non-blocking version of `java.util.concurrent.Future`.
153+
154+
http://apocalisp.wordpress.com/2008/06/30/parallel-list-transformations[A series of blog posts on the concurrency features can be found here.]
155+
156+
== Abstractions
157+
6158
Functional Java provides abstractions for the following types:
7159

8160
* Basic Data Structures

0 commit comments

Comments
 (0)