SlideShare a Scribd company logo
1 of 100
Functional OOP,
Clojure Style
Yoav Rubin
About me
• Software engineer at IBM Research - Haifa
– Development of development environments
– Large scale products
to small scale research
projects
• Lecture the course “Functional
programming on the JVM” in Haifa
University
{:name Yoav Rubin,
:email yoavrubin@gmail.com,
:blog http://yoavrubin.blogspot.com,
:twitter @yoavrubin}
First thing first
Alan Kay
Alan Kay Edsger W. Dijkstra
Alan Kay Edsger W. Dijkstra
“Perspective is worth 80 IQ points”
Alan Kay
• OOP
• Clojure
Agenda
in
What’s in a software
• Data types that describe the elements of
the domain (nouns)
• State changing operations (verbs)
Type  functionality f1 f2 f3 f4
T1 X X
T2 X
T3 X
T4 X X X X
The software matrix
Type  functionality f1 f2 f3 f4
T1 X X
T2 X
T3 X
T4 X X X X
The software matrix
Data types, nouns
Type  functionality f1 f2 f3 f4
T1 X X
T2 X
T3 X
T4 X X X X
The software matrix
API, verbs,
interfaces
Type  functionality f1 f2 f3 f4
T1 X X
T2 X
T3 X
T4 X X X X
The software matrix
implementations of
operationx by Typey
Data directed programming
The expression problem
Philip Wadler
SICP
Deciding which function to use based
on given data
How to add rows and columns to the
matrix without recompiling while
preserving static typing
Let’s talk OOP
• The rows are the classes
– The domain abstractions we define and use
– Which can hide within them their internal state
• The column headers are interfaces
– Which allow polymorphic usage
• Marked cell in row X and column Y signifies that
class X implements interface Y
– Saying that the implementation itself resides in class
Z means that X is-a Z
Let’s talk OOP
• The rows are the classes
– The domain abstractions we define and use
– Which can hide within them their internal state
• The column headers are interfaces
– Which allow polymorphic usage
• Marked cell in row X and column Y signifies that
class X implements interface Y
– Saying that the implementation itself resides in class
Z means that X is-a Z
Abstraction
Let’s talk OOP
• The rows are the classes
– The domain abstractions we define and use
– Which can hide within them their internal state
• The column headers are interfaces
– Which allow polymorphic usage
• Marked cell in row X and column Y signifies that
class X implements interface Y
– Saying that the implementation itself resides in class
Z means that X is-a Z
Let’s talk OOP
• The rows are the classes
– The domain abstractions we define and use
– Which can hide within them their internal state
• The column headers are interfaces
– Which allow polymorphic usage
• Marked cell in row X and column Y signifies that
class X implements interface Y
– Saying that the implementation itself resides in class
Z means that X is-a Z
Information hiding
Let’s talk OOP
• The rows are the classes
– The domain abstractions we define and use
– Which can hide within them their internal state
• The column headers are interfaces
– Which allow polymorphic usage
• Marked cell in row X and column Y signifies that
class X implements interface Y
– Saying that the implementation itself resides in class
Z means that X is-a Z
Let’s talk OOP
• The rows are the classes
– The domain abstractions we define and use
– Which can hide within them their internal state
• The column headers are interfaces
– Which allow polymorphic usage
• Marked cell in row X and column Y signifies that
class X implements interface Y
– Saying that the implementation itself resides in class
Z means that X is-a Z
Polymorphism
Let’s talk OOP
• The rows are the classes
– The domain abstractions we define and use
– Which can hide within them their internal state
• The column headers are interfaces
– Which allow polymorphic usage
• Marked cell in row X and column Y signifies that
class X implements interface Y
– Saying that the implementation itself resides in class
Z means that X is-a Z
Let’s talk OOP
• The rows are the classes
– The domain abstractions we define and use
– Which can hide within them their internal state
• The column headers are interfaces
– Which allow polymorphic usage
• Marked cell in row X and column Y signifies that
class X implements interface Y
– Saying that the implementation itself resides in class
Z means that X is-a Z
Inheritance
Type 
functionality
f1 f2 f3 f4
T1 X X
T2 X
T3 X
T4 X X X
Inheritance
Type 
functionality
f1 f2 f3 f4
T1 X X
T2 X
T3 X
T4 X X X
Inheritance
T4 can say that it is a
T3
Type 
functionality
f1 f2 f3 f4
T1 X X
T2 X
T3 X
T4 X X X
Inheritance
T4 can say that it is a
T3
and its implementation
of f3 is found at T3
X
OOP on the matrix
• The rows are the classes
– The domain abstractions we define and use
– Which can hide within them their internal state
• The column headers are interfaces
– Which allow polymorphic usage
• Marked cell in row X and column Y signifies that
class X implements interface Y
– Saying that the implementation itself resides in Class
Z means that X is-a Z
How is it all related to Clojure?
What is Clojure
What is Clojure
• A Lisp
• A functional language
• Dynamically typed
• Emphasis on immutability
• Treats concurrency as an elementary part of life
– Not as a burden
• Compiles to bytecode
– Of the JVM / CLR / JS (as the web’s bytecode)
• Excellent “great ideas to WTF” ratio
General structure
• A Clojure project is built of namespaces
• In each namespace there are functions
and data elements
• Functions can be either public or private
– Either visible or not visible outside of the
namespace
General structure
• A Clojure project is built of namespaces
• In each namespace there are functions
and data elements
• Functions can be either public or private
– Either visible or not visible outside of the
namespace
Functional Information hiding
How to define rows in Clojure?
Creating new types
• Metaobjects – a mechanism that allows
description and creation of new datatypes
• We can create our own metaobjects
– E.g., a map that one of its key is “type”
• In Clojure there are two metaobjects
– Type
– Record
The Type metaobject
• Upon definition we need to provide:
– Name
– Member fields
– APIs to implement and their implementation
• Override methods from Object, interfaces,
protocols (soon)
• Cannot introduce new APIs to the matrix
• Can be made mutable
The type metaobject
Definition:
Instantiation:
Usage:
The type metaobject
Definition:
Instantiation:
Usage:
The type metaobject
Definition:
Instantiation:
Usage:
The type metaobject
Definition:
Instantiation:
Usage:
Two main use cases
• You really know what you are doing
• You’re doing it wrong
The Record metaobject
• Similar to the Type metaobject
• Provides a map like behavior
• No mutability
So far in the software matrix
• Added new rows
– New types / records
– No new APIs
• Associate with an existing column
So far in the software matrix
• Added new rows
– New types / records
– No new APIs
• Associate with an existing column
Functional Abstraction
So far in the software matrix
• Added new rows
– New types / records
– No new APIs
• Associate with an existing column
Functional Abstraction
Polymorphism
How to define columns in Clojure?
Adding new APIs
• New APIs for one type
– just add a new function
• Problem: how to handle more types?
• Naïve solution: a simple dispatcher
Now there’s a new tree in town
What can a developer do?
• Re-write the existing tree-map function
– Because editing legacy code is fun…
• Create another tree-map in another namespace
and qualify its calls
– Name collisions
• Create tree-map2
– Complicating both developer’s and user’s code
A deeper look
Type  functionality f1 f2 f3 tree-map
GeneralTree X X X
YetAnotherType X
BinaryTree X X
A deeper look
Type  functionality f1 f2 f3 tree-map
GeneralTree X X X
YetAnotherType X
BinaryTree X X
New column header (API)
A deeper look
Type  functionality f1 f2 f3 tree-map
GeneralTree X X X
YetAnotherType X
BinaryTree X X
New column header (API)
Two
implementations
A deeper look
Type  functionality f1 f2 f3 tree-map
GeneralTree X X X
YetAnotherType X
BinaryTree X X
New column header (API)
Two
implementations
QuadTree
A deeper look
Type  functionality f1 f2 f3 tree-map
GeneralTree X X X
YetAnotherType X
BinaryTree X X
New column header (API)
Two
implementations
QuadTree ?
A deeper look
Type  functionality f1 f2 f3 tree-map
GeneralTree X X X
YetAnotherType X
BinaryTree X X
New column header (API)
Two
implementations
QuadTree ?
Tree-map did too much!!!
In the software matrix:
Need to decomplect the creation of
columns headers from cell marking
Or in software design language:
We need to separate the definition of an
API from its implementation
Creating abstract APIs
• No concrete implementation
• Define a semantic unit
– A set of behaviors that compose an API
• In another place define the mapping
between data types and the API
– Marking of a cell in the matrix
Protocol
• A set of several function signatures
– Just the signature, without implementation
– Dispatch is done based on the run-time type
Protocol
• A set of several function signatures
– Just the signature, without implementation
– Dispatch is done based on the run-time type
The protocol name
Protocol
• A set of several function signatures
– Just the signature, without implementation
– Dispatch is done based on the run-time type
The protocol name
A function signature
(there can be several of these)
Protocols and types
• The linking of a protocol to a type can be
done not as part of the definition of the
type
• This results in the possibility to extend
existing, compiled types
– Extend String
– Extend even nil
Added to an existing type a
new API
Without changing the type
Added to an existing type a
new API
Without changing the type
Functional polymorphism
Still, there are limitations
Protocols allow type based dispatch only
Multi methods
• Polymorphism which is based on a user
defined dispatching function
• The result of the execution of the dispatch
function determines which implementation
will be executes
(dispatch-fn)
take-care-of
::moon
::sun
(tco-sun)
(tco-moon)
dispatcher
(dispatch-fn)
take-care-of
::moon
::sun
(tco-sun)
(tco-moon)
This is the exposed API
dispatcher
(dispatch-fn)
take-care-of
::moon
::sun
(tco-sun)
(tco-moon)
dispatcher
(dispatch-fn)
take-care-of
::moon
::sun
(tco-sun)
(tco-moon)
dispatcher
(dispatch-fn)
take-care-of
::moon
::sun
(tco-sun)
::lightning
(tco-lightning)
(tco-moon)
dispatcher
The multi method name
The dispatching function
Meanwhile, at other namespaces
Meanwhile, at other namespaces
Meanwhile, at other namespaces
Multi method
• We can use the same API for different
data elements
• All we need to know is that they obey that
API
• We can introduce new APIs for existing
types
Multi method
• We can use the same API for different
data elements
• All we need to know is that they obey that
API
• We can introduce new APIs for existing
types
Functional polymorphism
Is-a relationship
• We can define that A is-a B
• The dispatcher would handle A the same
way that it handles B
• (derive ::A ::B)
– if the dispatch function return ::A
– if no value is found for ::A in the dispatcher
– Handle it as ::B
Is-a relationship
Is-a relationship
Is-a relationship
Is-a relationship
Is-a relationship
Is-a relationship
Why did it work
• Derive harms the referential transparency of the multi
method
– The return value may differ if (derive…) was called
– Referential transparency is our friend
• Derive works only with namespace bound keywords
– Those that start with ::
• Clojure localizes the effect of mutability to the
namespace
Type 
functionality
f1 f2 f3 f4
T1 X X
T2 X
T3 X
T4 X X X X
T4 isa T2 (for f2)
T4 isa T3 (for f3)
Type 
functionality
f1 f2 f3 f4
T1 X X
T2 X
T3 X
T4 X X X X
T4 isa T2 (for f2)
T4 isa T3 (for f3)
Functional inheritance
Summary
• We’ve seen
– Functional abstraction
– Functional information hiding
– Functional polymorphism
– Functional inheritance
Summary
• We’ve seen functional
– Abstraction
– Information hiding
– Polymorphism
– Inheritance
Summary
• We’ve seen functional OOP
Summary
• We’ve seen functional OOP
Clojure Style
Thank
You!

