SlideShare una empresa de Scribd logo
1 de 63
Deconstructing Functional
Programming
Gilad Bracha

Wednesday, November 13, 13
Watch the video with slide
synchronization on InfoQ.com!
http://www.infoq.com/presentations
/functional-pros-cons

InfoQ.com: News & Community Site
• 750,000 unique visitors/month
• Published in 4 languages (English, Chinese, Japanese and Brazilian
Portuguese)
• Post content from our QCon conferences
• News 15-20 / week
• Articles 3-4 / week
• Presentations (videos) 12-15 / week
• Interviews 2-3 / week
• Books 1 / month
Presented at QCon San Francisco
www.qconsf.com
Purpose of QCon
- to empower software development by facilitating the spread of
knowledge and innovation
Strategy
- practitioner-driven conference designed for YOU: influencers of
change and innovation in your teams
- speakers and topics driving the evolution and innovation
- connecting and catalyzing the influencers and innovators
Highlights
- attended by more than 12,000 delegates since 2007
- held in 9 cities worldwide
The Heart of Darkness

Wednesday, November 13, 13
What is Functional
Programming?
Definitions vary, but tend to involve
Higher order functions (HOFs)
Absence of effects

Wednesday, November 13, 13
Higher Order Functions
Functions are values
Can be passed as arguments and returned as results
Function literals are expressions that can be written
anywhere an expression can appear

Wednesday, November 13, 13
Languages with HOFs

Smalltalk, Self, Newspeak, Dart, Javascript ... and now
even Java (gasp)

Wednesday, November 13, 13
Languages with Effects

Lisp, Scheme, Racket, Clojure, ML, OCaml, F#, Erlang

Wednesday, November 13, 13
Languages w/o HOFs

Backus’ FP, SISAL, the original versions of Erlang ...
These are functional languages; in fact FP was the
original functional language

Wednesday, November 13, 13
What is Functional
Programming?
1. A style which utilizes HOFs and minimizes effects
2. A cult(ure)
3. All of the above
In particular, there is no contradiction with object
orientation (see Scala as an example)

Wednesday, November 13, 13
HOFs/Closures
HOFs are awesome. Lambdas even more so.
The great classical HOFs: map, filter, reduce
They’ve been around since Lisp & Smalltalk (i.e., long
before the term FP was introduced)

Wednesday, November 13, 13
User-defined Control
Structures

a > b ifTrue:[ a - b].

Wednesday, November 13, 13
User-defined Control
Structures

a > b ifTrue:[ a - b].
A boolean, an object, the target of ...

Wednesday, November 13, 13
User-defined Control
Structures

a > b ifTrue:[ a - b].
The method, invoked with ...

Wednesday, November 13, 13
User-defined Control
Structures

a > b ifTrue:[ a - b].
The argument, a block/lambda/closure

Wednesday, November 13, 13
User-defined Control
Structures
a > b ifTrue:[ a - b].
In class True, ifTrue: evaluates its argument and returns
the result.
In class False, ifTrue: returns nil.

Wednesday, November 13, 13
Tail Calls
Look ma, no loops:
while(b, s) { if b() then {s(); while(b,s)} else null}
How to express computation unbounded in time but
bounded in space.
A language is functional if it supports proper tail calls?

Wednesday, November 13, 13
Tail Calls
What about debugging? Where’s my stack trace?

http://gbracha.blogspot.com/2009/12/chased-byones-own-tail.html

Wednesday, November 13, 13
Hindley-Milner

If it typechecks, it works .... ?
But error messages are terrible
Leaks implementation information

Wednesday, November 13, 13
Currying
All functions take one argument, produce one result
Extremely compositional, BUT
We lose almost all structure
Forced to rely on type system

Wednesday, November 13, 13
Pattern Matching
Nice
Allows FPLs to pretend they don’t do dynamic typechecks
and casts
Usually second class
First class patterns are interesting
http://gbracha.blogspot.com/2010/05/patterns-of-dynamic-typechecking.html
http://gbracha.blogspot.com/2010/06/patterns-as-objects-in-newspeak.html

Wednesday, November 13, 13
Monads

Wednesday, November 13, 13
A Monad by any other name
would smell ...

Wednesday, November 13, 13
A Monad by any other name
would smell as sweet

Wednesday, November 13, 13
Ye Highlands and ye Lowlands,
Oh, where hae ye been?
They hae slain the Earl O' Moray,
And laid him on the green.
The Bonny Earl O’Moray

