SlideShare une entreprise Scribd logo
1  sur  84
Télécharger pour lire hors ligne
 
( life w/o objects )
( oop → functional  design )
( with clojure )
 
 
(Osvaldas Grigas | )@ogrigas
(oop hangover)
type systems
"hybrid" languages
(design)
(monolithic design)
(modular design)
(design process)
Before A er
(oo design)
Noun-oriented
Verb-oriented
(nouns)
CustomerDAO
CustomerService
CustomerController
(verbs)
RegisterCustomer
PromoteCustomerToVIP
RenderCustomerProfilePage
Domain-driven Design?
 
"As a teller, I want to transfer funds between accounts."
(oo design principles)
Single Responsibility
Interface Segregation
Dependency Inversion
(single responsibility)
public class ZipDownloadService { 
    List<File> downloadAndExtract(String location) { 
        ... 
    }
}
public class FileDownloader { 
    List<File> downloadFiles(String location) { 
        ... 
    }
}
public class ZipExtractor { 
    File extractZip(File archive) { 
        ... 
    }
}
Or ... just functions
(defn download­files [location] 
  (...)) 
(defn extract­zip [archive] 
  (...))
(clojure)
in a nutshell
(clojure)
in a nutshell
 calculate(5,2,9)
(clojure)
in a nutshell
(calculate 5,2,9)
(clojure)
in a nutshell
(calculate 5 2 9)
(function definition)
(defn calculate [a b c] 
  (+ a (* b c)))
(interface segregation)
public class ProductCatalog 
{ 
    public void Save(Product product) 
    {
        ... 
    }
    public Product FindById(int productId) 
    {
        ... 
    }
}
public class ProductSaver 
{ 
    public void Save(Product product) 
    {
        ... 
    }
}
public class ProductFinder 
{ 
    public Product FindById(int id) 
    {
        ... 
    }
}
Somethin' ain't right
public class ProductRepository 
{ 
    public void Save(Product product) 
    {
        ... 
    }
}
public class ProductQuery 
{ 
    public Product FindById(int id) 
    {
        ... 
    }
}
Feelin' good now
Or ... just functions
(defn save­product [product] 
  (...)) 
(defn find­product­by­id [id] 
  (...))