More Related Content

What's hot

Functional programming for the Advanced Beginner
Functional programming for the Advanced BeginnerFunctional programming for the Advanced Beginner
Functional programming for the Advanced BeginnerLuis Atencio
 
Object Oriented Programming : Part 2
Object Oriented Programming : Part 2Object Oriented Programming : Part 2
Object Oriented Programming : Part 2Madhavan Malolan
 
New c sharp3_features_(linq)_part_iv
New c sharp3_features_(linq)_part_ivNew c sharp3_features_(linq)_part_iv
New c sharp3_features_(linq)_part_ivNico Ludwig
 
A Field Guide to DSL Design in Scala
A Field Guide to DSL Design in ScalaA Field Guide to DSL Design in Scala
A Field Guide to DSL Design in ScalaTomer Gabel
 
Exploring C# DSLs: LINQ, Fluent Interfaces and Expression Trees
Exploring C# DSLs: LINQ, Fluent Interfaces and Expression TreesExploring C# DSLs: LINQ, Fluent Interfaces and Expression Trees
Exploring C# DSLs: LINQ, Fluent Interfaces and Expression Treesrasmuskl
 
Smalltalk and ruby - 2012-12-08
Smalltalk and ruby  - 2012-12-08Smalltalk and ruby  - 2012-12-08
Smalltalk and ruby - 2012-12-08Koan-Sin Tan
 
