https://github.com/howardabrams/fp-for-java
Demonstrating some functional programming principles in Java... it isn't as bad as you'd think (but still not nice)
https://github.com/howardabrams/fp-for-java
Last synced: about 2 months ago
JSON representation
Demonstrating some functional programming principles in Java... it isn't as bad as you'd think (but still not nice)
- Host: GitHub
- URL: https://github.com/howardabrams/fp-for-java
- Owner: howardabrams
- License: apache-2.0
- Created: 2012-08-28T06:12:15.000Z (almost 14 years ago)
- Default Branch: master
- Last Pushed: 2012-10-14T01:08:51.000Z (over 13 years ago)
- Last Synced: 2025-12-29T00:08:55.852Z (6 months ago)
- Language: Java
- Size: 176 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE-2.0
Awesome Lists containing this project
README
Function Programming Ideas for Java
===================================
The source code in this project (and its accompanying presentation)
demonstrate some principles of *functional programming* using standard
Java.
The concepts fall into the following categories:
Optional Parameters
-------------------
In order to get rid of the dreaded null pointer exceptions, you can
have a function accept parameters of type [Option][]. Only two classes
implement this interface: `None` and `Some`.
* [None][]: returns `false` when `isPresent()` function is called.
* [Some][]: returns `true` when `isPresent()` called, and returns results from `values()`
We can then have a function like:
public FlexiMap( Option putfn, Option getfn ) {
if(putfn.isPresent())
onPut = putfn.get();
if(getfn.isPresent())
onGet = getfn.get();
// ...
}
This process makes it less likely to encounter with `null` values.
Closures
--------
The [Closure][] interface has a single... ahem... function inside:
Object apply(Object ...objects);
This allows anonymous inner classes to pass simple functions around.
Of course, I find the syntax for anonymous inner classes quite icky,
and prefer to create private inner classes instead:
private class DefaultValueForNull implements Closure {
final Object defaultValue;
public DefaultValueForNull(Object value) {
this.defaultValue = value;
}
public Object apply(Object... objects) {
if (objects[0] == null)
return defaultValue;
else
return objects[0];
}
}
Composition
-----------
To demonstrate the concept of creating specialized versions of a `Map` through
*composition* (instead of inheritance), we took [these unit tests][1] from
the Apache [Commons Functor library][2], but instead of using their libraries,
we used our own `Closure` classes (see [these this code][Test]).
Same principle, different approach.
[1]: http://svn.apache.org/repos/asf/commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/example/FlexiMapExample.java
[2]: http://commons.apache.org/functor/index.html
[Closure]: /howardabrams/fp-for-java/blob/master/src/main/java/org/howardism/fpjava/Closure.java
[Option]: /howardabrams/fp-for-java/blob/master/src/main/java/org/howardism/fpjava/Option.java
[None]: /howardabrams/fp-for-java/blob/master/src/main/java/org/howardism/fpjava/None.java
[Some]: /howardabrams/fp-for-java/blob/master/src/main/java/org/howardism/fpjava/Some.java
[Test]: /howardabrams/fp-for-java/blob/master/src/test/java/org/howardism/fpjava/FlexiMapTest.java