Applying OO design principles
o en leads to
functional design
(what's missing)
(what's missing)
Code organization
Inheritance
Encapsulation
Composition
Polymorphism
State management
(code organization)
(ns my.product.repository) 
(defn save [product] 
  (...)) 
(defn find­by­id [id] 
  (...))
(ns my.other.namespace 
  (:require [my.product.repository :as product­repo])) 
(product­repo/find­by­id 42)
(inheritance)
 
(inheritance)
just don't
(encapsulation)
(encapsulation)
Data is not an object
 
(encapsulation)
Data is not an object
Data is immutable
(html [:body 
        [:h1 "Employees"] 
        [:ul 
        (for [person employees] 
          [:li 
            [:h2 (:name person)] 
            [:p (:email person)]])]])
(defn handle [request] 
  {:status 200 
   :headers {"Content­Type" "application/json" 
             "X­Custom­Header" "12345"} 
   :body (json/generate­string 
           {:name "John Wick" 
            :age 42 
            :email "john@wick.name" 
            :hobbies ["guns" "dogs" "judo"]})})
append-child array-map assoc associative? assoc-in bean bigdec bigint biginteger bit-and bit-and-not bit-clear
bit-flip bit-not bit-or bit-set bit-shi -le bit-shi -right bit-test bit-xor blank? branch? butlast byte capitalize cat
char char? char-escape-string char-name-string children coll? compare concat conj cons contains? count
counted? cycle dec dec' decimal? dedupe dense-int-set diff difference disj dissoc distinct distinct? double
down drop drop-last drop-while edit eduction empty empty? end? enumeration-seq escape even? every?
false? ffirst file-seq filter filterv find find-keyword first flatten float float? fnext for format frequencies gensym
get get-in group-by hash-map hash-set identical? inc inc' index insert-child insert-le insert-right int integer?
interleave interpose intersection int-map into into-array int-set iterate iterator-seq join keep keep-indexed key
keys keyword keyword? last lazy-cat lazy-seq le le most le s line-seq list list? list* long lower-case make-
node map map? mapcat map-indexed map-invert mapv max max-key merge merge-with min min-key mod
neg? next nfirst nil? nnext node not not= not-any? not-empty not-every? nth nthnext nthrest num number?
odd? partition partition-all partition-by path peek pmap pop pos? postwalk postwalk-demo postwalk-replace
prev prewalk prewalk-demo prewalk-replace priority-map project quot rand rand-int rand-nth random-sample
range ratio? rational? rationalize reduce reduce-kv reductions re-find re-groups rem re-matcher re-matches
remove rename rename-keys re-pattern repeat repeatedly replace replace-first re-quote-replacement re-seq
rest resultset-seq reverse reversible? right rightmost rights root rseq rsubseq second select select-keys seq
seq? seque sequence sequential? seq-zip set set? short shuffle some some? sort sort-by sorted? sorted-map
sorted-map-by sorted-set sorted-set-by split split-at split-lines split-with str string? subs subseq subset?
subvec superset? symbol symbol? take take-last take-nth take-while to-array-2d transduce tree-seq trim triml
trim-newline trimr true? unchecked-add unchecked-dec unchecked-inc unchecked-multiply unchecked-
negate unchecked-subtract union unsigned-bit-shi -right up update update-in upper-case val vals vec vector
vector? vector-of vector-zip walk when-first xml-seq xml-zip zero? zipmap
(composition)
(dependency inversion)
Good for decoupling
Good for isolated testing
(oo dependencies)
public class ProfilePage { 
    String render(Repository repo, int customerId) { 
        return toHtml(repo.loadProfile(customerId)); 
    }
}
Repository repo = new SqlRepository(); 
ProfilePage page = new ProfilePage();
String html = page.render(repo, customerId);
(fp dependencies)
(defn render­page [repository­fn customer­id] 
  (to­html (repository­fn customer­id)))
(defn load­from­sql [customer­id] 
  (...))
(render­page load­from­sql customer­id)
(basic fp polymorphism)
All functions implement the "Strategy" pattern
(oo composition)
Repository repo = new SqlRepository(); 
ProfilePage page = new ProfilePage(repo);
page.render(customerId);
(function closure)
(defn inject [f arg1] 
  (fn [arg2] (f arg1 arg2)))
(def render­from­db 
  (inject render­page load­from­sql))
(render­from­db customer­id)
(partial application)
(def render­from­db 
  (partial render­page load­from­sql))
(render­from­db customer­id)
"Object is a collection of partially-applied functions."
( J.B. Rainsberger )
(structural patterns)
Adapter
Decorator
...
("adapter" pattern)
(defn to­view­model [profile] (...)) 
(render­page (comp to­view­model load­from­sql) id)
("decorator" pattern)
(defn traceable [f] 
  (fn [& args] 
    (log/trace "Called with params:" args) 
    (apply f args)))
(render­page (traceable load­from­sql) id)
(polymorphism)
(oo polymorphism)
"Subtype Polymorphism"
(dispatch on the type of first argument)
public interface JsonObj { 
    String toJson(); 
}
public class JsonString implements JsonObj { 
    private final String value; 
    public JsonString(String value) { 
        this.value = value; 
    }
    public String toJson() { 
        return """ + value + """; 
    }
}
public class JsonList implements JsonObj { 
    private final List<JsonObj> items; 
    public JsonString(JsonObj... items) { 
        this.items = asList(items); 
    }
    public String toJson() { 
        return "[" + items.stream() 
            .map(item ­> item.toJson()) 
            .collect(joining(",")) + "]"; 
    }
}
JsonObj obj = new JsonList( 
    new JsonString("a"), 
    new JsonList( 
        new JsonString("b"), 
        new JsonString("c") 
    ), 
    new JsonString("d") 
); 
System.out.println(obj.toJson()); 
// ["a",["b","c"],"d"]
(limitations)
Subtyping is coupled to implementation
 
 
(limitations)
Subtyping is coupled to implementation
... cannot extend existing types
 
(limitations)
Subtyping is coupled to implementation
... cannot extend existing types
... need wrapper classes
Too constraining!
(clojure protocols)
dispatch on the type of first argument
(defprotocol Json 
  (to­json [this]))
(extend­type String Json 
  (to­json [this] 
    (str """ this """)))
(extend­type List Json 
  (to­json [this] 
    (str "[" (str/join "," (map to­json this)) "]")))
(extend­type nil Json 
  (to­json [this] 
    "null"))
(to­json ["a" ["b" "c"] nil "d"]) 
;;=> ["a",["b","c"],null,"d"]
Why stop there?
(clojure multimethods)
dispatch on anything!
(defmulti greet :country) 
(defmethod greet "PL" [person] 
  (println "Dzień dobry," (:name person))) 
(defmethod greet "FR" [person] 
  (println "Bonjour," (:name person) "!")) 
(defmethod greet :default [person] 
  (println "Hello," (:name person)))
(greet {:name "Jacques" :country "FR"}) 
;;=> Bonjour, Jacques !
(state management)
State Machine / Business Process / Entity
"Class" as a namespace
(def Account 
  {'deposit  (fn [state amount] 
               (update state :balance + amount)) 
   'withdraw (fn [state amount] 
               (if (< amount (state :balance)) 
                 (update state :balance ­ amount) 
                 state))})
