Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
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
ch...
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 (IOExcep...
Exception Handling as a Monad
Try.of(() -> new URL(“http://…”))

.flatMap(
u->Try.of(u::openConnection)
)
.flatMap(
c->Try...
Recovering from failure
Try.of(() -> new URL(“http://…”))
…
.recover(
t-> singletonList(t.getMessage())
)
.getOrElse(empty...
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 ...
…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 IResetta...
JΛVΛSLΛNG to the rescue
public void reset(IModel<?> model) {

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

Case($(i...
We can’t cover everything
Collections Seq, Set, Tree…
Functional Sugar Lift, Memoize, Currying
Values Option, Lazy, Future...
But wait…what are the drawbacks?
Java’s baroque type-system
Functional Programming…use Clojure
#Collection-lib > #Logging ...
Do you want to know more?
Javaslang Homepage
http://www.javaslang.io/javaslang-docs/
Mario Fusco, From object oriented to ...
THANK YOU!
<david.schmitz@senacor.com> @koenighotze
Upcoming SlideShare
Loading in …5
×

Javaslang - Functional Sugar For Java

2,413 views

Published on

Short introduction to Javaslang's features and API power

Published in: Technology

Javaslang - Functional Sugar For Java

  1. 1. JΛVΛSLΛNGFunctional Sugar For Java
  2. 2. 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
  3. 3. Functional Progamming? Side-Effects Referential Transparency Thinking in Values
  4. 4. Side-Effects Exceptions are non-local goto-statements try { int i = 1/0; } catch (Throwable t) { … } :(
  5. 5. Referential Transparency Math.random(); Math.max(1, 2); Pure functions are a Good ThingTm Side-effect :(
  6. 6. Thinking in Values Because Rich Hickey is always right
  7. 7. A taste of JΛVΛSLΛNG! Immutable Collections The Try Monad Structural Decomposition
  8. 8. Immutable Collections Image by Douglas Muth, https://flic.kr/p/acFwxG
  9. 9. Mutable Collections Returning void is a code smell! interface Collection<E> { … void clear(); } Side-effect :(
  10. 10. Functional Data Structures Immutable Persistent Referential transparent (and fully integrated with java.util.*)
  11. 11. List<~> t = List.of(withName(“F95”)); List<~> t2 = t.append(withName(“FCK”)); List F95 /t
  12. 12. List<~> t = List.of(withName(“F95”)); List<~> t2 = t.append(withName(“FCK”)); List F95 / FCK t t2
  13. 13. List<~> t = List.of(withName(“F95”)); List<~> t2 = t.append(withName(“FCK”)); List F95 / FCK t t2
  14. 14. The Try Monad
  15. 15. Classic Exception Handling try {
 URL url = new URL(“http://…”); …
 } catch (MalformedURLException e) { … } catch (IOException e) { … }
  16. 16. Exception Handling as a Monad Try.of(() -> new URL(“http://…”))
 .flatMap( u->Try.of(u::openConnection) ) .flatMap( c->Try.of(c::getInputStream) )…
  17. 17. Recovering from failure Try.of(() -> new URL(“http://…”)) … .recover( t-> singletonList(t.getMessage()) ) .getOrElse(emptyList());
  18. 18. Structural Decomposition Image by Arend, https://flic.kr/p/pkBe4g
  19. 19. Handling a HTTP response the classic way if (OK.equals(res.getStatusCode())) { return exchange.getBody(); } else { return emptyList(); }
  20. 20. …now the JΛVΛSLΛNG way Match(res.getStatusCode()) .of(
 Case($OK), res.getBody()),
 Case(API.<HttpStatus>$(), emptyList())
 );
  21. 21. Guard Clauses Match(Option.of("bla")).of(
 Case(Some( $(v -> v.length()>4)), identity()),
 Case(Some($()), "other")
 )
  22. 22. An example from the trenches Image by Shawn Carpenter, https://flic.kr/p/7FMFjC
  23. 23. 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(); }
  24. 24. 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();
 }
  25. 25. We can’t cover everything Collections Seq, Set, Tree… Functional Sugar Lift, Memoize, Currying Values Option, Lazy, Future Extensions Circuit Breaker, Property Test
  26. 26. But wait…what are the drawbacks? Java’s baroque type-system Functional Programming…use Clojure #Collection-lib > #Logging Impl
  27. 27. 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
  28. 28. THANK YOU! <david.schmitz@senacor.com> @koenighotze

×