Wednesday, November 13, 13
Ye Highlands and ye Lowlands,
Oh, where hae ye been?
They hae slain the Earl O' Moray,
And Lady Mondegreen.
The Bonny Earl O’Moray, revised by Sylvia Wright

Wednesday, November 13, 13
Ye Highlands and ye Lowlands,
Oh, where hae ye been?
They hae slain the Earl O' Moray,
And Lady Monadgreen.
The Bonny Earl O’Moray, revised by me

Wednesday, November 13, 13
Monad Metaphors, aka
Monadgreens
Nuclear waste containers
Romantic conquests
Space Suits
Monsters
Macros
Containers
Conversations
Black holes
Wednesday, November 13, 13
Lady Monadgreen’s Curse

Once you understand monads, you immediately
become incapable of explaining them to anyone else

Wednesday, November 13, 13
Monads

http://gbracha.blogspot.com/2011/01/maybemonads-might-not-matter.html

Wednesday, November 13, 13
abstract class FlatMappable {
FlatMappable(a);
flatMap(f);
}

Wednesday, November 13, 13
abstract class Monad {
Monad(a);
flatMap(f);
}

Wednesday, November 13, 13
abstract class Monad {
Monad.unit(a);
flatMap(f);
}

Wednesday, November 13, 13
abstract class Monad {
Monad.return(a);
flatMap(f);
}

Wednesday, November 13, 13
abstract class Monad {
Monad.return(a);
bind(f);
}

Wednesday, November 13, 13
abstract class Monad {
Monad.return(a);
operator * (f);
}

Wednesday, November 13, 13
abstract class Monad {
Monad.return(a);
operator >>= (f);
}

Wednesday, November 13, 13
abstract class FlatMappable {
FlatMappable(a);
flatMap(f); // map, then flatten
}

Wednesday, November 13, 13
abstract class Mappable {
Mappable(a);
map(f);
}
abstract class MappableAndFlattenable
extends Mappable {
MappableAndFlattenable(a): super(a);
flatten();
}
Wednesday, November 13, 13
abstract class FlatMappable extends
MappableAndFlattenable {
FlatMappable(a): super(a);
flatMap(f) => map(f).flatten();
}

Wednesday, November 13, 13
What’s the Contract Like?
Sample Clause:
new Mappable(x).map(f) ==
new Mappable( f(x));

Wednesday, November 13, 13
What’s the Contract Like?
Sample Clause:
new FlatMappable(x).flatMap(f) == f(x);

Wednesday, November 13, 13
What’s the Contract Like?
Sample Clause:
new C(x).flatMap(f) == f(x);
for any class C that implements the contract

Wednesday, November 13, 13
The Whole Contract
new C(x).flatMap(f) == f(x);
c.flatMap((x)=> new C(x)) == c;
c.flatMap((x)=> f(x).flatMap(g)) ==
c.flatMap(f).flatMap(g);

Wednesday, November 13, 13
FlatMappable is more useful
than you think

Collections of all kinds: in-memory, databases, streams
“Singleton collections”: functions, continuations, futures

Wednesday, November 13, 13
A simple expression

e.b.c
what could possibly go wrong?

Wednesday, November 13, 13
A simple expression
e.b.c
what could possibly go wrong?
e could be null. Ok
e == null ? null : e.b.c

Wednesday, November 13, 13
A simple expression
e.b.c
what could possibly go wrong?
e could be null. Ok
e == null ? null : e.b.c
oops, e.b could be null
e == null ? null : e.b == null ? null : e.b.c

Wednesday, November 13, 13
A simple expression
e == null ? null : e.b == null ? null : e.b.c
what about side effects?
(var it = e) == null ?
null :
(var ab = it.b) == null ?
null :
ab.c
Wednesday, November 13, 13
Safe Navigation Operator
e?.b?.c
e?.id(a1, .. an) is sugar for
e.ifNotNull((x) => x.id(a1, .. an))
where we define
class Object {
ifNotNull(f) => this == null ? null : f(this);
}

Wednesday, November 13, 13
Promise Pipelining
e <- id(a1, .. an) is sugar for
e.then((x) => x.id(a1, .. an))
where we define
class Object {
then(f) => f(this);
}

Wednesday, November 13, 13
Safe Promise Pipelining
e <- id(a1, .. an) is sugar for
e.then((x) => x.id(a1, .. an))
where we define
class Object {
then(f) => this == null ? null : f(this);
}

Wednesday, November 13, 13
Async message pipelining
e <- id(a1, .. an) is sugar for
e.then((x) => x.id(a1, .. an))
where we define
class Object {
then(f) => Future.immediate(this).then(f);
}