(def state0 {:balance 0})
(def state1 ((Account 'deposit)  state0 100)) 
(def state2 ((Account 'withdraw) state1 30)) 
(def state3 ((Account 'withdraw) state2 20))
Partially apply state with "constructor"
(defn new­object [clazz initial­state] 
  .......... 
    .......... 
      ..........)
(def acc (new­object Account {:balance 0})) 
(acc 'deposit 100) 
(acc 'withdraw 30) 
(acc 'withdraw 20)
Partially apply state with "constructor"
(defn new­object [clazz initial­state] 
  (let [state­ref (atom initial­state)] 
    .......... 
      ..........))
(def acc (new­object Account {:balance 0})) 
(acc 'deposit 100) 
(acc 'withdraw 30) 
(acc 'withdraw 20)
Partially apply state with "constructor"
(defn new­object [clazz initial­state] 
  (let [state­ref (atom initial­state)] 
    (fn [method & args] 
      ..........)))
(def acc (new­object Account {:balance 0})) 
(acc 'deposit 100) 
(acc 'withdraw 30) 
(acc 'withdraw 20)
Partially apply state with "constructor"
(defn new­object [clazz initial­state] 
  (let [state­ref (atom initial­state)] 
    (fn [method & args] 
      (apply swap! state­ref (clazz method) args))))
(def acc (new­object Account {:balance 0})) 
(acc 'deposit 100) 
(acc 'withdraw 30) 
(acc 'withdraw 20)
(the takeaway)
Eierlegende Wollmilchsau
(the takeaway)
Object
(google "Clojure Made Simple")
(questions? "Osvaldas Grigas" @ogrigas)

Contenu connexe

En vedette

En vedette (9)

Programuotojo karjeros mitai
Programuotojo karjeros mitaiProgramuotojo karjeros mitai
Programuotojo karjeros mitai
 
Modular Design - Laws and Principles - in OOP and FP
Modular Design - Laws and Principles - in OOP and FPModular Design - Laws and Principles - in OOP and FP
Modular Design - Laws and Principles - in OOP and FP
 
Evolution of Macros
Evolution of MacrosEvolution of Macros
Evolution of Macros
 
Name Stuff
Name StuffName Stuff
Name Stuff
 
Simplicity
SimplicitySimplicity
Simplicity
 
Framework-less Applications
Framework-less ApplicationsFramework-less Applications
Framework-less Applications
 
Simplicity
SimplicitySimplicity
Simplicity
 
Chinese market
Chinese marketChinese market
Chinese market
 
The Top Skills That Can Get You Hired in 2017
The Top Skills That Can Get You Hired in 2017The Top Skills That Can Get You Hired in 2017
The Top Skills That Can Get You Hired in 2017
 

Similaire à Life without Objects (w/ Clojure)

Ejercicios de estilo en la programación
Ejercicios de estilo en la programaciónEjercicios de estilo en la programación
Ejercicios de estilo en la programación
Software Guru
 
Refactoring to Macros with Clojure
Refactoring to Macros with ClojureRefactoring to Macros with Clojure
Refactoring to Macros with Clojure
Dmitry Buzdin
 
Introduction to R
Introduction to RIntroduction to R
Introduction to R
agnonchik
 

Similaire à Life without Objects (w/ Clojure) (20)

Spark + Clojure for Topic Discovery - Zalando Tech Clojure/Conj Talk
Spark + Clojure for Topic Discovery - Zalando Tech Clojure/Conj TalkSpark + Clojure for Topic Discovery - Zalando Tech Clojure/Conj Talk
Spark + Clojure for Topic Discovery - Zalando Tech Clojure/Conj Talk
 
Pune Clojure Course Outline
Pune Clojure Course OutlinePune Clojure Course Outline
Pune Clojure Course Outline
 
Ejercicios de estilo en la programación
Ejercicios de estilo en la programaciónEjercicios de estilo en la programación
Ejercicios de estilo en la programación
 
Introductionto fp with groovy
Introductionto fp with groovyIntroductionto fp with groovy
Introductionto fp with groovy
 
Big Data Analytics with Scala at SCALA.IO 2013
Big Data Analytics with Scala at SCALA.IO 2013Big Data Analytics with Scala at SCALA.IO 2013
Big Data Analytics with Scala at SCALA.IO 2013
 
Spark training-in-bangalore
Spark training-in-bangaloreSpark training-in-bangalore
Spark training-in-bangalore
 
Hadoop trainingin bangalore
Hadoop trainingin bangaloreHadoop trainingin bangalore
Hadoop trainingin bangalore
 
Working with Complex Types in DataFrames: Optics to the Rescue
Working with Complex Types in DataFrames: Optics to the RescueWorking with Complex Types in DataFrames: Optics to the Rescue
Working with Complex Types in DataFrames: Optics to the Rescue
 
R for Pythonistas (PyData NYC 2017)
R for Pythonistas (PyData NYC 2017)R for Pythonistas (PyData NYC 2017)
R for Pythonistas (PyData NYC 2017)
 
Introduction To Scala
Introduction To ScalaIntroduction To Scala
Introduction To Scala
 
Refactoring to Macros with Clojure
Refactoring to Macros with ClojureRefactoring to Macros with Clojure
Refactoring to Macros with Clojure
 
Contracts in-clojure-pete
Contracts in-clojure-peteContracts in-clojure-pete
Contracts in-clojure-pete
 
JavaScript Foundations Day1
JavaScript Foundations Day1JavaScript Foundations Day1
JavaScript Foundations Day1
 
Groovy unleashed
Groovy unleashed Groovy unleashed
Groovy unleashed
 
Introduction to R
Introduction to RIntroduction to R
Introduction to R
 
Advance Scala - Oleg Mürk
Advance Scala - Oleg MürkAdvance Scala - Oleg Mürk
Advance Scala - Oleg Mürk
 
Intro to Functional Programming in Scala
Intro to Functional Programming in ScalaIntro to Functional Programming in Scala
Intro to Functional Programming in Scala
 
Big Data LDN 2017: Processing Fast Data With Apache Spark: the Tale of Two APIs
Big Data LDN 2017: Processing Fast Data With Apache Spark: the Tale of Two APIsBig Data LDN 2017: Processing Fast Data With Apache Spark: the Tale of Two APIs
Big Data LDN 2017: Processing Fast Data With Apache Spark: the Tale of Two APIs
 
Libretto
LibrettoLibretto
Libretto
 
Scala Refactoring for Fun and Profit
Scala Refactoring for Fun and ProfitScala Refactoring for Fun and Profit
Scala Refactoring for Fun and Profit
 

Dernier

Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
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
panagenda
 
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
Safe Software
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Dernier (20)

Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
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
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
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
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
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
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu SubbuApidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
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
 
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
 
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
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Apidays New York 2024 - 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...
 
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
 
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 

Life without Objects (w/ Clojure)