Functional OOP, Clojure style
-
Upload
yoavrubin -
Category
Technology
-
view
2.190 -
download
0
description
Transcript of Functional OOP, Clojure style
![Page 1: Functional OOP, Clojure style](https://reader033.fdocuments.net/reader033/viewer/2022061118/546999dbaf795909568b74e9/html5/thumbnails/1.jpg)
![Page 2: Functional OOP, Clojure style](https://reader033.fdocuments.net/reader033/viewer/2022061118/546999dbaf795909568b74e9/html5/thumbnails/2.jpg)
Functional OOP,Clojure Style
Yoav Rubin
![Page 3: Functional OOP, Clojure style](https://reader033.fdocuments.net/reader033/viewer/2022061118/546999dbaf795909568b74e9/html5/thumbnails/3.jpg)
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 [email protected],:blog http://yoavrubin.blogspot.com,:twitter @yoavrubin}
![Page 4: Functional OOP, Clojure style](https://reader033.fdocuments.net/reader033/viewer/2022061118/546999dbaf795909568b74e9/html5/thumbnails/4.jpg)
First thing first
![Page 5: Functional OOP, Clojure style](https://reader033.fdocuments.net/reader033/viewer/2022061118/546999dbaf795909568b74e9/html5/thumbnails/5.jpg)
Alan Kay
![Page 6: Functional OOP, Clojure style](https://reader033.fdocuments.net/reader033/viewer/2022061118/546999dbaf795909568b74e9/html5/thumbnails/6.jpg)
Alan Kay Edsger W. Dijkstra
![Page 7: Functional OOP, Clojure style](https://reader033.fdocuments.net/reader033/viewer/2022061118/546999dbaf795909568b74e9/html5/thumbnails/7.jpg)
Alan Kay Edsger W. Dijkstra
![Page 8: Functional OOP, Clojure style](https://reader033.fdocuments.net/reader033/viewer/2022061118/546999dbaf795909568b74e9/html5/thumbnails/8.jpg)
“Perspective is worth 80 IQ points”
Alan Kay
![Page 9: Functional OOP, Clojure style](https://reader033.fdocuments.net/reader033/viewer/2022061118/546999dbaf795909568b74e9/html5/thumbnails/9.jpg)
• OOP
• Clojure
Agenda
in
![Page 10: Functional OOP, Clojure style](https://reader033.fdocuments.net/reader033/viewer/2022061118/546999dbaf795909568b74e9/html5/thumbnails/10.jpg)
What’s in a software
• Data types that describe the elements of the domain (nouns)
• State changing operations (verbs)
![Page 11: Functional OOP, Clojure style](https://reader033.fdocuments.net/reader033/viewer/2022061118/546999dbaf795909568b74e9/html5/thumbnails/11.jpg)
Type \ functionalityf1f2f3f4
T1XX
T2X
T3X
T4XXXX
The software matrix
![Page 12: Functional OOP, Clojure style](https://reader033.fdocuments.net/reader033/viewer/2022061118/546999dbaf795909568b74e9/html5/thumbnails/12.jpg)
Type \ functionalityf1f2f3f4
T1XX
T2X
T3X
T4 XXXX
The software matrix
Data types, nouns
![Page 13: Functional OOP, Clojure style](https://reader033.fdocuments.net/reader033/viewer/2022061118/546999dbaf795909568b74e9/html5/thumbnails/13.jpg)
Type \ functionalityf1f2f3f4
T1XX
T2X
T3X
T4 XXXX
The software matrixAPI, verbs, interfaces
![Page 14: Functional OOP, Clojure style](https://reader033.fdocuments.net/reader033/viewer/2022061118/546999dbaf795909568b74e9/html5/thumbnails/14.jpg)
Type \ functionalityf1f2f3f4
T1XX
T2X
T3X
T4XXXX
The software matrix
implementations of operationx by Typey
![Page 15: Functional OOP, Clojure style](https://reader033.fdocuments.net/reader033/viewer/2022061118/546999dbaf795909568b74e9/html5/thumbnails/15.jpg)
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
![Page 16: Functional OOP, Clojure style](https://reader033.fdocuments.net/reader033/viewer/2022061118/546999dbaf795909568b74e9/html5/thumbnails/16.jpg)
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
![Page 17: Functional OOP, Clojure style](https://reader033.fdocuments.net/reader033/viewer/2022061118/546999dbaf795909568b74e9/html5/thumbnails/17.jpg)
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
![Page 18: Functional OOP, Clojure style](https://reader033.fdocuments.net/reader033/viewer/2022061118/546999dbaf795909568b74e9/html5/thumbnails/18.jpg)
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
![Page 19: Functional OOP, Clojure style](https://reader033.fdocuments.net/reader033/viewer/2022061118/546999dbaf795909568b74e9/html5/thumbnails/19.jpg)
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
![Page 20: Functional OOP, Clojure style](https://reader033.fdocuments.net/reader033/viewer/2022061118/546999dbaf795909568b74e9/html5/thumbnails/20.jpg)
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
![Page 21: Functional OOP, Clojure style](https://reader033.fdocuments.net/reader033/viewer/2022061118/546999dbaf795909568b74e9/html5/thumbnails/21.jpg)
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
![Page 22: Functional OOP, Clojure style](https://reader033.fdocuments.net/reader033/viewer/2022061118/546999dbaf795909568b74e9/html5/thumbnails/22.jpg)
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
![Page 23: Functional OOP, Clojure style](https://reader033.fdocuments.net/reader033/viewer/2022061118/546999dbaf795909568b74e9/html5/thumbnails/23.jpg)
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
![Page 24: Functional OOP, Clojure style](https://reader033.fdocuments.net/reader033/viewer/2022061118/546999dbaf795909568b74e9/html5/thumbnails/24.jpg)
Type \ functionality
f1f2f3f4
T1XX
T2X
T3X
T4 XXX
Inheritance
![Page 25: Functional OOP, Clojure style](https://reader033.fdocuments.net/reader033/viewer/2022061118/546999dbaf795909568b74e9/html5/thumbnails/25.jpg)
Type \ functionality
f1f2f3f4
T1XX
T2X
T3X
T4 XXX
Inheritance
T4 can say that it is a T3
![Page 26: Functional OOP, Clojure style](https://reader033.fdocuments.net/reader033/viewer/2022061118/546999dbaf795909568b74e9/html5/thumbnails/26.jpg)
Type \ functionality
f1f2f3f4
T1XX
T2X
T3X
T4 XXX
Inheritance
T4 can say that it is a T3
and its implementation of f3 is found at T3
X
![Page 27: Functional OOP, Clojure style](https://reader033.fdocuments.net/reader033/viewer/2022061118/546999dbaf795909568b74e9/html5/thumbnails/27.jpg)
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
![Page 28: Functional OOP, Clojure style](https://reader033.fdocuments.net/reader033/viewer/2022061118/546999dbaf795909568b74e9/html5/thumbnails/28.jpg)
How is it all related to Clojure?
![Page 29: Functional OOP, Clojure style](https://reader033.fdocuments.net/reader033/viewer/2022061118/546999dbaf795909568b74e9/html5/thumbnails/29.jpg)
What is Clojure
![Page 30: Functional OOP, Clojure style](https://reader033.fdocuments.net/reader033/viewer/2022061118/546999dbaf795909568b74e9/html5/thumbnails/30.jpg)
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
![Page 31: Functional OOP, Clojure style](https://reader033.fdocuments.net/reader033/viewer/2022061118/546999dbaf795909568b74e9/html5/thumbnails/31.jpg)
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
![Page 32: Functional OOP, Clojure style](https://reader033.fdocuments.net/reader033/viewer/2022061118/546999dbaf795909568b74e9/html5/thumbnails/32.jpg)
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
![Page 33: Functional OOP, Clojure style](https://reader033.fdocuments.net/reader033/viewer/2022061118/546999dbaf795909568b74e9/html5/thumbnails/33.jpg)
How to define rows in Clojure?
![Page 34: Functional OOP, Clojure style](https://reader033.fdocuments.net/reader033/viewer/2022061118/546999dbaf795909568b74e9/html5/thumbnails/34.jpg)
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
![Page 35: Functional OOP, Clojure style](https://reader033.fdocuments.net/reader033/viewer/2022061118/546999dbaf795909568b74e9/html5/thumbnails/35.jpg)
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
![Page 36: Functional OOP, Clojure style](https://reader033.fdocuments.net/reader033/viewer/2022061118/546999dbaf795909568b74e9/html5/thumbnails/36.jpg)
The type metaobjectDefinition:
Instantiation:
Usage:
![Page 37: Functional OOP, Clojure style](https://reader033.fdocuments.net/reader033/viewer/2022061118/546999dbaf795909568b74e9/html5/thumbnails/37.jpg)
The type metaobjectDefinition:
Instantiation:
Usage:
![Page 38: Functional OOP, Clojure style](https://reader033.fdocuments.net/reader033/viewer/2022061118/546999dbaf795909568b74e9/html5/thumbnails/38.jpg)
The type metaobjectDefinition:
Instantiation:
Usage:
![Page 39: Functional OOP, Clojure style](https://reader033.fdocuments.net/reader033/viewer/2022061118/546999dbaf795909568b74e9/html5/thumbnails/39.jpg)
The type metaobjectDefinition:
Instantiation:
Usage:
![Page 40: Functional OOP, Clojure style](https://reader033.fdocuments.net/reader033/viewer/2022061118/546999dbaf795909568b74e9/html5/thumbnails/40.jpg)
Two main use cases
• You really know what you are doing
• You’re doing it wrong
![Page 41: Functional OOP, Clojure style](https://reader033.fdocuments.net/reader033/viewer/2022061118/546999dbaf795909568b74e9/html5/thumbnails/41.jpg)
The Record metaobject• Similar to the Type metaobject
• Provides a map like behavior
• No mutability
![Page 42: Functional OOP, Clojure style](https://reader033.fdocuments.net/reader033/viewer/2022061118/546999dbaf795909568b74e9/html5/thumbnails/42.jpg)
So far in the software matrix
• Added new rows– New types / records– No new APIs
• Associate with an existing column
![Page 43: Functional OOP, Clojure style](https://reader033.fdocuments.net/reader033/viewer/2022061118/546999dbaf795909568b74e9/html5/thumbnails/43.jpg)
So far in the software matrix
• Added new rows– New types / records– No new APIs
• Associate with an existing column
Functional Abstraction
![Page 44: Functional OOP, Clojure style](https://reader033.fdocuments.net/reader033/viewer/2022061118/546999dbaf795909568b74e9/html5/thumbnails/44.jpg)
So far in the software matrix
• Added new rows– New types / records– No new APIs
• Associate with an existing column
Functional Abstraction
Polymorphism
![Page 45: Functional OOP, Clojure style](https://reader033.fdocuments.net/reader033/viewer/2022061118/546999dbaf795909568b74e9/html5/thumbnails/45.jpg)
How to define columns in Clojure?
![Page 46: Functional OOP, Clojure style](https://reader033.fdocuments.net/reader033/viewer/2022061118/546999dbaf795909568b74e9/html5/thumbnails/46.jpg)
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
![Page 47: Functional OOP, Clojure style](https://reader033.fdocuments.net/reader033/viewer/2022061118/546999dbaf795909568b74e9/html5/thumbnails/47.jpg)
![Page 48: Functional OOP, Clojure style](https://reader033.fdocuments.net/reader033/viewer/2022061118/546999dbaf795909568b74e9/html5/thumbnails/48.jpg)
![Page 49: Functional OOP, Clojure style](https://reader033.fdocuments.net/reader033/viewer/2022061118/546999dbaf795909568b74e9/html5/thumbnails/49.jpg)
Now there’s a new tree in town
![Page 50: Functional OOP, Clojure style](https://reader033.fdocuments.net/reader033/viewer/2022061118/546999dbaf795909568b74e9/html5/thumbnails/50.jpg)
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
![Page 51: Functional OOP, Clojure style](https://reader033.fdocuments.net/reader033/viewer/2022061118/546999dbaf795909568b74e9/html5/thumbnails/51.jpg)
A deeper look
Type \ functionalityf1f2f3tree-map
GeneralTreeXXX
YetAnotherTypeX
BinaryTreeXX
![Page 52: Functional OOP, Clojure style](https://reader033.fdocuments.net/reader033/viewer/2022061118/546999dbaf795909568b74e9/html5/thumbnails/52.jpg)
A deeper look
Type \ functionalityf1f2f3tree-map
GeneralTreeXXX
YetAnotherTypeX
BinaryTreeXX
New column header (API)
![Page 53: Functional OOP, Clojure style](https://reader033.fdocuments.net/reader033/viewer/2022061118/546999dbaf795909568b74e9/html5/thumbnails/53.jpg)
A deeper look
Type \ functionalityf1f2f3tree-map
GeneralTreeXXX
YetAnotherTypeX
BinaryTreeXX
New column header (API)
Two implementations
![Page 54: Functional OOP, Clojure style](https://reader033.fdocuments.net/reader033/viewer/2022061118/546999dbaf795909568b74e9/html5/thumbnails/54.jpg)
A deeper look
Type \ functionalityf1f2f3tree-map
GeneralTreeXXX
YetAnotherTypeX
BinaryTreeXX
New column header (API)
Two implementations
QuadTree
![Page 55: Functional OOP, Clojure style](https://reader033.fdocuments.net/reader033/viewer/2022061118/546999dbaf795909568b74e9/html5/thumbnails/55.jpg)
A deeper look
Type \ functionalityf1f2f3tree-map
GeneralTreeXXX
YetAnotherTypeX
BinaryTreeXX
New column header (API)
Two implementations
QuadTree ?
![Page 56: Functional OOP, Clojure style](https://reader033.fdocuments.net/reader033/viewer/2022061118/546999dbaf795909568b74e9/html5/thumbnails/56.jpg)
A deeper look
Type \ functionalityf1f2f3tree-map
GeneralTreeXXX
YetAnotherTypeX
BinaryTreeXX
New column header (API)
Two implementations
QuadTree ?
Tree-map did too much!!!
![Page 57: Functional OOP, Clojure style](https://reader033.fdocuments.net/reader033/viewer/2022061118/546999dbaf795909568b74e9/html5/thumbnails/57.jpg)
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
![Page 58: Functional OOP, Clojure style](https://reader033.fdocuments.net/reader033/viewer/2022061118/546999dbaf795909568b74e9/html5/thumbnails/58.jpg)
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
![Page 59: Functional OOP, Clojure style](https://reader033.fdocuments.net/reader033/viewer/2022061118/546999dbaf795909568b74e9/html5/thumbnails/59.jpg)
Protocol
• A set of several function signatures– Just the signature, without implementation– Dispatch is done based on the run-time type
![Page 60: Functional OOP, Clojure style](https://reader033.fdocuments.net/reader033/viewer/2022061118/546999dbaf795909568b74e9/html5/thumbnails/60.jpg)
Protocol
• A set of several function signatures– Just the signature, without implementation– Dispatch is done based on the run-time type
The protocol name
![Page 61: Functional OOP, Clojure style](https://reader033.fdocuments.net/reader033/viewer/2022061118/546999dbaf795909568b74e9/html5/thumbnails/61.jpg)
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)
![Page 62: Functional OOP, Clojure style](https://reader033.fdocuments.net/reader033/viewer/2022061118/546999dbaf795909568b74e9/html5/thumbnails/62.jpg)
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
![Page 63: Functional OOP, Clojure style](https://reader033.fdocuments.net/reader033/viewer/2022061118/546999dbaf795909568b74e9/html5/thumbnails/63.jpg)
![Page 64: Functional OOP, Clojure style](https://reader033.fdocuments.net/reader033/viewer/2022061118/546999dbaf795909568b74e9/html5/thumbnails/64.jpg)
![Page 65: Functional OOP, Clojure style](https://reader033.fdocuments.net/reader033/viewer/2022061118/546999dbaf795909568b74e9/html5/thumbnails/65.jpg)
![Page 66: Functional OOP, Clojure style](https://reader033.fdocuments.net/reader033/viewer/2022061118/546999dbaf795909568b74e9/html5/thumbnails/66.jpg)
![Page 67: Functional OOP, Clojure style](https://reader033.fdocuments.net/reader033/viewer/2022061118/546999dbaf795909568b74e9/html5/thumbnails/67.jpg)
Added to an existing type a new API
Without changing the type
![Page 68: Functional OOP, Clojure style](https://reader033.fdocuments.net/reader033/viewer/2022061118/546999dbaf795909568b74e9/html5/thumbnails/68.jpg)
Added to an existing type a new API
Without changing the type
Functional polymorphism
![Page 69: Functional OOP, Clojure style](https://reader033.fdocuments.net/reader033/viewer/2022061118/546999dbaf795909568b74e9/html5/thumbnails/69.jpg)
Still, there are limitations
Protocols allow type based dispatch only
![Page 70: Functional OOP, Clojure style](https://reader033.fdocuments.net/reader033/viewer/2022061118/546999dbaf795909568b74e9/html5/thumbnails/70.jpg)
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
![Page 71: Functional OOP, Clojure style](https://reader033.fdocuments.net/reader033/viewer/2022061118/546999dbaf795909568b74e9/html5/thumbnails/71.jpg)
(dispatch-fn)
take-care-of
::moon
::sun(tco-sun)
(tco-moon)
disp
atch
er
![Page 72: Functional OOP, Clojure style](https://reader033.fdocuments.net/reader033/viewer/2022061118/546999dbaf795909568b74e9/html5/thumbnails/72.jpg)
(dispatch-fn)
take-care-of
::moon
::sun(tco-sun)
(tco-moon)
This is the exposed API
disp
atch
er
![Page 73: Functional OOP, Clojure style](https://reader033.fdocuments.net/reader033/viewer/2022061118/546999dbaf795909568b74e9/html5/thumbnails/73.jpg)
(dispatch-fn)
take-care-of
::moon
::sun(tco-sun)
(tco-moon)
disp
atch
er
![Page 74: Functional OOP, Clojure style](https://reader033.fdocuments.net/reader033/viewer/2022061118/546999dbaf795909568b74e9/html5/thumbnails/74.jpg)
(dispatch-fn)
take-care-of
::moon
::sun(tco-sun)
(tco-moon)
disp
atch
er
![Page 75: Functional OOP, Clojure style](https://reader033.fdocuments.net/reader033/viewer/2022061118/546999dbaf795909568b74e9/html5/thumbnails/75.jpg)
(dispatch-fn)
take-care-of
::moon
::sun(tco-sun)
::lightning
(tco-lightning)
(tco-moon)
disp
atch
er
![Page 76: Functional OOP, Clojure style](https://reader033.fdocuments.net/reader033/viewer/2022061118/546999dbaf795909568b74e9/html5/thumbnails/76.jpg)
![Page 77: Functional OOP, Clojure style](https://reader033.fdocuments.net/reader033/viewer/2022061118/546999dbaf795909568b74e9/html5/thumbnails/77.jpg)
The multi method name
![Page 78: Functional OOP, Clojure style](https://reader033.fdocuments.net/reader033/viewer/2022061118/546999dbaf795909568b74e9/html5/thumbnails/78.jpg)
The dispatching function
![Page 79: Functional OOP, Clojure style](https://reader033.fdocuments.net/reader033/viewer/2022061118/546999dbaf795909568b74e9/html5/thumbnails/79.jpg)
Meanwhile, at other namespaces
![Page 80: Functional OOP, Clojure style](https://reader033.fdocuments.net/reader033/viewer/2022061118/546999dbaf795909568b74e9/html5/thumbnails/80.jpg)
Meanwhile, at other namespaces
![Page 81: Functional OOP, Clojure style](https://reader033.fdocuments.net/reader033/viewer/2022061118/546999dbaf795909568b74e9/html5/thumbnails/81.jpg)
![Page 82: Functional OOP, Clojure style](https://reader033.fdocuments.net/reader033/viewer/2022061118/546999dbaf795909568b74e9/html5/thumbnails/82.jpg)
![Page 83: Functional OOP, Clojure style](https://reader033.fdocuments.net/reader033/viewer/2022061118/546999dbaf795909568b74e9/html5/thumbnails/83.jpg)
Meanwhile, at other namespaces
![Page 84: Functional OOP, Clojure style](https://reader033.fdocuments.net/reader033/viewer/2022061118/546999dbaf795909568b74e9/html5/thumbnails/84.jpg)
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
![Page 85: Functional OOP, Clojure style](https://reader033.fdocuments.net/reader033/viewer/2022061118/546999dbaf795909568b74e9/html5/thumbnails/85.jpg)
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
![Page 86: Functional OOP, Clojure style](https://reader033.fdocuments.net/reader033/viewer/2022061118/546999dbaf795909568b74e9/html5/thumbnails/86.jpg)
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
![Page 87: Functional OOP, Clojure style](https://reader033.fdocuments.net/reader033/viewer/2022061118/546999dbaf795909568b74e9/html5/thumbnails/87.jpg)
Is-a relationship
![Page 88: Functional OOP, Clojure style](https://reader033.fdocuments.net/reader033/viewer/2022061118/546999dbaf795909568b74e9/html5/thumbnails/88.jpg)
Is-a relationship
![Page 89: Functional OOP, Clojure style](https://reader033.fdocuments.net/reader033/viewer/2022061118/546999dbaf795909568b74e9/html5/thumbnails/89.jpg)
Is-a relationship
![Page 90: Functional OOP, Clojure style](https://reader033.fdocuments.net/reader033/viewer/2022061118/546999dbaf795909568b74e9/html5/thumbnails/90.jpg)
Is-a relationship
![Page 91: Functional OOP, Clojure style](https://reader033.fdocuments.net/reader033/viewer/2022061118/546999dbaf795909568b74e9/html5/thumbnails/91.jpg)
Is-a relationship
![Page 92: Functional OOP, Clojure style](https://reader033.fdocuments.net/reader033/viewer/2022061118/546999dbaf795909568b74e9/html5/thumbnails/92.jpg)
Is-a relationship
![Page 93: Functional OOP, Clojure style](https://reader033.fdocuments.net/reader033/viewer/2022061118/546999dbaf795909568b74e9/html5/thumbnails/93.jpg)
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
![Page 94: Functional OOP, Clojure style](https://reader033.fdocuments.net/reader033/viewer/2022061118/546999dbaf795909568b74e9/html5/thumbnails/94.jpg)
Type \ functionality
f1f2f3f4
T1XX
T2X
T3X
T4 XXXX
T4 isa T2 (for f2)
T4 isa T3 (for f3)
![Page 95: Functional OOP, Clojure style](https://reader033.fdocuments.net/reader033/viewer/2022061118/546999dbaf795909568b74e9/html5/thumbnails/95.jpg)
Type \ functionality
f1f2f3f4
T1XX
T2X
T3X
T4 XXXX
T4 isa T2 (for f2)
T4 isa T3 (for f3)
Functional inheritance
![Page 96: Functional OOP, Clojure style](https://reader033.fdocuments.net/reader033/viewer/2022061118/546999dbaf795909568b74e9/html5/thumbnails/96.jpg)
Summary
• We’ve seen– Functional abstraction– Functional information hiding– Functional polymorphism– Functional inheritance
![Page 97: Functional OOP, Clojure style](https://reader033.fdocuments.net/reader033/viewer/2022061118/546999dbaf795909568b74e9/html5/thumbnails/97.jpg)
Summary
• We’ve seen functional– Abstraction– Information hiding– Polymorphism– Inheritance
![Page 98: Functional OOP, Clojure style](https://reader033.fdocuments.net/reader033/viewer/2022061118/546999dbaf795909568b74e9/html5/thumbnails/98.jpg)
Summary
• We’ve seen functional OOP
![Page 99: Functional OOP, Clojure style](https://reader033.fdocuments.net/reader033/viewer/2022061118/546999dbaf795909568b74e9/html5/thumbnails/99.jpg)
Summary
• We’ve seen functional OOP
Clojure Style
![Page 100: Functional OOP, Clojure style](https://reader033.fdocuments.net/reader033/viewer/2022061118/546999dbaf795909568b74e9/html5/thumbnails/100.jpg)
Thank
You!