Functional OOP,Clojure StyleYoav Rubin
About me• Software engineer at IBM Research - Haifa– Development of development environments– Large scale productsto small...
First thing first
Alan Kay
Alan Kay Edsger W. Dijkstra
Alan Kay Edsger W. Dijkstra
“Perspective is worth 80 IQ points”Alan Kay
• OOP• ClojureAgendain
What’s in a software• Data types that describe the elements ofthe domain (nouns)• State changing operations (verbs)
Type  functionality f1 f2 f3 f4T1 X XT2 XT3 XT4 X X X XThe software matrix
Type  functionality f1 f2 f3 f4T1 X XT2 XT3 XT4 X X X XThe software matrixData types, nouns
Type  functionality f1 f2 f3 f4T1 X XT2 XT3 XT4 X X X XThe software matrixAPI, verbs,interfaces
Type  functionality f1 f2 f3 f4T1 X XT2 XT3 XT4 X X X XThe software matriximplementations ofoperationx by Typey
Data directed programmingThe expression problemPhilip WadlerSICPDeciding which function to use basedon given dataHow to ad...
Let’s talk OOP• The rows are the classes– The domain abstractions we define and use– Which can hide within them their inte...
Let’s talk OOP• The rows are the classes– The domain abstractions we define and use– Which can hide within them their inte...
Let’s talk OOP• The rows are the classes– The domain abstractions we define and use– Which can hide within them their inte...
Let’s talk OOP• The rows are the classes– The domain abstractions we define and use– Which can hide within them their inte...
Let’s talk OOP• The rows are the classes– The domain abstractions we define and use– Which can hide within them their inte...
Let’s talk OOP• The rows are the classes– The domain abstractions we define and use– Which can hide within them their inte...
Let’s talk OOP• The rows are the classes– The domain abstractions we define and use– Which can hide within them their inte...
Let’s talk OOP• The rows are the classes– The domain abstractions we define and use– Which can hide within them their inte...
Type functionalityf1 f2 f3 f4T1 X XT2 XT3 XT4 X X XInheritance
Type functionalityf1 f2 f3 f4T1 X XT2 XT3 XT4 X X XInheritanceT4 can say that it is aT3
Type functionalityf1 f2 f3 f4T1 X XT2 XT3 XT4 X X XInheritanceT4 can say that it is aT3and its implementationof f3 is foun...
OOP on the matrix• The rows are the classes– The domain abstractions we define and use– Which can hide within them their i...
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 elem...
General structure• A Clojure project is built of namespaces• In each namespace there are functionsand data elements• Funct...
General structure• A Clojure project is built of namespaces• In each namespace there are functionsand data elements• Funct...
How to define rows in Clojure?
Creating new types• Metaobjects – a mechanism that allowsdescription and creation of new datatypes• We can create our own ...
The Type metaobject• Upon definition we need to provide:– Name– Member fields– APIs to implement and their implementation•...
The type metaobjectDefinition:Instantiation:Usage:
The type metaobjectDefinition:Instantiation:Usage:
The type metaobjectDefinition:Instantiation:Usage:
The type metaobjectDefinition: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 columnFunction...
So far in the software matrix• Added new rows– New types / records– No new APIs• Associate with an existing columnFunction...
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 sim...
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 tre...
A deeper lookType  functionality f1 f2 f3 tree-mapGeneralTree X X XYetAnotherType XBinaryTree X X
A deeper lookType  functionality f1 f2 f3 tree-mapGeneralTree X X XYetAnotherType XBinaryTree X XNew column header (API)
A deeper lookType  functionality f1 f2 f3 tree-mapGeneralTree X X XYetAnotherType XBinaryTree X XNew column header (API)Tw...
A deeper lookType  functionality f1 f2 f3 tree-mapGeneralTree X X XYetAnotherType XBinaryTree X XNew column header (API)Tw...
A deeper lookType  functionality f1 f2 f3 tree-mapGeneralTree X X XYetAnotherType XBinaryTree X XNew column header (API)Tw...
A deeper lookType  functionality f1 f2 f3 tree-mapGeneralTree X X XYetAnotherType XBinaryTree X XNew column header (API)Tw...
In the software matrix:Need to decomplect the creation ofcolumns headers from cell markingOr in software design language:W...
Creating abstract APIs• No concrete implementation• Define a semantic unit– A set of behaviors that compose an API• In ano...
Protocol• A set of several function signatures– Just the signature, without implementation– Dispatch is done based on the ...
Protocol• A set of several function signatures– Just the signature, without implementation– Dispatch is done based on the ...
Protocol• A set of several function signatures– Just the signature, without implementation– Dispatch is done based on the ...
Protocols and types• The linking of a protocol to a type can bedone not as part of the definition of thetype• This results...
Added to an existing type anew APIWithout changing the type
Added to an existing type anew APIWithout changing the typeFunctional polymorphism
Still, there are limitationsProtocols allow type based dispatch only
Multi methods• Polymorphism which is based on a userdefined dispatching function• The result of the execution of the dispa...
(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 APIdispatcher
(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 differentdata elements• All we need to know is that they obey thatAPI• We can in...
Multi method• We can use the same API for differentdata elements• All we need to know is that they obey thatAPI• We can in...
Is-a relationship• We can define that A is-a B• The dispatcher would handle A the sameway that it handles B• (derive ::A :...
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 multimethod– The return value may differ if (derive…) wa...
Type functionalityf1 f2 f3 f4T1 X XT2 XT3 XT4 X X X XT4 isa T2 (for f2)T4 isa T3 (for f3)
Type functionalityf1 f2 f3 f4T1 X XT2 XT3 XT4 X X X XT4 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 OOPClojure Style
ThankYou!
Functional OOP, Clojure style
Functional OOP, Clojure style
Functional OOP, Clojure style
Functional OOP, Clojure style
Functional OOP, Clojure style
Functional OOP, Clojure style
Functional OOP, Clojure style
Functional OOP, Clojure style
Functional OOP, Clojure style
Functional OOP, Clojure style
Upcoming SlideShare
Loading in …5
×

Functional OOP, Clojure style

3,610 views

Published on

The presentation for the talk given at the reversim summit

Published in: Technology, Business
0 Comments
9 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
3,610
On SlideShare
0
From Embeds
0
Number of Embeds
64
Actions
Shares
0
Downloads
46
Comments
0
Likes
9
Embeds 0
No embeds

No notes for slide

Functional OOP, Clojure style

  1. 1. Functional OOP,Clojure StyleYoav Rubin
  2. 2. About me• Software engineer at IBM Research - Haifa– Development of development environments– Large scale productsto small scale researchprojects• Lecture the course “Functionalprogramming on the JVM” in HaifaUniversity{:name Yoav Rubin,:email yoavrubin@gmail.com,:blog http://yoavrubin.blogspot.com,:twitter @yoavrubin}
  3. 3. First thing first
  4. 4. Alan Kay
  5. 5. Alan Kay Edsger W. Dijkstra
  6. 6. Alan Kay Edsger W. Dijkstra
  7. 7. “Perspective is worth 80 IQ points”Alan Kay
  8. 8. • OOP• ClojureAgendain
  9. 9. What’s in a software• Data types that describe the elements ofthe domain (nouns)• State changing operations (verbs)
  10. 10. Type functionality f1 f2 f3 f4T1 X XT2 XT3 XT4 X X X XThe software matrix
  11. 11. Type functionality f1 f2 f3 f4T1 X XT2 XT3 XT4 X X X XThe software matrixData types, nouns
  12. 12. Type functionality f1 f2 f3 f4T1 X XT2 XT3 XT4 X X X XThe software matrixAPI, verbs,interfaces
  13. 13. Type functionality f1 f2 f3 f4T1 X XT2 XT3 XT4 X X X XThe software matriximplementations ofoperationx by Typey
  14. 14. Data directed programmingThe expression problemPhilip WadlerSICPDeciding which function to use basedon given dataHow to add rows and columns to thematrix without recompiling whilepreserving static typing
  15. 15. 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 thatclass X implements interface Y– Saying that the implementation itself resides in classZ means that X is-a Z
  16. 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 thatclass X implements interface Y– Saying that the implementation itself resides in classZ means that X is-a ZAbstraction
  17. 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 thatclass X implements interface Y– Saying that the implementation itself resides in classZ means that X is-a Z
  18. 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 thatclass X implements interface Y– Saying that the implementation itself resides in classZ means that X is-a ZInformation hiding
  19. 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 thatclass X implements interface Y– Saying that the implementation itself resides in classZ means that X is-a Z
  20. 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 thatclass X implements interface Y– Saying that the implementation itself resides in classZ means that X is-a ZPolymorphism
  21. 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 thatclass X implements interface Y– Saying that the implementation itself resides in classZ means that X is-a Z
  22. 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 thatclass X implements interface Y– Saying that the implementation itself resides in classZ means that X is-a ZInheritance
  23. 23. Type functionalityf1 f2 f3 f4T1 X XT2 XT3 XT4 X X XInheritance
  24. 24. Type functionalityf1 f2 f3 f4T1 X XT2 XT3 XT4 X X XInheritanceT4 can say that it is aT3
  25. 25. Type functionalityf1 f2 f3 f4T1 X XT2 XT3 XT4 X X XInheritanceT4 can say that it is aT3and its implementationof f3 is found at T3X
  26. 26. 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 thatclass X implements interface Y– Saying that the implementation itself resides in ClassZ means that X is-a Z
  27. 27. How is it all related to Clojure?
  28. 28. What is Clojure
  29. 29. 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
  30. 30. General structure• A Clojure project is built of namespaces• In each namespace there are functionsand data elements• Functions can be either public or private– Either visible or not visible outside of thenamespace
  31. 31. General structure• A Clojure project is built of namespaces• In each namespace there are functionsand data elements• Functions can be either public or private– Either visible or not visible outside of thenamespaceFunctional Information hiding
  32. 32. How to define rows in Clojure?
  33. 33. Creating new types• Metaobjects – a mechanism that allowsdescription 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
  34. 34. 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
  35. 35. The type metaobjectDefinition:Instantiation:Usage:
  36. 36. The type metaobjectDefinition:Instantiation:Usage:
  37. 37. The type metaobjectDefinition:Instantiation:Usage:
  38. 38. The type metaobjectDefinition:Instantiation:Usage:
  39. 39. Two main use cases• You really know what you are doing• You’re doing it wrong
  40. 40. The Record metaobject• Similar to the Type metaobject• Provides a map like behavior• No mutability
  41. 41. So far in the software matrix• Added new rows– New types / records– No new APIs• Associate with an existing column
  42. 42. So far in the software matrix• Added new rows– New types / records– No new APIs• Associate with an existing columnFunctional Abstraction
  43. 43. So far in the software matrix• Added new rows– New types / records– No new APIs• Associate with an existing columnFunctional AbstractionPolymorphism
  44. 44. How to define columns in Clojure?
  45. 45. 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
  46. 46. Now there’s a new tree in town
  47. 47. What can a developer do?• Re-write the existing tree-map function– Because editing legacy code is fun…• Create another tree-map in another namespaceand qualify its calls– Name collisions• Create tree-map2– Complicating both developer’s and user’s code
  48. 48. A deeper lookType functionality f1 f2 f3 tree-mapGeneralTree X X XYetAnotherType XBinaryTree X X
  49. 49. A deeper lookType functionality f1 f2 f3 tree-mapGeneralTree X X XYetAnotherType XBinaryTree X XNew column header (API)
  50. 50. A deeper lookType functionality f1 f2 f3 tree-mapGeneralTree X X XYetAnotherType XBinaryTree X XNew column header (API)Twoimplementations
  51. 51. A deeper lookType functionality f1 f2 f3 tree-mapGeneralTree X X XYetAnotherType XBinaryTree X XNew column header (API)TwoimplementationsQuadTree
  52. 52. A deeper lookType functionality f1 f2 f3 tree-mapGeneralTree X X XYetAnotherType XBinaryTree X XNew column header (API)TwoimplementationsQuadTree ?
  53. 53. A deeper lookType functionality f1 f2 f3 tree-mapGeneralTree X X XYetAnotherType XBinaryTree X XNew column header (API)TwoimplementationsQuadTree ?Tree-map did too much!!!
  54. 54. In the software matrix:Need to decomplect the creation ofcolumns headers from cell markingOr in software design language:We need to separate the definition of anAPI from its implementation
  55. 55. Creating abstract APIs• No concrete implementation• Define a semantic unit– A set of behaviors that compose an API• In another place define the mappingbetween data types and the API– Marking of a cell in the matrix
  56. 56. Protocol• A set of several function signatures– Just the signature, without implementation– Dispatch is done based on the run-time type
  57. 57. Protocol• A set of several function signatures– Just the signature, without implementation– Dispatch is done based on the run-time typeThe protocol name
  58. 58. Protocol• A set of several function signatures– Just the signature, without implementation– Dispatch is done based on the run-time typeThe protocol nameA function signature(there can be several of these)
  59. 59. Protocols and types• The linking of a protocol to a type can bedone not as part of the definition of thetype• This results in the possibility to extendexisting, compiled types– Extend String– Extend even nil
  60. 60. Added to an existing type anew APIWithout changing the type
  61. 61. Added to an existing type anew APIWithout changing the typeFunctional polymorphism
  62. 62. Still, there are limitationsProtocols allow type based dispatch only
  63. 63. Multi methods• Polymorphism which is based on a userdefined dispatching function• The result of the execution of the dispatchfunction determines which implementationwill be executes
  64. 64. (dispatch-fn)take-care-of::moon::sun(tco-sun)(tco-moon)dispatcher
  65. 65. (dispatch-fn)take-care-of::moon::sun(tco-sun)(tco-moon)This is the exposed APIdispatcher
  66. 66. (dispatch-fn)take-care-of::moon::sun(tco-sun)(tco-moon)dispatcher
  67. 67. (dispatch-fn)take-care-of::moon::sun(tco-sun)(tco-moon)dispatcher
  68. 68. (dispatch-fn)take-care-of::moon::sun(tco-sun)::lightning(tco-lightning)(tco-moon)dispatcher
  69. 69. The multi method name
  70. 70. The dispatching function
  71. 71. Meanwhile, at other namespaces
  72. 72. Meanwhile, at other namespaces
  73. 73. Meanwhile, at other namespaces
  74. 74. Multi method• We can use the same API for differentdata elements• All we need to know is that they obey thatAPI• We can introduce new APIs for existingtypes
  75. 75. Multi method• We can use the same API for differentdata elements• All we need to know is that they obey thatAPI• We can introduce new APIs for existingtypesFunctional polymorphism
  76. 76. Is-a relationship• We can define that A is-a B• The dispatcher would handle A the sameway 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
  77. 77. Is-a relationship
  78. 78. Is-a relationship
  79. 79. Is-a relationship
  80. 80. Is-a relationship
  81. 81. Is-a relationship
  82. 82. Is-a relationship
  83. 83. Why did it work• Derive harms the referential transparency of the multimethod– 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 thenamespace
  84. 84. Type functionalityf1 f2 f3 f4T1 X XT2 XT3 XT4 X X X XT4 isa T2 (for f2)T4 isa T3 (for f3)
  85. 85. Type functionalityf1 f2 f3 f4T1 X XT2 XT3 XT4 X X X XT4 isa T2 (for f2)T4 isa T3 (for f3)Functional inheritance
  86. 86. Summary• We’ve seen– Functional abstraction– Functional information hiding– Functional polymorphism– Functional inheritance
  87. 87. Summary• We’ve seen functional– Abstraction– Information hiding– Polymorphism– Inheritance
  88. 88. Summary• We’ve seen functional OOP
  89. 89. Summary• We’ve seen functional OOPClojure Style
  90. 90. ThankYou!

×