Wednesday, November 13, 13
Scalar operators extended
pointwise to collections
e.* id(a1, .. an) is sugar for
e.map((x) => x.id(a1, .. an))
where we define
class Object {
map(f) => f(this);
}
[‘abc’, ‘de’, ‘wxyz’].* length evaluates to [3, 2, 4]
Wednesday, November 13, 13
Scalar operators extended
pointwise to collections
e.* id(a1, .. an) is sugar for
e.map((x) => x.id(a1, .. an))
where we define
class Object {
map(f) => this == null ? null : f(this);
}
[‘abc’, null, ‘wxyz’].* length evaluates to [3, null, 4]
Wednesday, November 13, 13
Stream transformers

mouseclicks.* x

Wednesday, November 13, 13
Is There a Pattern Here?
The Curse of the Monadgreens prevents me from
discussing this further
In practice, we see generalizations of set notation
rather than navigation
LINQ is an example. However, you didn’t need to have
even heard of monads to invent LINQ. Just look at
Smalltalk’s collection API, Glorp etc.
http://gbracha.blogspot.com/2011/01/maybemonads-might-not-matter.html
Wednesday, November 13, 13
The Killer App for FP?

Wednesday, November 13, 13
Wednesday, November 13, 13
The Killer App for FP?

Live Programming
http://gbracha.blogspot.com/2012/11/debug-mode-is-only-mode.html
http://gbracha.blogspot.com/2013/04/making-methods-live.html
https://www.youtube.com/watch?v=74WqdS_58uY

Wednesday, November 13, 13
Summary
Much to learn from FP
FP and OOP are often complementary
Filter out propaganda
Separate cultural baggage from core values

Wednesday, November 13, 13
The Newspeak eye
on slide 5 was designed by
Victoria Bracha and is used by permission.

The cartoon on slide 2 comes from http://xkcd.com/
1270/ and is licensed by xkcd.com under http://
creativecommons.org/licenses/by-nc/2.5/
The rose on slide 16 is by Kikuo Teranishi and licensed
under http://creativecommons.org/licenses/by-sa/3.0/
deed.en

Wednesday, November 13, 13
Watch the video with slide synchronization on
InfoQ.com!
http://www.infoq.com/presentations/functional
-pros-cons

Más contenido relacionado

Similar a Deconstructing Functional Programming

First Ride on Rust
First Ride on RustFirst Ride on Rust
First Ride on RustDavid Evans
 
Localizing iOS Apps
Localizing iOS AppsLocalizing iOS Apps
Localizing iOS Appsweissazool
 
Municipal Government Meets NoSQL
Municipal Government Meets NoSQLMunicipal Government Meets NoSQL
Municipal Government Meets NoSQLMongoDB
 
The Not Java That's Not Scala
The Not Java That's Not ScalaThe Not Java That's Not Scala
The Not Java That's Not ScalaJustin Lee
 
Rupy2012 ArangoDB Workshop Part1
Rupy2012 ArangoDB Workshop Part1Rupy2012 ArangoDB Workshop Part1
Rupy2012 ArangoDB Workshop Part1ArangoDB Database
 
Elasticsearch – mye mer enn søk! [JavaZone 2013]
Elasticsearch – mye mer enn søk! [JavaZone 2013]Elasticsearch – mye mer enn søk! [JavaZone 2013]
Elasticsearch – mye mer enn søk! [JavaZone 2013]foundsearch
 
Semantic Pipes (London Perl Workshop 2009)
Semantic Pipes (London Perl Workshop 2009)Semantic Pipes (London Perl Workshop 2009)
Semantic Pipes (London Perl Workshop 2009)osfameron
 
03 introduction to graph databases
03   introduction to graph databases03   introduction to graph databases
03 introduction to graph databasesNeo4j
 
실시간 웹 협업도구 만들기 V0.3
실시간 웹 협업도구 만들기 V0.3실시간 웹 협업도구 만들기 V0.3
실시간 웹 협업도구 만들기 V0.3NAVER D2
 
Ks2009 Semanticweb In Action
Ks2009 Semanticweb In ActionKs2009 Semanticweb In Action
Ks2009 Semanticweb In ActionRinke Hoekstra
 
Baking Delicious Modularity in Scala
Baking Delicious Modularity in ScalaBaking Delicious Modularity in Scala
Baking Delicious Modularity in ScalaDerek Wyatt
 
Recommender Systems with Ruby (adding machine learning, statistics, etc)
Recommender Systems with Ruby (adding machine learning, statistics, etc)Recommender Systems with Ruby (adding machine learning, statistics, etc)
Recommender Systems with Ruby (adding machine learning, statistics, etc)Marcel Caraciolo
 