Introducing object oriented programming (oop)
Introducing object oriented programming (oop)Introducing object oriented programming (oop)
Introducing object oriented programming (oop)Hemlathadhevi Annadhurai
 
Intro to Object Oriented Programming with Java
Intro to Object Oriented Programming with Java Intro to Object Oriented Programming with Java
Intro to Object Oriented Programming with Java Hitesh-Java
 

What's hot (11)

Scala’s implicits
Scala’s implicitsScala’s implicits
Scala’s implicits
 
Functional programming for the Advanced Beginner
Functional programming for the Advanced BeginnerFunctional programming for the Advanced Beginner
Functional programming for the Advanced Beginner
 
Object Oriented Programming : Part 2
Object Oriented Programming : Part 2Object Oriented Programming : Part 2
Object Oriented Programming : Part 2
 
New c sharp3_features_(linq)_part_iv
New c sharp3_features_(linq)_part_ivNew c sharp3_features_(linq)_part_iv
New c sharp3_features_(linq)_part_iv
 
Scala
ScalaScala
Scala
 
A Field Guide to DSL Design in Scala
A Field Guide to DSL Design in ScalaA Field Guide to DSL Design in Scala
A Field Guide to DSL Design in Scala
 
Exploring C# DSLs: LINQ, Fluent Interfaces and Expression Trees
Exploring C# DSLs: LINQ, Fluent Interfaces and Expression TreesExploring C# DSLs: LINQ, Fluent Interfaces and Expression Trees
Exploring C# DSLs: LINQ, Fluent Interfaces and Expression Trees
 
