KEMBAR78
Javaslang - Functional Sugar For Java | PDF
JΛVΛSLΛNGFunctional Sugar For Java
slang (noun ˈslaŋ)
1. language peculiar to a
particular group
2. a non-standard vocabulary
composed of words and senses
characterized primarily by
connotations of extreme
informality
Functional Progamming?
Side-Effects
Referential Transparency
Thinking in Values
Side-Effects
Exceptions are non-local goto-statements
try {
int i = 1/0;
} catch (Throwable t) {
…
} :(
Referential Transparency
Math.random();
Math.max(1, 2);
Pure functions are a Good ThingTm
Side-effect :(
Thinking in Values
Because Rich Hickey is always right
A taste of JΛVΛSLΛNG!
Immutable Collections
The Try Monad
Structural Decomposition
Immutable Collections
Image by Douglas Muth, https://flic.kr/p/acFwxG
Mutable Collections
Returning void is a code smell!
interface Collection<E> {
…
void clear();
} Side-effect :(
Functional Data Structures
Immutable
Persistent
Referential transparent
(and fully integrated with java.util.*)
List<~> t = List.of(withName(“F95”));
List<~> t2 = t.append(withName(“FCK”));
List
F95 /t
List<~> t = List.of(withName(“F95”));
List<~> t2 = t.append(withName(“FCK”));
List
F95 /
FCK
t
t2
List<~> t = List.of(withName(“F95”));
List<~> t2 = t.append(withName(“FCK”));
List
F95 /
FCK
t
t2
The Try Monad
Classic Exception Handling
try {

URL url = new URL(“http://…”);
…

}
catch (MalformedURLException e)
{ … }
catch (IOException e) { … }
Exception Handling as a Monad
Try.of(() -> new URL(“http://…”))

.flatMap(
u->Try.of(u::openConnection)
)
.flatMap(
c->Try.of(c::getInputStream)
)…
Recovering from failure
Try.of(() -> new URL(“http://…”))
…
.recover(
t-> singletonList(t.getMessage())
)
.getOrElse(emptyList());
Structural Decomposition
Image by Arend, https://flic.kr/p/pkBe4g
Handling a HTTP response the classic way
if (OK.equals(res.getStatusCode()))
{
return exchange.getBody();
}
else {
return emptyList();
}
…now the JΛVΛSLΛNG way
Match(res.getStatusCode())
.of(

Case($OK),
res.getBody()),

Case(API.<HttpStatus>$(),
emptyList())

);
Guard Clauses
Match(Option.of("bla")).of(

Case(Some(
$(v -> v.length()>4)),
identity()),

Case(Some($()), "other")

)
An example from the trenches
Image by Shawn Carpenter, https://flic.kr/p/7FMFjC
A taste, classic stuff
public void reset(IModel<?> model) {
MyLadenModel<?, ?> myLadenModel;
if (model instanceof IResettableWrapModel) {
IResettableWrapModel<?> res = (IResettableWrapModel<?>) model;
myLadenModel = (AbstractKontenLadenModel<?, ?>)
res.getWrappedModel();
} else if (model instanceof AbstractLadenModel) {
myLadenModel = (AbstractLadenModel<?, ?>) model;
} else {
throw new IllegalArgumentException(“Peng!”);
}
kontenLadenModel.reset();
}
JΛVΛSLΛNG to the rescue
public void reset(IModel<?> model) {

MyLadenModel<?, ?> myLadenModel =
Match(model).of(

Case($(instanceOf(IResettableWrapModel.class)),
res->res.getWrappedModel()),

Case($(instanceOf(AbstractLadenModel.class)),
identity())

);
myLadenModel.reset();

}
We can’t cover everything
Collections Seq, Set, Tree…
Functional Sugar Lift, Memoize, Currying
Values Option, Lazy, Future
Extensions Circuit Breaker, Property Test
But wait…what are the drawbacks?
Java’s baroque type-system
Functional Programming…use Clojure
#Collection-lib > #Logging Impl
Do you want to know more?
Javaslang Homepage
http://www.javaslang.io/javaslang-docs/
Mario Fusco, From object oriented to functional domain modeling
http://bit.ly/28Nj1Ut
THANK YOU!
<david.schmitz@senacor.com> @koenighotze

Javaslang - Functional Sugar For Java