Similar a Deconstructing Functional Programming (20)

Invitation to Scala
Invitation to ScalaInvitation to Scala
Invitation to Scala
 
First Ride on Rust
First Ride on RustFirst Ride on Rust
First Ride on Rust
 
Dig1108 Lesson 3
Dig1108 Lesson 3Dig1108 Lesson 3
Dig1108 Lesson 3
 
Localizing iOS Apps
Localizing iOS AppsLocalizing iOS Apps
Localizing iOS Apps
 
Municipal Government Meets NoSQL
Municipal Government Meets NoSQLMunicipal Government Meets NoSQL
Municipal Government Meets NoSQL
 
The Not Java That's Not Scala
The Not Java That's Not ScalaThe Not Java That's Not Scala
The Not Java That's Not Scala
 
Rupy2012 ArangoDB Workshop Part1
Rupy2012 ArangoDB Workshop Part1Rupy2012 ArangoDB Workshop Part1
Rupy2012 ArangoDB Workshop Part1
 
StORM preview
StORM previewStORM preview
StORM preview
 
Effective Scala @ Jfokus
Effective Scala @ JfokusEffective Scala @ Jfokus
Effective Scala @ Jfokus
 
Elasticsearch – mye mer enn søk! [JavaZone 2013]
Elasticsearch – mye mer enn søk! [JavaZone 2013]Elasticsearch – mye mer enn søk! [JavaZone 2013]
Elasticsearch – mye mer enn søk! [JavaZone 2013]
 
Scala - Java2Days Sofia
Scala - Java2Days SofiaScala - Java2Days Sofia
Scala - Java2Days Sofia
 
Why Haskell
Why HaskellWhy Haskell
Why Haskell
 
Semantic Pipes (London Perl Workshop 2009)
Semantic Pipes (London Perl Workshop 2009)Semantic Pipes (London Perl Workshop 2009)
Semantic Pipes (London Perl Workshop 2009)
 
03 introduction to graph databases
03   introduction to graph databases03   introduction to graph databases
03 introduction to graph databases
 
실시간 웹 협업도구 만들기 V0.3
실시간 웹 협업도구 만들기 V0.3실시간 웹 협업도구 만들기 V0.3
실시간 웹 협업도구 만들기 V0.3
 
Ks2009 Semanticweb In Action
Ks2009 Semanticweb In ActionKs2009 Semanticweb In Action
Ks2009 Semanticweb In Action
 
Introduction to ansible
Introduction to ansibleIntroduction to ansible
Introduction to ansible
 
Ai 02
Ai 02Ai 02
Ai 02
 
Baking Delicious Modularity in Scala
Baking Delicious Modularity in ScalaBaking Delicious Modularity in Scala
Baking Delicious Modularity in Scala
 
Recommender Systems with Ruby (adding machine learning, statistics, etc)
Recommender Systems with Ruby (adding machine learning, statistics, etc)Recommender Systems with Ruby (adding machine learning, statistics, etc)
Recommender Systems with Ruby (adding machine learning, statistics, etc)
 

Más de C4Media

Streaming a Million Likes/Second: Real-Time Interactions on Live Video
Streaming a Million Likes/Second: Real-Time Interactions on Live VideoStreaming a Million Likes/Second: Real-Time Interactions on Live Video
Streaming a Million Likes/Second: Real-Time Interactions on Live VideoC4Media
 
Next Generation Client APIs in Envoy Mobile
Next Generation Client APIs in Envoy MobileNext Generation Client APIs in Envoy Mobile
Next Generation Client APIs in Envoy MobileC4Media
 
Software Teams and Teamwork Trends Report Q1 2020
Software Teams and Teamwork Trends Report Q1 2020Software Teams and Teamwork Trends Report Q1 2020
Software Teams and Teamwork Trends Report Q1 2020C4Media
 
Understand the Trade-offs Using Compilers for Java Applications
Understand the Trade-offs Using Compilers for Java ApplicationsUnderstand the Trade-offs Using Compilers for Java Applications
Understand the Trade-offs Using Compilers for Java ApplicationsC4Media
 
Kafka Needs No Keeper
Kafka Needs No KeeperKafka Needs No Keeper
Kafka Needs No KeeperC4Media
 
High Performing Teams Act Like Owners
High Performing Teams Act Like OwnersHigh Performing Teams Act Like Owners
High Performing Teams Act Like OwnersC4Media
 