Smalltalk and ruby - 2012-12-08
Smalltalk and ruby  - 2012-12-08Smalltalk and ruby  - 2012-12-08
Smalltalk and ruby - 2012-12-08
 
Metaprogramming ruby
Metaprogramming rubyMetaprogramming ruby
Metaprogramming ruby
 
Introducing object oriented programming (oop)
Introducing object oriented programming (oop)Introducing object oriented programming (oop)
Introducing object oriented programming (oop)
 
Intro to Object Oriented Programming with Java
Intro to Object Oriented Programming with Java Intro to Object Oriented Programming with Java
Intro to Object Oriented Programming with Java
 

Similar to Functional OOP, Clojure style

Introduction to oop and java fundamentals
Introduction to oop and java fundamentalsIntroduction to oop and java fundamentals
Introduction to oop and java fundamentalsAnsgarMary
 
Complete PPT about the Java lokesh kept it
Complete PPT about the Java lokesh kept itComplete PPT about the Java lokesh kept it
Complete PPT about the Java lokesh kept itlokeshpappaka10
 
Functional Programming In Jdk8
Functional Programming In Jdk8 Functional Programming In Jdk8
Functional Programming In Jdk8 Bansilal Haudakari
 
UNIT-IV WT web technology for 1st year cs
UNIT-IV WT web technology for 1st year csUNIT-IV WT web technology for 1st year cs
UNIT-IV WT web technology for 1st year csjaved75
 
Iterator - a powerful but underappreciated design pattern
Iterator - a powerful but underappreciated design patternIterator - a powerful but underappreciated design pattern
Iterator - a powerful but underappreciated design patternNitin Bhide
 
Note for Java Programming////////////////
Note for Java Programming////////////////Note for Java Programming////////////////
Note for Java Programming////////////////MeghaKulkarni27
 
Software Engineering Thailand: Programming with Scala
Software Engineering Thailand: Programming with ScalaSoftware Engineering Thailand: Programming with Scala
Software Engineering Thailand: Programming with ScalaBrian Topping
 
full defination of final opp.pptx
full defination of final opp.pptxfull defination of final opp.pptx
full defination of final opp.pptxrayanbabur
 
Introduction to Java Part-3
Introduction to Java Part-3Introduction to Java Part-3
Introduction to Java Part-3RatnaJava
 
Unit 1 Core Java for Compter Science 3rd
Unit 1 Core Java for Compter Science 3rdUnit 1 Core Java for Compter Science 3rd
Unit 1 Core Java for Compter Science 3rdprat0ham
 
