SlideShare a Scribd company logo
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

More Related Content

More from David Schmitz

More from David Schmitz (17)

Going Cloud Native
Going Cloud NativeGoing Cloud Native
Going Cloud Native
 
Eventsourcing you-are-doing-it-wrong-vxdparis
Eventsourcing you-are-doing-it-wrong-vxdparisEventsourcing you-are-doing-it-wrong-vxdparis
Eventsourcing you-are-doing-it-wrong-vxdparis
 
Vavr Java User Group Rheinland
Vavr Java User Group RheinlandVavr Java User Group Rheinland
Vavr Java User Group Rheinland
 
Event Sourcing - You are doing it wrong @ Devoxx
Event Sourcing - You are doing it wrong @ DevoxxEvent Sourcing - You are doing it wrong @ Devoxx
Event Sourcing - You are doing it wrong @ Devoxx
 
10 Tipps für ein absolutes Microservice-Desaster
10 Tipps für ein absolutes Microservice-Desaster10 Tipps für ein absolutes Microservice-Desaster
10 Tipps für ein absolutes Microservice-Desaster
 
10 tips for failing at microservices @ DevExperience 2018
10 tips for failing at microservices @ DevExperience 201810 tips for failing at microservices @ DevExperience 2018
10 tips for failing at microservices @ DevExperience 2018
 
Real world serverless - architecture, patterns and lessons learned
Real world serverless - architecture, patterns and lessons learnedReal world serverless - architecture, patterns and lessons learned
Real world serverless - architecture, patterns and lessons learned
 
The FaaS and the Furious
The FaaS and the FuriousThe FaaS and the Furious
The FaaS and the Furious
 
10 Tips for failing at microservices - badly (BedCon 2017)
10 Tips for failing at microservices - badly (BedCon 2017)10 Tips for failing at microservices - badly (BedCon 2017)
10 Tips for failing at microservices - badly (BedCon 2017)
 
Javaslang Talk @ Javaland 2017
Javaslang Talk @ Javaland 2017Javaslang Talk @ Javaland 2017
Javaslang Talk @ Javaland 2017
 
10 Tips for failing at microservices
10 Tips for failing at microservices10 Tips for failing at microservices
10 Tips for failing at microservices
 
Elixir - Easy fun for busy developers @ Devoxx 2016
Elixir - Easy fun for busy developers @ Devoxx 2016Elixir - Easy fun for busy developers @ Devoxx 2016
Elixir - Easy fun for busy developers @ Devoxx 2016
 
Javaslang @ Devoxx
Javaslang @ DevoxxJavaslang @ Devoxx
Javaslang @ Devoxx
 
Bootstrap |> Elixir - Easy fun for busy developers
Bootstrap |> Elixir - Easy fun for busy developersBootstrap |> Elixir - Easy fun for busy developers
Bootstrap |> Elixir - Easy fun for busy developers
 
Resilience testing with Wiremock and Spock
Resilience testing with Wiremock and SpockResilience testing with Wiremock and Spock
Resilience testing with Wiremock and Spock
 
Docker for the Brave
Docker for the BraveDocker for the Brave
Docker for the Brave
 
Spring boot - Getting Started
Spring boot - Getting StartedSpring boot - Getting Started
Spring boot - Getting Started
 

Recently uploaded

Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
Safe Software
 
Search and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical FuturesSearch and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical Futures
Bhaskar Mitra
 

Recently uploaded (20)

Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptxUnpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
 
Search and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical FuturesSearch and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical Futures
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
 
ODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User GroupODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User Group
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
 
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptxIOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
 
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
 
"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi
 
10 Differences between Sales Cloud and CPQ, Blanka Doktorová
10 Differences between Sales Cloud and CPQ, Blanka Doktorová10 Differences between Sales Cloud and CPQ, Blanka Doktorová
10 Differences between Sales Cloud and CPQ, Blanka Doktorová
 
SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...
SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...
SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...
 
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
 

Javaslang - Functional Sugar For Java