Does Java Need Inline Types? What Project Valhalla Can Bring to Java
Does Java Need Inline Types? What Project Valhalla Can Bring to JavaDoes Java Need Inline Types? What Project Valhalla Can Bring to Java
Does Java Need Inline Types? What Project Valhalla Can Bring to JavaC4Media
 
Service Meshes- The Ultimate Guide
Service Meshes- The Ultimate GuideService Meshes- The Ultimate Guide
Service Meshes- The Ultimate GuideC4Media
 
Shifting Left with Cloud Native CI/CD
Shifting Left with Cloud Native CI/CDShifting Left with Cloud Native CI/CD
Shifting Left with Cloud Native CI/CDC4Media
 
CI/CD for Machine Learning
CI/CD for Machine LearningCI/CD for Machine Learning
CI/CD for Machine LearningC4Media
 
Fault Tolerance at Speed
Fault Tolerance at SpeedFault Tolerance at Speed
Fault Tolerance at SpeedC4Media
 
Architectures That Scale Deep - Regaining Control in Deep Systems
Architectures That Scale Deep - Regaining Control in Deep SystemsArchitectures That Scale Deep - Regaining Control in Deep Systems
Architectures That Scale Deep - Regaining Control in Deep SystemsC4Media
 
ML in the Browser: Interactive Experiences with Tensorflow.js
ML in the Browser: Interactive Experiences with Tensorflow.jsML in the Browser: Interactive Experiences with Tensorflow.js
ML in the Browser: Interactive Experiences with Tensorflow.jsC4Media
 
Build Your Own WebAssembly Compiler
Build Your Own WebAssembly CompilerBuild Your Own WebAssembly Compiler
Build Your Own WebAssembly CompilerC4Media
 
User & Device Identity for Microservices @ Netflix Scale
User & Device Identity for Microservices @ Netflix ScaleUser & Device Identity for Microservices @ Netflix Scale
User & Device Identity for Microservices @ Netflix ScaleC4Media
 
Scaling Patterns for Netflix's Edge
Scaling Patterns for Netflix's EdgeScaling Patterns for Netflix's Edge
Scaling Patterns for Netflix's EdgeC4Media
 
Make Your Electron App Feel at Home Everywhere
Make Your Electron App Feel at Home EverywhereMake Your Electron App Feel at Home Everywhere
Make Your Electron App Feel at Home EverywhereC4Media
 
The Talk You've Been Await-ing For
The Talk You've Been Await-ing ForThe Talk You've Been Await-ing For
The Talk You've Been Await-ing ForC4Media
 
Future of Data Engineering
Future of Data EngineeringFuture of Data Engineering
Future of Data EngineeringC4Media
 
Automated Testing for Terraform, Docker, Packer, Kubernetes, and More
Automated Testing for Terraform, Docker, Packer, Kubernetes, and MoreAutomated Testing for Terraform, Docker, Packer, Kubernetes, and More
Automated Testing for Terraform, Docker, Packer, Kubernetes, and MoreC4Media
 

Más de C4Media (20)

Streaming a Million Likes/Second: Real-Time Interactions on Live Video
Streaming a Million Likes/Second: Real-Time Interactions on Live VideoStreaming a Million Likes/Second: Real-Time Interactions on Live Video
Streaming a Million Likes/Second: Real-Time Interactions on Live Video
 
Next Generation Client APIs in Envoy Mobile
Next Generation Client APIs in Envoy MobileNext Generation Client APIs in Envoy Mobile
Next Generation Client APIs in Envoy Mobile
 
Software Teams and Teamwork Trends Report Q1 2020
Software Teams and Teamwork Trends Report Q1 2020Software Teams and Teamwork Trends Report Q1 2020
Software Teams and Teamwork Trends Report Q1 2020
 
Understand the Trade-offs Using Compilers for Java Applications
Understand the Trade-offs Using Compilers for Java ApplicationsUnderstand the Trade-offs Using Compilers for Java Applications
Understand the Trade-offs Using Compilers for Java Applications
 
Kafka Needs No Keeper
Kafka Needs No KeeperKafka Needs No Keeper
Kafka Needs No Keeper
 
High Performing Teams Act Like Owners
High Performing Teams Act Like OwnersHigh Performing Teams Act Like Owners
High Performing Teams Act Like Owners
 
Does Java Need Inline Types? What Project Valhalla Can Bring to Java
Does Java Need Inline Types? What Project Valhalla Can Bring to JavaDoes Java Need Inline Types? What Project Valhalla Can Bring to Java
Does Java Need Inline Types? What Project Valhalla Can Bring to Java
 