Introduction to object oriented programming
Introduction to object oriented programmingIntroduction to object oriented programming
Introduction to object oriented programmingAbzetdin Adamov
 
Exploring Elixir Codebases with Archeometer
Exploring Elixir Codebases with ArcheometerExploring Elixir Codebases with Archeometer
Exploring Elixir Codebases with ArcheometerAgustin Ramos
 
C++ in object oriented programming
C++ in object oriented programmingC++ in object oriented programming
C++ in object oriented programmingSaket Khopkar
 
Scala, Play 2.0 & Cloud Foundry
Scala, Play 2.0 & Cloud FoundryScala, Play 2.0 & Cloud Foundry
Scala, Play 2.0 & Cloud FoundryPray Desai
 

Similar to Functional OOP, Clojure style (20)

10-DesignPatterns.ppt
10-DesignPatterns.ppt10-DesignPatterns.ppt
10-DesignPatterns.ppt
 
Introduction to oop and java fundamentals
Introduction to oop and java fundamentalsIntroduction to oop and java fundamentals
Introduction to oop and java fundamentals
 
Complete PPT about the Java lokesh kept it
Complete PPT about the Java lokesh kept itComplete PPT about the Java lokesh kept it
Complete PPT about the Java lokesh kept it
 
Functional Programming In Jdk8
Functional Programming In Jdk8 Functional Programming In Jdk8
Functional Programming In Jdk8
 
Oopsinphp
OopsinphpOopsinphp
Oopsinphp
 
UNIT-IV WT web technology for 1st year cs
UNIT-IV WT web technology for 1st year csUNIT-IV WT web technology for 1st year cs
UNIT-IV WT web technology for 1st year cs
 
Iterator - a powerful but underappreciated design pattern
Iterator - a powerful but underappreciated design patternIterator - a powerful but underappreciated design pattern
Iterator - a powerful but underappreciated design pattern
 
Note for Java Programming////////////////
Note for Java Programming////////////////Note for Java Programming////////////////
Note for Java Programming////////////////
 
TypeScript Overview
TypeScript OverviewTypeScript Overview
TypeScript Overview
 
Software Engineering Thailand: Programming with Scala
Software Engineering Thailand: Programming with ScalaSoftware Engineering Thailand: Programming with Scala
Software Engineering Thailand: Programming with Scala
 
full defination of final opp.pptx
full defination of final opp.pptxfull defination of final opp.pptx
full defination of final opp.pptx
 
Introduction to Java Part-3
Introduction to Java Part-3Introduction to Java Part-3
Introduction to Java Part-3
 
Unit 1 Core Java for Compter Science 3rd
Unit 1 Core Java for Compter Science 3rdUnit 1 Core Java for Compter Science 3rd
Unit 1 Core Java for Compter Science 3rd
 
Java
JavaJava
Java
 
Scala Days NYC 2016
Scala Days NYC 2016Scala Days NYC 2016
Scala Days NYC 2016
 
Introduction to object oriented programming
Introduction to object oriented programmingIntroduction to object oriented programming
Introduction to object oriented programming
 
Exploring Elixir Codebases with Archeometer
Exploring Elixir Codebases with ArcheometerExploring Elixir Codebases with Archeometer
Exploring Elixir Codebases with Archeometer
 
General oop concept
General oop conceptGeneral oop concept
General oop concept
 
C++ in object oriented programming
C++ in object oriented programmingC++ in object oriented programming
C++ in object oriented programming
 
Scala, Play 2.0 & Cloud Foundry
Scala, Play 2.0 & Cloud FoundryScala, Play 2.0 & Cloud Foundry
Scala, Play 2.0 & Cloud Foundry
 

More from yoavrubin

CR17 - Designing a database like an archaeologist
CR17 - Designing a database like an archaeologistCR17 - Designing a database like an archaeologist
CR17 - Designing a database like an archaeologistyoavrubin
 
Designing a database like an archaeologist
Designing a database like an archaeologistDesigning a database like an archaeologist
Designing a database like an archaeologistyoavrubin
 
Functional Programming in Javascript - IL Tech Talks week
Functional Programming in Javascript - IL Tech Talks weekFunctional Programming in Javascript - IL Tech Talks week
Functional Programming in Javascript - IL Tech Talks weekyoavrubin
 
