SlideShare une entreprise Scribd logo
1  sur  28
Télécharger pour lire hors ligne
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

Contenu connexe

Plus de David Schmitz

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-vxdparisDavid Schmitz
 
Vavr Java User Group Rheinland
Vavr Java User Group RheinlandVavr Java User Group Rheinland
Vavr Java User Group RheinlandDavid Schmitz
 
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 @ DevoxxDavid Schmitz
 
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-DesasterDavid Schmitz
 
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 2018David Schmitz
 
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 learnedDavid Schmitz
 
The FaaS and the Furious
The FaaS and the FuriousThe FaaS and the Furious
The FaaS and the FuriousDavid Schmitz
 
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)David Schmitz
 
Javaslang Talk @ Javaland 2017
Javaslang Talk @ Javaland 2017Javaslang Talk @ Javaland 2017
Javaslang Talk @ Javaland 2017David Schmitz
 
10 Tips for failing at microservices
10 Tips for failing at microservices10 Tips for failing at microservices
10 Tips for failing at microservicesDavid Schmitz
 
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 2016David Schmitz
 
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 developersDavid Schmitz
 
Resilience testing with Wiremock and Spock
Resilience testing with Wiremock and SpockResilience testing with Wiremock and Spock
Resilience testing with Wiremock and SpockDavid Schmitz
 
Docker for the Brave
Docker for the BraveDocker for the Brave
Docker for the BraveDavid Schmitz
 
Spring boot - Getting Started
Spring boot - Getting StartedSpring boot - Getting Started
Spring boot - Getting StartedDavid Schmitz
 

Plus de 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
 

Dernier

Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxRustici Software
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...apidays
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...apidays
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...apidays
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelDeepika Singh
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfOverkill Security
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 

Dernier (20)

Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 

Javaslang - Functional Sugar For Java