Service Meshes- The Ultimate Guide
Service Meshes- The Ultimate GuideService Meshes- The Ultimate Guide
Service Meshes- The Ultimate Guide
 
Shifting Left with Cloud Native CI/CD
Shifting Left with Cloud Native CI/CDShifting Left with Cloud Native CI/CD
Shifting Left with Cloud Native CI/CD
 
CI/CD for Machine Learning
CI/CD for Machine LearningCI/CD for Machine Learning
CI/CD for Machine Learning
 
Fault Tolerance at Speed
Fault Tolerance at SpeedFault Tolerance at Speed
Fault Tolerance at Speed
 
Architectures That Scale Deep - Regaining Control in Deep Systems
Architectures That Scale Deep - Regaining Control in Deep SystemsArchitectures That Scale Deep - Regaining Control in Deep Systems
Architectures That Scale Deep - Regaining Control in Deep Systems
 
ML in the Browser: Interactive Experiences with Tensorflow.js
ML in the Browser: Interactive Experiences with Tensorflow.jsML in the Browser: Interactive Experiences with Tensorflow.js
ML in the Browser: Interactive Experiences with Tensorflow.js
 
Build Your Own WebAssembly Compiler
Build Your Own WebAssembly CompilerBuild Your Own WebAssembly Compiler
Build Your Own WebAssembly Compiler
 
User & Device Identity for Microservices @ Netflix Scale
User & Device Identity for Microservices @ Netflix ScaleUser & Device Identity for Microservices @ Netflix Scale
User & Device Identity for Microservices @ Netflix Scale
 
Scaling Patterns for Netflix's Edge
Scaling Patterns for Netflix's EdgeScaling Patterns for Netflix's Edge
Scaling Patterns for Netflix's Edge
 
Make Your Electron App Feel at Home Everywhere
Make Your Electron App Feel at Home EverywhereMake Your Electron App Feel at Home Everywhere
Make Your Electron App Feel at Home Everywhere
 
The Talk You've Been Await-ing For
The Talk You've Been Await-ing ForThe Talk You've Been Await-ing For
The Talk You've Been Await-ing For
 
Future of Data Engineering
Future of Data EngineeringFuture of Data Engineering
Future of Data Engineering
 
Automated Testing for Terraform, Docker, Packer, Kubernetes, and More
Automated Testing for Terraform, Docker, Packer, Kubernetes, and MoreAutomated Testing for Terraform, Docker, Packer, Kubernetes, and More
Automated Testing for Terraform, Docker, Packer, Kubernetes, and More
 

Último

Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsRoshan Dwivedi
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
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
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
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
 
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
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 

Último (20)

Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
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...
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
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...
 
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
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 