Clojure's take on concurrency
Clojure's take on concurrencyClojure's take on concurrency
Clojure's take on concurrencyyoavrubin
 
JavaScript - Programming Languages course
JavaScript - Programming Languages course JavaScript - Programming Languages course
JavaScript - Programming Languages course yoavrubin
 
Introduction To Dojo
Introduction To DojoIntroduction To Dojo
Introduction To Dojoyoavrubin
 
Dojo - from web page to web apps
Dojo - from web page to web appsDojo - from web page to web apps
Dojo - from web page to web appsyoavrubin
 

More from yoavrubin (7)

CR17 - Designing a database like an archaeologist
CR17 - Designing a database like an archaeologistCR17 - Designing a database like an archaeologist
CR17 - Designing a database like an archaeologist
 
Designing a database like an archaeologist
Designing a database like an archaeologistDesigning a database like an archaeologist
Designing a database like an archaeologist
 
Functional Programming in Javascript - IL Tech Talks week
Functional Programming in Javascript - IL Tech Talks weekFunctional Programming in Javascript - IL Tech Talks week
Functional Programming in Javascript - IL Tech Talks week
 
Clojure's take on concurrency
Clojure's take on concurrencyClojure's take on concurrency
Clojure's take on concurrency
 
JavaScript - Programming Languages course
JavaScript - Programming Languages course JavaScript - Programming Languages course
JavaScript - Programming Languages course
 
Introduction To Dojo
Introduction To DojoIntroduction To Dojo
Introduction To Dojo
 
Dojo - from web page to web apps
Dojo - from web page to web appsDojo - from web page to web apps
Dojo - from web page to web apps
 

Recently uploaded

ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024The Digital Insurer
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
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
 
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKSpring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKJago de Vreede
 
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
 
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
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businesspanagenda
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024The Digital Insurer
 
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
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...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
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native ApplicationsWSO2
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
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
 
Cyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdfCyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdfOverkill Security
 
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
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWERMadyBayot
 
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
 

Recently uploaded (20)

ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
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
 
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKSpring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
 
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
 
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
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
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
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
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
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
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
 
Cyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdfCyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdf
 
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...
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
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, ...
 