Deconstructing Functional Programming

  • 2. Watch the video with slide synchronization on InfoQ.com! http://www.infoq.com/presentations /functional-pros-cons InfoQ.com: News & Community Site • 750,000 unique visitors/month • Published in 4 languages (English, Chinese, Japanese and Brazilian Portuguese) • Post content from our QCon conferences • News 15-20 / week • Articles 3-4 / week • Presentations (videos) 12-15 / week • Interviews 2-3 / week • Books 1 / month
  • 3. Presented at QCon San Francisco www.qconsf.com Purpose of QCon - to empower software development by facilitating the spread of knowledge and innovation Strategy - practitioner-driven conference designed for YOU: influencers of change and innovation in your teams - speakers and topics driving the evolution and innovation - connecting and catalyzing the influencers and innovators Highlights - attended by more than 12,000 delegates since 2007 - held in 9 cities worldwide
  • 4. The Heart of Darkness Wednesday, November 13, 13
  • 5. What is Functional Programming? Definitions vary, but tend to involve Higher order functions (HOFs) Absence of effects Wednesday, November 13, 13
  • 6. Higher Order Functions Functions are values Can be passed as arguments and returned as results Function literals are expressions that can be written anywhere an expression can appear Wednesday, November 13, 13
  • 7. Languages with HOFs Smalltalk, Self, Newspeak, Dart, Javascript ... and now even Java (gasp) Wednesday, November 13, 13
  • 8. Languages with Effects Lisp, Scheme, Racket, Clojure, ML, OCaml, F#, Erlang Wednesday, November 13, 13
  • 9. Languages w/o HOFs Backus’ FP, SISAL, the original versions of Erlang ... These are functional languages; in fact FP was the original functional language Wednesday, November 13, 13
  • 10. What is Functional Programming? 1. A style which utilizes HOFs and minimizes effects 2. A cult(ure) 3. All of the above In particular, there is no contradiction with object orientation (see Scala as an example) Wednesday, November 13, 13
  • 11. HOFs/Closures HOFs are awesome. Lambdas even more so. The great classical HOFs: map, filter, reduce They’ve been around since Lisp & Smalltalk (i.e., long before the term FP was introduced) Wednesday, November 13, 13
  • 12. User-defined Control Structures a > b ifTrue:[ a - b]. Wednesday, November 13, 13
  • 13. User-defined Control Structures a > b ifTrue:[ a - b]. A boolean, an object, the target of ... Wednesday, November 13, 13
  • 14. User-defined Control Structures a > b ifTrue:[ a - b]. The method, invoked with ... Wednesday, November 13, 13
  • 15. User-defined Control Structures a > b ifTrue:[ a - b]. The argument, a block/lambda/closure Wednesday, November 13, 13
  • 16. User-defined Control Structures a > b ifTrue:[ a - b]. In class True, ifTrue: evaluates its argument and returns the result. In class False, ifTrue: returns nil. Wednesday, November 13, 13
  • 17. Tail Calls Look ma, no loops: while(b, s) { if b() then {s(); while(b,s)} else null} How to express computation unbounded in time but bounded in space. A language is functional if it supports proper tail calls? Wednesday, November 13, 13
  • 18. Tail Calls What about debugging? Where’s my stack trace? http://gbracha.blogspot.com/2009/12/chased-byones-own-tail.html Wednesday, November 13, 13
  • 19. Hindley-Milner If it typechecks, it works .... ? But error messages are terrible Leaks implementation information Wednesday, November 13, 13
  • 20. Currying All functions take one argument, produce one result Extremely compositional, BUT We lose almost all structure Forced to rely on type system Wednesday, November 13, 13
  • 21. Pattern Matching Nice Allows FPLs to pretend they don’t do dynamic typechecks and casts Usually second class First class patterns are interesting http://gbracha.blogspot.com/2010/05/patterns-of-dynamic-typechecking.html http://gbracha.blogspot.com/2010/06/patterns-as-objects-in-newspeak.html Wednesday, November 13, 13
  • 23. A Monad by any other name would smell ... Wednesday, November 13, 13
  • 24. A Monad by any other name would smell as sweet Wednesday, November 13, 13
  • 25. Ye Highlands and ye Lowlands, Oh, where hae ye been? They hae slain the Earl O' Moray, And laid him on the green. The Bonny Earl O’Moray Wednesday, November 13, 13
  • 26. Ye Highlands and ye Lowlands, Oh, where hae ye been? They hae slain the Earl O' Moray, And Lady Mondegreen. The Bonny Earl O’Moray, revised by Sylvia Wright Wednesday, November 13, 13
  • 27. Ye Highlands and ye Lowlands, Oh, where hae ye been? They hae slain the Earl O' Moray, And Lady Monadgreen. The Bonny Earl O’Moray, revised by me Wednesday, November 13, 13
  • 28. Monad Metaphors, aka Monadgreens Nuclear waste containers Romantic conquests Space Suits Monsters Macros Containers Conversations Black holes Wednesday, November 13, 13
  • 29. Lady Monadgreen’s Curse Once you understand monads, you immediately become incapable of explaining them to anyone else Wednesday, November 13, 13
  • 31. abstract class FlatMappable { FlatMappable(a); flatMap(f); } Wednesday, November 13, 13
  • 32. abstract class Monad { Monad(a); flatMap(f); } Wednesday, November 13, 13
  • 33. abstract class Monad { Monad.unit(a); flatMap(f); } Wednesday, November 13, 13
  • 34. abstract class Monad { Monad.return(a); flatMap(f); } Wednesday, November 13, 13
  • 35. abstract class Monad { Monad.return(a); bind(f); } Wednesday, November 13, 13
  • 36. abstract class Monad { Monad.return(a); operator * (f); } Wednesday, November 13, 13
  • 37. abstract class Monad { Monad.return(a); operator >>= (f); } Wednesday, November 13, 13
  • 38. abstract class FlatMappable { FlatMappable(a); flatMap(f); // map, then flatten } Wednesday, November 13, 13
  • 39. abstract class Mappable { Mappable(a); map(f); } abstract class MappableAndFlattenable extends Mappable { MappableAndFlattenable(a): super(a); flatten(); } Wednesday, November 13, 13
  • 40. abstract class FlatMappable extends MappableAndFlattenable { FlatMappable(a): super(a); flatMap(f) => map(f).flatten(); } Wednesday, November 13, 13
  • 41. What’s the Contract Like? Sample Clause: new Mappable(x).map(f) == new Mappable( f(x)); Wednesday, November 13, 13
  • 42. What’s the Contract Like? Sample Clause: new FlatMappable(x).flatMap(f) == f(x); Wednesday, November 13, 13
  • 43. What’s the Contract Like? Sample Clause: new C(x).flatMap(f) == f(x); for any class C that implements the contract Wednesday, November 13, 13
  • 44. The Whole Contract new C(x).flatMap(f) == f(x); c.flatMap((x)=> new C(x)) == c; c.flatMap((x)=> f(x).flatMap(g)) == c.flatMap(f).flatMap(g); Wednesday, November 13, 13
  • 45. FlatMappable is more useful than you think Collections of all kinds: in-memory, databases, streams “Singleton collections”: functions, continuations, futures Wednesday, November 13, 13
  • 46. A simple expression e.b.c what could possibly go wrong? Wednesday, November 13, 13
  • 47. A simple expression e.b.c what could possibly go wrong? e could be null. Ok e == null ? null : e.b.c Wednesday, November 13, 13
  • 48. A simple expression e.b.c what could possibly go wrong? e could be null. Ok e == null ? null : e.b.c oops, e.b could be null e == null ? null : e.b == null ? null : e.b.c Wednesday, November 13, 13
  • 49. A simple expression e == null ? null : e.b == null ? null : e.b.c what about side effects? (var it = e) == null ? null : (var ab = it.b) == null ? null : ab.c Wednesday, November 13, 13
  • 50. Safe Navigation Operator e?.b?.c e?.id(a1, .. an) is sugar for e.ifNotNull((x) => x.id(a1, .. an)) where we define class Object { ifNotNull(f) => this == null ? null : f(this); } Wednesday, November 13, 13
  • 51. Promise Pipelining e <- id(a1, .. an) is sugar for e.then((x) => x.id(a1, .. an)) where we define class Object { then(f) => f(this); } Wednesday, November 13, 13
  • 52. Safe Promise Pipelining e <- id(a1, .. an) is sugar for e.then((x) => x.id(a1, .. an)) where we define class Object { then(f) => this == null ? null : f(this); } Wednesday, November 13, 13
  • 53. Async message pipelining e <- id(a1, .. an) is sugar for e.then((x) => x.id(a1, .. an)) where we define class Object { then(f) => Future.immediate(this).then(f); } Wednesday, November 13, 13
  • 54. Scalar operators extended pointwise to collections e.* id(a1, .. an) is sugar for e.map((x) => x.id(a1, .. an)) where we define class Object { map(f) => f(this); } [‘abc’, ‘de’, ‘wxyz’].* length evaluates to [3, 2, 4] Wednesday, November 13, 13
  • 55. Scalar operators extended pointwise to collections e.* id(a1, .. an) is sugar for e.map((x) => x.id(a1, .. an)) where we define class Object { map(f) => this == null ? null : f(this); } [‘abc’, null, ‘wxyz’].* length evaluates to [3, null, 4] Wednesday, November 13, 13
  • 57. Is There a Pattern Here? The Curse of the Monadgreens prevents me from discussing this further In practice, we see generalizations of set notation rather than navigation LINQ is an example. However, you didn’t need to have even heard of monads to invent LINQ. Just look at Smalltalk’s collection API, Glorp etc. http://gbracha.blogspot.com/2011/01/maybemonads-might-not-matter.html Wednesday, November 13, 13
  • 58. The Killer App for FP? Wednesday, November 13, 13
  • 60. The Killer App for FP? Live Programming http://gbracha.blogspot.com/2012/11/debug-mode-is-only-mode.html http://gbracha.blogspot.com/2013/04/making-methods-live.html https://www.youtube.com/watch?v=74WqdS_58uY Wednesday, November 13, 13
  • 61. Summary Much to learn from FP FP and OOP are often complementary Filter out propaganda Separate cultural baggage from core values Wednesday, November 13, 13
  • 62. The Newspeak eye on slide 5 was designed by Victoria Bracha and is used by permission. The cartoon on slide 2 comes from http://xkcd.com/ 1270/ and is licensed by xkcd.com under http:// creativecommons.org/licenses/by-nc/2.5/ The rose on slide 16 is by Kikuo Teranishi and licensed under http://creativecommons.org/licenses/by-sa/3.0/ deed.en Wednesday, November 13, 13
  • 63. Watch the video with slide synchronization on InfoQ.com! http://www.infoq.com/presentations/functional -pros-cons