Functional OOP, Clojure style

  • 1.
  • 3. About me • Software engineer at IBM Research - Haifa – Development of development environments – Large scale products to small scale research projects • Lecture the course “Functional programming on the JVM” in Haifa University {:name Yoav Rubin, :email yoavrubin@gmail.com, :blog http://yoavrubin.blogspot.com, :twitter @yoavrubin}
  • 6. Alan Kay Edsger W. Dijkstra
  • 7. Alan Kay Edsger W. Dijkstra
  • 8. “Perspective is worth 80 IQ points” Alan Kay
  • 10. What’s in a software • Data types that describe the elements of the domain (nouns) • State changing operations (verbs)
  • 11. Type functionality f1 f2 f3 f4 T1 X X T2 X T3 X T4 X X X X The software matrix
  • 12. Type functionality f1 f2 f3 f4 T1 X X T2 X T3 X T4 X X X X The software matrix Data types, nouns
  • 13. Type functionality f1 f2 f3 f4 T1 X X T2 X T3 X T4 X X X X The software matrix API, verbs, interfaces
  • 14. Type functionality f1 f2 f3 f4 T1 X X T2 X T3 X T4 X X X X The software matrix implementations of operationx by Typey
  • 15. Data directed programming The expression problem Philip Wadler SICP Deciding which function to use based on given data How to add rows and columns to the matrix without recompiling while preserving static typing
  • 16. Let’s talk OOP • The rows are the classes – The domain abstractions we define and use – Which can hide within them their internal state • The column headers are interfaces – Which allow polymorphic usage • Marked cell in row X and column Y signifies that class X implements interface Y – Saying that the implementation itself resides in class Z means that X is-a Z
  • 17. Let’s talk OOP • The rows are the classes – The domain abstractions we define and use – Which can hide within them their internal state • The column headers are interfaces – Which allow polymorphic usage • Marked cell in row X and column Y signifies that class X implements interface Y – Saying that the implementation itself resides in class Z means that X is-a Z Abstraction
  • 18. Let’s talk OOP • The rows are the classes – The domain abstractions we define and use – Which can hide within them their internal state • The column headers are interfaces – Which allow polymorphic usage • Marked cell in row X and column Y signifies that class X implements interface Y – Saying that the implementation itself resides in class Z means that X is-a Z
  • 19. Let’s talk OOP • The rows are the classes – The domain abstractions we define and use – Which can hide within them their internal state • The column headers are interfaces – Which allow polymorphic usage • Marked cell in row X and column Y signifies that class X implements interface Y – Saying that the implementation itself resides in class Z means that X is-a Z Information hiding
  • 20. Let’s talk OOP • The rows are the classes – The domain abstractions we define and use – Which can hide within them their internal state • The column headers are interfaces – Which allow polymorphic usage • Marked cell in row X and column Y signifies that class X implements interface Y – Saying that the implementation itself resides in class Z means that X is-a Z
  • 21. Let’s talk OOP • The rows are the classes – The domain abstractions we define and use – Which can hide within them their internal state • The column headers are interfaces – Which allow polymorphic usage • Marked cell in row X and column Y signifies that class X implements interface Y – Saying that the implementation itself resides in class Z means that X is-a Z Polymorphism
  • 22. Let’s talk OOP • The rows are the classes – The domain abstractions we define and use – Which can hide within them their internal state • The column headers are interfaces – Which allow polymorphic usage • Marked cell in row X and column Y signifies that class X implements interface Y – Saying that the implementation itself resides in class Z means that X is-a Z
  • 23. Let’s talk OOP • The rows are the classes – The domain abstractions we define and use – Which can hide within them their internal state • The column headers are interfaces – Which allow polymorphic usage • Marked cell in row X and column Y signifies that class X implements interface Y – Saying that the implementation itself resides in class Z means that X is-a Z Inheritance
  • 24. Type functionality f1 f2 f3 f4 T1 X X T2 X T3 X T4 X X X Inheritance
  • 25. Type functionality f1 f2 f3 f4 T1 X X T2 X T3 X T4 X X X Inheritance T4 can say that it is a T3
  • 26. Type functionality f1 f2 f3 f4 T1 X X T2 X T3 X T4 X X X Inheritance T4 can say that it is a T3 and its implementation of f3 is found at T3 X
  • 27. OOP on the matrix • The rows are the classes – The domain abstractions we define and use – Which can hide within them their internal state • The column headers are interfaces – Which allow polymorphic usage • Marked cell in row X and column Y signifies that class X implements interface Y – Saying that the implementation itself resides in Class Z means that X is-a Z
  • 28. How is it all related to Clojure?
  • 30. What is Clojure • A Lisp • A functional language • Dynamically typed • Emphasis on immutability • Treats concurrency as an elementary part of life – Not as a burden • Compiles to bytecode – Of the JVM / CLR / JS (as the web’s bytecode) • Excellent “great ideas to WTF” ratio
  • 31. General structure • A Clojure project is built of namespaces • In each namespace there are functions and data elements • Functions can be either public or private – Either visible or not visible outside of the namespace
  • 32. General structure • A Clojure project is built of namespaces • In each namespace there are functions and data elements • Functions can be either public or private – Either visible or not visible outside of the namespace Functional Information hiding
  • 33. How to define rows in Clojure?
  • 34. Creating new types • Metaobjects – a mechanism that allows description and creation of new datatypes • We can create our own metaobjects – E.g., a map that one of its key is “type” • In Clojure there are two metaobjects – Type – Record
  • 35. The Type metaobject • Upon definition we need to provide: – Name – Member fields – APIs to implement and their implementation • Override methods from Object, interfaces, protocols (soon) • Cannot introduce new APIs to the matrix • Can be made mutable
  • 40. Two main use cases • You really know what you are doing • You’re doing it wrong
  • 41. The Record metaobject • Similar to the Type metaobject • Provides a map like behavior • No mutability
  • 42. So far in the software matrix • Added new rows – New types / records – No new APIs • Associate with an existing column
  • 43. So far in the software matrix • Added new rows – New types / records – No new APIs • Associate with an existing column Functional Abstraction
  • 44. So far in the software matrix • Added new rows – New types / records – No new APIs • Associate with an existing column Functional Abstraction Polymorphism
  • 45. How to define columns in Clojure?
  • 46. Adding new APIs • New APIs for one type – just add a new function • Problem: how to handle more types? • Naïve solution: a simple dispatcher
  • 47.
  • 48.
  • 49. Now there’s a new tree in town
  • 50. What can a developer do? • Re-write the existing tree-map function – Because editing legacy code is fun… • Create another tree-map in another namespace and qualify its calls – Name collisions • Create tree-map2 – Complicating both developer’s and user’s code
  • 51. A deeper look Type functionality f1 f2 f3 tree-map GeneralTree X X X YetAnotherType X BinaryTree X X
  • 52. A deeper look Type functionality f1 f2 f3 tree-map GeneralTree X X X YetAnotherType X BinaryTree X X New column header (API)
  • 53. A deeper look Type functionality f1 f2 f3 tree-map GeneralTree X X X YetAnotherType X BinaryTree X X New column header (API) Two implementations
  • 54. A deeper look Type functionality f1 f2 f3 tree-map GeneralTree X X X YetAnotherType X BinaryTree X X New column header (API) Two implementations QuadTree
  • 55. A deeper look Type functionality f1 f2 f3 tree-map GeneralTree X X X YetAnotherType X BinaryTree X X New column header (API) Two implementations QuadTree ?
  • 56. A deeper look Type functionality f1 f2 f3 tree-map GeneralTree X X X YetAnotherType X BinaryTree X X New column header (API) Two implementations QuadTree ? Tree-map did too much!!!
  • 57. In the software matrix: Need to decomplect the creation of columns headers from cell marking Or in software design language: We need to separate the definition of an API from its implementation
  • 58. Creating abstract APIs • No concrete implementation • Define a semantic unit – A set of behaviors that compose an API • In another place define the mapping between data types and the API – Marking of a cell in the matrix
  • 59. Protocol • A set of several function signatures – Just the signature, without implementation – Dispatch is done based on the run-time type
  • 60. Protocol • A set of several function signatures – Just the signature, without implementation – Dispatch is done based on the run-time type The protocol name
  • 61. Protocol • A set of several function signatures – Just the signature, without implementation – Dispatch is done based on the run-time type The protocol name A function signature (there can be several of these)
  • 62. Protocols and types • The linking of a protocol to a type can be done not as part of the definition of the type • This results in the possibility to extend existing, compiled types – Extend String – Extend even nil
  • 63.
  • 64.
  • 65.
  • 66.
  • 67. Added to an existing type a new API Without changing the type
  • 68. Added to an existing type a new API Without changing the type Functional polymorphism
  • 69. Still, there are limitations Protocols allow type based dispatch only
  • 70. Multi methods • Polymorphism which is based on a user defined dispatching function • The result of the execution of the dispatch function determines which implementation will be executes
  • 76.
  • 79. Meanwhile, at other namespaces
  • 80. Meanwhile, at other namespaces
  • 81.
  • 82.
  • 83. Meanwhile, at other namespaces
  • 84. Multi method • We can use the same API for different data elements • All we need to know is that they obey that API • We can introduce new APIs for existing types
  • 85. Multi method • We can use the same API for different data elements • All we need to know is that they obey that API • We can introduce new APIs for existing types Functional polymorphism
  • 86. Is-a relationship • We can define that A is-a B • The dispatcher would handle A the same way that it handles B • (derive ::A ::B) – if the dispatch function return ::A – if no value is found for ::A in the dispatcher – Handle it as ::B
  • 93. Why did it work • Derive harms the referential transparency of the multi method – The return value may differ if (derive…) was called – Referential transparency is our friend • Derive works only with namespace bound keywords – Those that start with :: • Clojure localizes the effect of mutability to the namespace
  • 94. Type functionality f1 f2 f3 f4 T1 X X T2 X T3 X T4 X X X X T4 isa T2 (for f2) T4 isa T3 (for f3)
  • 95. Type functionality f1 f2 f3 f4 T1 X X T2 X T3 X T4 X X X X T4 isa T2 (for f2) T4 isa T3 (for f3) Functional inheritance
  • 96. Summary • We’ve seen – Functional abstraction – Functional information hiding – Functional polymorphism – Functional inheritance
  • 97. Summary • We’ve seen functional – Abstraction – Information hiding – Polymorphism – Inheritance
  • 98. Summary • We’ve seen functional OOP
  • 99. Summary • We’ve seen functional OOP Clojure Style