SlideShare uma empresa Scribd logo
1 de 73
Baixar para ler offline
Cracking Clojure
Alex Miller
Revelytix
Clojure
• A Lisp on the JVM         (also ClojureScript on JavaScript)

• Dynamic (types, code, etc)
• Functional language
• Compiled (there is no interpreter)
• Immutability and state management
• Code is data
• REPL - Read / Eval / Print / Loop
• Interactive development


                                                                 2
It looks like this...
  (defn neighbors [[x y]]
   (for [dx [-1 0 1]
        dy (if (zero? dx)
           [-1 1]
           [-1 0 1])]
     [(+ dx x) (+ dy y)]))

  (defn live [n alive?]
   (or (= n 3)
      (and (= n 2) alive?)))

  (defn step [world]
   (set
    (for [[cell n] (frequencies (mapcat neighbors world))
         :when (live n (world cell))]
      cell)))

  (defn life [initial-world]
   (iterate step initial-world))
                                                            3
Primitives




             4
Collections




              5
Sequences




            6
Sequences




            6
Functions




            7
Functions




            7
Functions




            7
Compiler




           8
Casting spells




                 9
Creating functions




                     10
Creating functions




                     10
Creating functions




                     10
Creating functions




                     10
Creating functions




                     10
An example...




                11
map




      12
Sequence of lines




                    13
Sequence of files




                    14
Sequence of files




                    14
Sequence functions




                     15
You




      16
Lazy sequences




                 17
18
18
Power




        19
Objects




          20
Maps as cheap objects




                        21
Records




          22
Java
package domain;                                                            	     	     int result = 1;
                                                                           	     	     result = prime *   result + Float.floatToIntBits(alcohol);
public class Beer {                                                        	     	     result = prime *   result + ((beer == null) ? 0 : beer.hashCode());
	     private String beer;                                                 	     	     result = prime *   result + ((brewery == null) ? 0 :
	     private String brewery;                                              brewery.hashCode());
	     private float alcohol;                                               	     	     result = prime *   result + ibu;
	     private int ibu;                                                     	     	     return result;
	                                                                          	     }
	     public Beer(String beer, String brewery, float alcohol, int ibu) {
	     	     super();                                                       	     @Override
	     	     this.beer = beer;                                              	     public boolean equals(Object obj) {
	     	     this.brewery = brewery;                                        	     	     if (this == obj)
	     	     this.alcohol = alcohol;                                        	     	     	     return true;
	     	     this.ibu = ibu;                                                	     	     if (obj == null)
	     }                                                                    	     	     	     return false;
                                                                           	     	     if (getClass() != obj.getClass())
	    public String getBeer() {                                             	     	     	     return false;
	    	     return beer;                                                    	     	     Beer other = (Beer) obj;
	    }                                                                     	     	     if (Float.floatToIntBits(alcohol) != Float
	    public String getBrewery() {                                          	     	     	     	     .floatToIntBits(other.alcohol))
	    	     return brewery;                                                 	     	     	     return false;
	    }                                                                     	     	     if (beer == null) {
	    public float getAlcohol() {                                           	     	     	     if (other.beer != null)
	    	     return alcohol;                                                 	     	     	     	     return false;
	    }                                                                     	     	     } else if (!beer.equals(other.beer))
	    public int getIbu() {                                                 	     	     	     return false;
	    	     return ibu;                                                     	     	     if (brewery == null) {
	    }                                                                     	     	     	     if (other.brewery != null)
	    public void setBeer(String beer) {                                    	     	     	     	     return false;
	    	     this.beer = beer;                                               	     	     } else if (!brewery.equals(other.brewery))
	    }                                                                     	     	     	     return false;
	    public void setBrewery(String brewery) {                              	     	     if (ibu != other.ibu)
	    	     this.brewery = brewery;                                         	     	     	     return false;
	    }                                                                     	     	     return true;
	    public void setAlcohol(float alcohol) {                               	     }
	    	     this.alcohol = alcohol;
	    }                                                                     	     @Override
	    public void setIbu(int ibu) {                                         	     public String toString() {
	    	     this.ibu = ibu;                                                 	     	     return "Beer [beer=" + beer + ", brewery=" + brewery + ",
	    }                                                                     alcohol=" + alcohol + ", ibu=" + ibu + "]";
                                                                           	     }
	    @Override                                                             }
	    public int hashCode() {
	    	     final int prime = 31;                                                                                                                       23
Java
package domain;                                                            	     	     int result = 1;
                                                                           	     	     result = prime *   result + Float.floatToIntBits(alcohol);
public class Beer {                                                        	     	     result = prime *   result + ((beer == null) ? 0 : beer.hashCode());
	     private String beer;                                                 	     	     result = prime *   result + ((brewery == null) ? 0 :
	     private String brewery;                                              brewery.hashCode());
	     private float alcohol;         Fields and types                      	     	     result = prime *   result + ibu;
	
	
      private int ibu;                                                     	
                                                                           	
                                                                                 	
                                                                                 }
                                                                                       return result;                                Hashing
	     public Beer(String beer, String brewery, float alcohol, int ibu) {
	     	     super();                                                       	     @Override
	     	     this.beer = beer;                                              	     public boolean equals(Object obj) {
	
	
      	
      	
            this.brewery = brewery;
            this.alcohol = alcohol;
                                     Construction                          	
                                                                           	
                                                                                 	
                                                                                 	
                                                                                       if (this == obj)
                                                                                       	     return true;

                                                                                                                                     Equality
	     	     this.ibu = ibu;                                                	     	     if (obj == null)
	     }                                                                    	     	     	     return false;
                                                                           	     	     if (getClass() != obj.getClass())
	    public String getBeer() {                                             	     	     	     return false;
	    	     return beer;                                                    	     	     Beer other = (Beer) obj;
	    }                                                                     	     	     if (Float.floatToIntBits(alcohol) != Float
	    public String getBrewery() {                                          	     	     	     	     .floatToIntBits(other.alcohol))
	    	     return brewery;                                                 	     	     	     return false;
	
	
     }
     public float getAlcohol() {       Getters                             	
                                                                           	
                                                                                 	
                                                                                 	
                                                                                       if (beer == null) {
                                                                                       	     if (other.beer != null)
	    	     return alcohol;                                                 	     	     	     	     return false;
	    }                                                                     	     	     } else if (!beer.equals(other.beer))
	    public int getIbu() {                                                 	     	     	     return false;
	    	     return ibu;                                                     	     	     if (brewery == null) {
	    }                                                                     	     	     	     if (other.brewery != null)
	    public void setBeer(String beer) {                                    	     	     	     	     return false;
	    	     this.beer = beer;                                               	     	     } else if (!brewery.equals(other.brewery))
	    }                                                                     	     	     	     return false;
	    public void setBrewery(String brewery) {                              	     	     if (ibu != other.ibu)
	    	     this.brewery = brewery;                                         	     	     	     return false;
	    }                                                                     	     	     return true;
	
	
     public void setAlcohol(float alcohol) {
     	     this.alcohol = alcohol;
                                                    Setters                	     }

	    }                                                                     	     @Override
	    public void setIbu(int ibu) {                                         	     public String toString() {
	    	     this.ibu = ibu;                                                 	     	     return "Beer [beer=" + beer + ", brewery=" + brewery + ",
	    }                                                                     alcohol=" + alcohol + ", ibu=" + ibu + "]";

                                                                                                                                         Printing
                                                                           	     }
	    @Override                                                             }
	    public int hashCode() {
	    	     final int prime = 31;                                                                                                                       23
Data interfaces




                  24
Data interfaces




                  24
Data interfaces




                  25
Data - Clojure vs Java




                         26
Data - Clojure vs Java




                         26
Polymorphism




               27
Generic access FTW




                     28
Multimethods




               29
Multimethod dispatch




                       30
Protocols




            31
State




        32
Atoms




        33
Refs




       34
Agents




         35
Destructuring




                36
for comprehensions




                     37
for comprehensions




                     37
Macros




         38
Macros




         39
Review

            CONCURRENCY          HOST




         DATA             CODE




                                        40
Review

                CONCURRENCY          HOST




         DATA                 CODE



         primitives



                                            40
Review

                CONCURRENCY                HOST




         DATA                       CODE


                      collections
         primitives



                                                  40
Review

                CONCURRENCY                HOST




          sequences




         DATA                       CODE


                      collections
         primitives



                                                  40
Review

                CONCURRENCY                  HOST


             laziness


          sequences




         DATA                         CODE


                        collections
         primitives



                                                    40
Review

                CONCURRENCY                       HOST


             laziness


          sequences
                                      FP




         DATA                              CODE


                        collections
         primitives



                                                         40
Review

                CONCURRENCY                                    HOST


             laziness
                                sequence library

          sequences
                                                   FP




         DATA                                           CODE


                        collections
         primitives



                                                                      40
Review

                  CONCURRENCY                                    HOST


               laziness
                                  sequence library

            sequences
                                                     FP

 records
 types

           DATA                                           CODE


                          collections
           primitives



                                                                        40
Review

                  CONCURRENCY                                    HOST


               laziness
                                  sequence library

            sequences
                                                                        multimethods
                                                     FP
                                                                        protocols

 records
 types

           DATA                                           CODE


                          collections
           primitives



                                                                                   40
Review
        refs    agents
atoms

                             CONCURRENCY                                    HOST
        state

                          laziness
                                             sequence library

                         sequences
                                                                                   multimethods
                                                                FP
                                                                                   protocols

  records
  types

                     DATA                                            CODE


                                     collections
                     primitives



                                                                                              40
Review
        refs    agents
atoms

                             CONCURRENCY                                        HOST
        state

                          laziness
                                             sequence library

                         sequences
                                                                                       multimethods
                                                                 FP
                                                                                       protocols

  records
  types

                     DATA                                                CODE


                                     collections
                     primitives
                                                                destructuring


                                                                                                  40
Review
        refs    agents
atoms

                             CONCURRENCY                                        HOST
        state

                          laziness
                                             sequence library

                         sequences
                                                                                       multimethods
                                                                 FP
                                                                                       protocols

  records
  types

                     DATA                          macros                CODE


                                     collections
                     primitives
                                                                destructuring


                                                                                                  40
Review
         refs      agents
atoms

                                CONCURRENCY                                        HOST
         state

                             laziness
                                                sequence library

                            sequences
                                                                                          multimethods
                                                                    FP
                                                                                          protocols

  records
  types

                        DATA                          macros                CODE


        metadata                        collections
                        primitives
                                                                   destructuring
                                            transients

                                                                                                     40
Review
         refs      agents
atoms

                                CONCURRENCY                                        HOST
         state

                             laziness
                                                sequence library

                            sequences
                                                                                          multimethods
                                                                    FP
                                                                                          protocols

  records
  types

                        DATA                          macros                CODE


        metadata                        collections
                                                                                     namespaces
                        primitives
                                                                   destructuring
                                            transients

                                                                                                     40
Review
         refs      agents
atoms

                                CONCURRENCY                                        HOST
         state

                             laziness
                                                sequence library

                            sequences
                                                                                           multimethods
                                                                    FP
                                                                                           protocols

  records
  types
                                                                                          recursion
                        DATA                          macros                CODE


        metadata                        collections
                                                                                     namespaces
                        primitives
                                                                   destructuring
                                            transients

                                                                                                      40
Review
         refs      agents
atoms

                                CONCURRENCY                                           HOST
         state
                                                                                                    Java libs
                             laziness
                                                sequence library                   Java
                            sequences                                              interop
                                                                                                 multimethods
                                                                    FP
                                                                                                 protocols

  records
  types
                                                                                                recursion
                        DATA                          macros                CODE


        metadata                        collections
                                                                                             namespaces
                        primitives
                                                                   destructuring
                                            transients

                                                                                                                40
Review
                                     futures       promises
         refs      agents
atoms
                                                               pmap
                                CONCURRENCY                                              HOST
         state
                                                                                                       Java libs
                             laziness
                                                  sequence library                    Java
                            sequences                                                 interop
                                                                                                    multimethods
                                                                       FP
                                                                                                    protocols

  records
  types
                                                                                                   recursion
                        DATA                          macros                   CODE


        metadata                        collections
                                                                                                namespaces
                        primitives
                                                                      destructuring
                                               transients

                                                                                                                   40
Conway's Life

   Life's rules:
    If alive and 2 or 3 neighbors Then stay alive   Else die
    If dead and 3 neighbors       Then come to life

                       "Blinker" configuration




   Implementation courtesy of Christophe Grand
   http://clj-me.cgrand.net/2011/08/19/conways-game-of-life/

                                                               41
Thanks!

  • Twitter: @puredanger
  • Blog: http://tech.puredanger.com
  • Work: http://revelytix.com
  • My conferences
    – Strange Loop - http://thestrangeloop.com
    – Clojure/West - http://clojurewest.com

   If you want to pair on Clojure during
   Devoxx, ping me on Twitter!
                                                 43

Mais conteúdo relacionado

Mais procurados

JEEConf 2017 - Having fun with Javassist
JEEConf 2017 - Having fun with JavassistJEEConf 2017 - Having fun with Javassist
JEEConf 2017 - Having fun with JavassistAnton Arhipov
 
Java8 tgtbatu javaone
Java8 tgtbatu javaoneJava8 tgtbatu javaone
Java8 tgtbatu javaoneBrian Vermeer
 
JEEConf 2017 - The hitchhiker’s guide to Java class reloading
JEEConf 2017 - The hitchhiker’s guide to Java class reloadingJEEConf 2017 - The hitchhiker’s guide to Java class reloading
JEEConf 2017 - The hitchhiker’s guide to Java class reloadingAnton Arhipov
 
Kotlin: forse è la volta buona (Trento)
Kotlin: forse è la volta buona (Trento)Kotlin: forse è la volta buona (Trento)
Kotlin: forse è la volta buona (Trento)Davide Cerbo
 
Async code on kotlin: rx java or/and coroutines - Kotlin Night Turin
Async code on kotlin: rx java or/and coroutines - Kotlin Night TurinAsync code on kotlin: rx java or/and coroutines - Kotlin Night Turin
Async code on kotlin: rx java or/and coroutines - Kotlin Night TurinFabio Collini
 
Java(8) The Good, The Bad and the Ugly
Java(8) The Good, The Bad and the UglyJava(8) The Good, The Bad and the Ugly
Java(8) The Good, The Bad and the UglyBrian Vermeer
 
Planet-HTML5-Game-Engine Javascript Performance Enhancement
Planet-HTML5-Game-Engine Javascript Performance EnhancementPlanet-HTML5-Game-Engine Javascript Performance Enhancement
Planet-HTML5-Game-Engine Javascript Performance Enhancementup2soul
 
JNI - Java & C in the same project
JNI - Java & C in the same projectJNI - Java & C in the same project
JNI - Java & C in the same projectKarol Wrótniak
 
Davide Cerbo - Kotlin: forse è la volta buona - Codemotion Milan 2017
Davide Cerbo - Kotlin: forse è la volta buona - Codemotion Milan 2017 Davide Cerbo - Kotlin: forse è la volta buona - Codemotion Milan 2017
Davide Cerbo - Kotlin: forse è la volta buona - Codemotion Milan 2017 Codemotion
 
Java8 tgtbatu devoxxuk18
Java8 tgtbatu devoxxuk18Java8 tgtbatu devoxxuk18
Java8 tgtbatu devoxxuk18Brian Vermeer
 
2020 Droid Knights CustomLint 적용기
2020 Droid Knights CustomLint 적용기2020 Droid Knights CustomLint 적용기
2020 Droid Knights CustomLint 적용기Insung Hwang
 
How to Clone Flappy Bird in Swift
How to Clone Flappy Bird in SwiftHow to Clone Flappy Bird in Swift
How to Clone Flappy Bird in SwiftGiordano Scalzo
 

Mais procurados (14)

Kotlin meets Gadsu
Kotlin meets GadsuKotlin meets Gadsu
Kotlin meets Gadsu
 
JEEConf 2017 - Having fun with Javassist
JEEConf 2017 - Having fun with JavassistJEEConf 2017 - Having fun with Javassist
JEEConf 2017 - Having fun with Javassist
 
Java8 tgtbatu javaone
Java8 tgtbatu javaoneJava8 tgtbatu javaone
Java8 tgtbatu javaone
 
JEEConf 2017 - The hitchhiker’s guide to Java class reloading
JEEConf 2017 - The hitchhiker’s guide to Java class reloadingJEEConf 2017 - The hitchhiker’s guide to Java class reloading
JEEConf 2017 - The hitchhiker’s guide to Java class reloading
 
Kotlin: forse è la volta buona (Trento)
Kotlin: forse è la volta buona (Trento)Kotlin: forse è la volta buona (Trento)
Kotlin: forse è la volta buona (Trento)
 
Async code on kotlin: rx java or/and coroutines - Kotlin Night Turin
Async code on kotlin: rx java or/and coroutines - Kotlin Night TurinAsync code on kotlin: rx java or/and coroutines - Kotlin Night Turin
Async code on kotlin: rx java or/and coroutines - Kotlin Night Turin
 
Java(8) The Good, The Bad and the Ugly
Java(8) The Good, The Bad and the UglyJava(8) The Good, The Bad and the Ugly
Java(8) The Good, The Bad and the Ugly
 
Planet-HTML5-Game-Engine Javascript Performance Enhancement
Planet-HTML5-Game-Engine Javascript Performance EnhancementPlanet-HTML5-Game-Engine Javascript Performance Enhancement
Planet-HTML5-Game-Engine Javascript Performance Enhancement
 
JNI - Java & C in the same project
JNI - Java & C in the same projectJNI - Java & C in the same project
JNI - Java & C in the same project
 
Java VS Python
Java VS PythonJava VS Python
Java VS Python
 
Davide Cerbo - Kotlin: forse è la volta buona - Codemotion Milan 2017
Davide Cerbo - Kotlin: forse è la volta buona - Codemotion Milan 2017 Davide Cerbo - Kotlin: forse è la volta buona - Codemotion Milan 2017
Davide Cerbo - Kotlin: forse è la volta buona - Codemotion Milan 2017
 
Java8 tgtbatu devoxxuk18
Java8 tgtbatu devoxxuk18Java8 tgtbatu devoxxuk18
Java8 tgtbatu devoxxuk18
 
2020 Droid Knights CustomLint 적용기
2020 Droid Knights CustomLint 적용기2020 Droid Knights CustomLint 적용기
2020 Droid Knights CustomLint 적용기
 
How to Clone Flappy Bird in Swift
How to Clone Flappy Bird in SwiftHow to Clone Flappy Bird in Swift
How to Clone Flappy Bird in Swift
 

Destaque

Clojure: The Art of Abstraction
Clojure: The Art of AbstractionClojure: The Art of Abstraction
Clojure: The Art of AbstractionAlex Miller
 
Scaling Your Cache
Scaling Your CacheScaling Your Cache
Scaling Your CacheAlex Miller
 
Releasing Relational Data to the Semantic Web
Releasing Relational Data to the Semantic WebReleasing Relational Data to the Semantic Web
Releasing Relational Data to the Semantic WebAlex Miller
 
Innovative Software
Innovative SoftwareInnovative Software
Innovative SoftwareAlex Miller
 
Caching In The Cloud
Caching In The CloudCaching In The Cloud
Caching In The CloudAlex Miller
 
Stream Execution with Clojure and Fork/join
Stream Execution with Clojure and Fork/joinStream Execution with Clojure and Fork/join
Stream Execution with Clojure and Fork/joinAlex Miller
 
Project Fortress
Project FortressProject Fortress
Project FortressAlex Miller
 
Groovy concurrency
Groovy concurrencyGroovy concurrency
Groovy concurrencyAlex Miller
 
Scaling Hibernate with Terracotta
Scaling Hibernate with TerracottaScaling Hibernate with Terracotta
Scaling Hibernate with TerracottaAlex Miller
 
Java Concurrency Gotchas
Java Concurrency GotchasJava Concurrency Gotchas
Java Concurrency GotchasAlex Miller
 
Visualising Data on Interactive Maps
Visualising Data on Interactive MapsVisualising Data on Interactive Maps
Visualising Data on Interactive MapsAnna Pawlicka
 
Concurrent Stream Processing
Concurrent Stream ProcessingConcurrent Stream Processing
Concurrent Stream ProcessingAlex Miller
 
Strange Loop Conference 2009
Strange Loop Conference 2009Strange Loop Conference 2009
Strange Loop Conference 2009Alex Miller
 
Tree Editing with Zippers
Tree Editing with ZippersTree Editing with Zippers
Tree Editing with ZippersAlex Miller
 
Erlang - Because s**t Happens by Mahesh Paolini-Subramanya
Erlang - Because s**t Happens by Mahesh Paolini-SubramanyaErlang - Because s**t Happens by Mahesh Paolini-Subramanya
Erlang - Because s**t Happens by Mahesh Paolini-SubramanyaHakka Labs
 
Messaging With Erlang And Jabber
Messaging With  Erlang And  JabberMessaging With  Erlang And  Jabber
Messaging With Erlang And Jabberl xf
 
Winning the Erlang Edit•Build•Test Cycle
Winning the Erlang Edit•Build•Test CycleWinning the Erlang Edit•Build•Test Cycle
Winning the Erlang Edit•Build•Test CycleRusty Klophaus
 

Destaque (20)

Clojure: The Art of Abstraction
Clojure: The Art of AbstractionClojure: The Art of Abstraction
Clojure: The Art of Abstraction
 
Blogging ZOMG
Blogging ZOMGBlogging ZOMG
Blogging ZOMG
 
Scaling Your Cache
Scaling Your CacheScaling Your Cache
Scaling Your Cache
 
Releasing Relational Data to the Semantic Web
Releasing Relational Data to the Semantic WebReleasing Relational Data to the Semantic Web
Releasing Relational Data to the Semantic Web
 
Innovative Software
Innovative SoftwareInnovative Software
Innovative Software
 
Caching In The Cloud
Caching In The CloudCaching In The Cloud
Caching In The Cloud
 
Stream Execution with Clojure and Fork/join
Stream Execution with Clojure and Fork/joinStream Execution with Clojure and Fork/join
Stream Execution with Clojure and Fork/join
 
Cold Hard Cache
Cold Hard CacheCold Hard Cache
Cold Hard Cache
 
Project Fortress
Project FortressProject Fortress
Project Fortress
 
Groovy concurrency
Groovy concurrencyGroovy concurrency
Groovy concurrency
 
Scaling Hibernate with Terracotta
Scaling Hibernate with TerracottaScaling Hibernate with Terracotta
Scaling Hibernate with Terracotta
 
Java Concurrency Gotchas
Java Concurrency GotchasJava Concurrency Gotchas
Java Concurrency Gotchas
 
Visualising Data on Interactive Maps
Visualising Data on Interactive MapsVisualising Data on Interactive Maps
Visualising Data on Interactive Maps
 
Concurrent Stream Processing
Concurrent Stream ProcessingConcurrent Stream Processing
Concurrent Stream Processing
 
Strange Loop Conference 2009
Strange Loop Conference 2009Strange Loop Conference 2009
Strange Loop Conference 2009
 
Tree Editing with Zippers
Tree Editing with ZippersTree Editing with Zippers
Tree Editing with Zippers
 
Erlang - Because s**t Happens by Mahesh Paolini-Subramanya
Erlang - Because s**t Happens by Mahesh Paolini-SubramanyaErlang - Because s**t Happens by Mahesh Paolini-Subramanya
Erlang - Because s**t Happens by Mahesh Paolini-Subramanya
 
Messaging With Erlang And Jabber
Messaging With  Erlang And  JabberMessaging With  Erlang And  Jabber
Messaging With Erlang And Jabber
 
High Performance Erlang
High  Performance  ErlangHigh  Performance  Erlang
High Performance Erlang
 
Winning the Erlang Edit•Build•Test Cycle
Winning the Erlang Edit•Build•Test CycleWinning the Erlang Edit•Build•Test Cycle
Winning the Erlang Edit•Build•Test Cycle
 

Mais de Alex Miller

Clojure/West Overview (12/1/11)
Clojure/West Overview (12/1/11)Clojure/West Overview (12/1/11)
Clojure/West Overview (12/1/11)Alex Miller
 
Scaling Your Cache And Caching At Scale
Scaling Your Cache And Caching At ScaleScaling Your Cache And Caching At Scale
Scaling Your Cache And Caching At ScaleAlex Miller
 
Marshmallow Test
Marshmallow TestMarshmallow Test
Marshmallow TestAlex Miller
 
Java Collections API
Java Collections APIJava Collections API
Java Collections APIAlex Miller
 
Java Concurrency Idioms
Java Concurrency IdiomsJava Concurrency Idioms
Java Concurrency IdiomsAlex Miller
 
Design Patterns Reconsidered
Design Patterns ReconsideredDesign Patterns Reconsidered
Design Patterns ReconsideredAlex Miller
 
Exploring Terracotta
Exploring TerracottaExploring Terracotta
Exploring TerracottaAlex Miller
 
Actor Concurrency
Actor ConcurrencyActor Concurrency
Actor ConcurrencyAlex Miller
 
Java Concurrency Gotchas
Java Concurrency GotchasJava Concurrency Gotchas
Java Concurrency GotchasAlex Miller
 

Mais de Alex Miller (10)

Clojure/West Overview (12/1/11)
Clojure/West Overview (12/1/11)Clojure/West Overview (12/1/11)
Clojure/West Overview (12/1/11)
 
Scaling Your Cache And Caching At Scale
Scaling Your Cache And Caching At ScaleScaling Your Cache And Caching At Scale
Scaling Your Cache And Caching At Scale
 
Marshmallow Test
Marshmallow TestMarshmallow Test
Marshmallow Test
 
Java Collections API
Java Collections APIJava Collections API
Java Collections API
 
Java Concurrency Idioms
Java Concurrency IdiomsJava Concurrency Idioms
Java Concurrency Idioms
 
Design Patterns Reconsidered
Design Patterns ReconsideredDesign Patterns Reconsidered
Design Patterns Reconsidered
 
Java 7 Preview
Java 7 PreviewJava 7 Preview
Java 7 Preview
 
Exploring Terracotta
Exploring TerracottaExploring Terracotta
Exploring Terracotta
 
Actor Concurrency
Actor ConcurrencyActor Concurrency
Actor Concurrency
 
Java Concurrency Gotchas
Java Concurrency GotchasJava Concurrency Gotchas
Java Concurrency Gotchas
 

Último

PHX May 2024 Corporate Presentation Final
PHX May 2024 Corporate Presentation FinalPHX May 2024 Corporate Presentation Final
PHX May 2024 Corporate Presentation FinalPanhandleOilandGas
 
Cheap Rate Call Girls In Noida Sector 62 Metro 959961乂3876
Cheap Rate Call Girls In Noida Sector 62 Metro 959961乂3876Cheap Rate Call Girls In Noida Sector 62 Metro 959961乂3876
Cheap Rate Call Girls In Noida Sector 62 Metro 959961乂3876dlhescort
 
RSA Conference Exhibitor List 2024 - Exhibitors Data
RSA Conference Exhibitor List 2024 - Exhibitors DataRSA Conference Exhibitor List 2024 - Exhibitors Data
RSA Conference Exhibitor List 2024 - Exhibitors DataExhibitors Data
 
It will be International Nurses' Day on 12 May
It will be International Nurses' Day on 12 MayIt will be International Nurses' Day on 12 May
It will be International Nurses' Day on 12 MayNZSG
 
Falcon Invoice Discounting: Empowering Your Business Growth
Falcon Invoice Discounting: Empowering Your Business GrowthFalcon Invoice Discounting: Empowering Your Business Growth
Falcon Invoice Discounting: Empowering Your Business GrowthFalcon investment
 
Call Girls in Delhi, Escort Service Available 24x7 in Delhi 959961-/-3876
Call Girls in Delhi, Escort Service Available 24x7 in Delhi 959961-/-3876Call Girls in Delhi, Escort Service Available 24x7 in Delhi 959961-/-3876
Call Girls in Delhi, Escort Service Available 24x7 in Delhi 959961-/-3876dlhescort
 
Falcon Invoice Discounting: The best investment platform in india for investors
Falcon Invoice Discounting: The best investment platform in india for investorsFalcon Invoice Discounting: The best investment platform in india for investors
Falcon Invoice Discounting: The best investment platform in india for investorsFalcon Invoice Discounting
 
BAGALUR CALL GIRL IN 98274*61493 ❤CALL GIRLS IN ESCORT SERVICE❤CALL GIRL
BAGALUR CALL GIRL IN 98274*61493 ❤CALL GIRLS IN ESCORT SERVICE❤CALL GIRLBAGALUR CALL GIRL IN 98274*61493 ❤CALL GIRLS IN ESCORT SERVICE❤CALL GIRL
BAGALUR CALL GIRL IN 98274*61493 ❤CALL GIRLS IN ESCORT SERVICE❤CALL GIRLkapoorjyoti4444
 
Call Girls Zirakpur👧 Book Now📱7837612180 📞👉Call Girl Service In Zirakpur No A...
Call Girls Zirakpur👧 Book Now📱7837612180 📞👉Call Girl Service In Zirakpur No A...Call Girls Zirakpur👧 Book Now📱7837612180 📞👉Call Girl Service In Zirakpur No A...
Call Girls Zirakpur👧 Book Now📱7837612180 📞👉Call Girl Service In Zirakpur No A...Sheetaleventcompany
 
Falcon's Invoice Discounting: Your Path to Prosperity
Falcon's Invoice Discounting: Your Path to ProsperityFalcon's Invoice Discounting: Your Path to Prosperity
Falcon's Invoice Discounting: Your Path to Prosperityhemanthkumar470700
 
Call Girls In Noida 959961⊹3876 Independent Escort Service Noida
Call Girls In Noida 959961⊹3876 Independent Escort Service NoidaCall Girls In Noida 959961⊹3876 Independent Escort Service Noida
Call Girls In Noida 959961⊹3876 Independent Escort Service Noidadlhescort
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756dollysharma2066
 
Call Girls Jp Nagar Just Call 👗 7737669865 👗 Top Class Call Girl Service Bang...
Call Girls Jp Nagar Just Call 👗 7737669865 👗 Top Class Call Girl Service Bang...Call Girls Jp Nagar Just Call 👗 7737669865 👗 Top Class Call Girl Service Bang...
Call Girls Jp Nagar Just Call 👗 7737669865 👗 Top Class Call Girl Service Bang...amitlee9823
 
Russian Call Girls In Gurgaon ❤️8448577510 ⊹Best Escorts Service In 24/7 Delh...
Russian Call Girls In Gurgaon ❤️8448577510 ⊹Best Escorts Service In 24/7 Delh...Russian Call Girls In Gurgaon ❤️8448577510 ⊹Best Escorts Service In 24/7 Delh...
Russian Call Girls In Gurgaon ❤️8448577510 ⊹Best Escorts Service In 24/7 Delh...lizamodels9
 
Eluru Call Girls Service ☎ ️93326-06886 ❤️‍🔥 Enjoy 24/7 Escort Service
Eluru Call Girls Service ☎ ️93326-06886 ❤️‍🔥 Enjoy 24/7 Escort ServiceEluru Call Girls Service ☎ ️93326-06886 ❤️‍🔥 Enjoy 24/7 Escort Service
Eluru Call Girls Service ☎ ️93326-06886 ❤️‍🔥 Enjoy 24/7 Escort ServiceDamini Dixit
 
SEO Case Study: How I Increased SEO Traffic & Ranking by 50-60% in 6 Months
SEO Case Study: How I Increased SEO Traffic & Ranking by 50-60%  in 6 MonthsSEO Case Study: How I Increased SEO Traffic & Ranking by 50-60%  in 6 Months
SEO Case Study: How I Increased SEO Traffic & Ranking by 50-60% in 6 MonthsIndeedSEO
 
Katrina Personal Brand Project and portfolio 1
Katrina Personal Brand Project and portfolio 1Katrina Personal Brand Project and portfolio 1
Katrina Personal Brand Project and portfolio 1kcpayne
 
Russian Call Girls In Rajiv Chowk Gurgaon ❤️8448577510 ⊹Best Escorts Service ...
Russian Call Girls In Rajiv Chowk Gurgaon ❤️8448577510 ⊹Best Escorts Service ...Russian Call Girls In Rajiv Chowk Gurgaon ❤️8448577510 ⊹Best Escorts Service ...
Russian Call Girls In Rajiv Chowk Gurgaon ❤️8448577510 ⊹Best Escorts Service ...lizamodels9
 
Call Girls Ludhiana Just Call 98765-12871 Top Class Call Girl Service Available
Call Girls Ludhiana Just Call 98765-12871 Top Class Call Girl Service AvailableCall Girls Ludhiana Just Call 98765-12871 Top Class Call Girl Service Available
Call Girls Ludhiana Just Call 98765-12871 Top Class Call Girl Service AvailableSeo
 

Último (20)

PHX May 2024 Corporate Presentation Final
PHX May 2024 Corporate Presentation FinalPHX May 2024 Corporate Presentation Final
PHX May 2024 Corporate Presentation Final
 
Cheap Rate Call Girls In Noida Sector 62 Metro 959961乂3876
Cheap Rate Call Girls In Noida Sector 62 Metro 959961乂3876Cheap Rate Call Girls In Noida Sector 62 Metro 959961乂3876
Cheap Rate Call Girls In Noida Sector 62 Metro 959961乂3876
 
RSA Conference Exhibitor List 2024 - Exhibitors Data
RSA Conference Exhibitor List 2024 - Exhibitors DataRSA Conference Exhibitor List 2024 - Exhibitors Data
RSA Conference Exhibitor List 2024 - Exhibitors Data
 
It will be International Nurses' Day on 12 May
It will be International Nurses' Day on 12 MayIt will be International Nurses' Day on 12 May
It will be International Nurses' Day on 12 May
 
Falcon Invoice Discounting: Empowering Your Business Growth
Falcon Invoice Discounting: Empowering Your Business GrowthFalcon Invoice Discounting: Empowering Your Business Growth
Falcon Invoice Discounting: Empowering Your Business Growth
 
Call Girls in Delhi, Escort Service Available 24x7 in Delhi 959961-/-3876
Call Girls in Delhi, Escort Service Available 24x7 in Delhi 959961-/-3876Call Girls in Delhi, Escort Service Available 24x7 in Delhi 959961-/-3876
Call Girls in Delhi, Escort Service Available 24x7 in Delhi 959961-/-3876
 
Falcon Invoice Discounting: The best investment platform in india for investors
Falcon Invoice Discounting: The best investment platform in india for investorsFalcon Invoice Discounting: The best investment platform in india for investors
Falcon Invoice Discounting: The best investment platform in india for investors
 
BAGALUR CALL GIRL IN 98274*61493 ❤CALL GIRLS IN ESCORT SERVICE❤CALL GIRL
BAGALUR CALL GIRL IN 98274*61493 ❤CALL GIRLS IN ESCORT SERVICE❤CALL GIRLBAGALUR CALL GIRL IN 98274*61493 ❤CALL GIRLS IN ESCORT SERVICE❤CALL GIRL
BAGALUR CALL GIRL IN 98274*61493 ❤CALL GIRLS IN ESCORT SERVICE❤CALL GIRL
 
Call Girls Zirakpur👧 Book Now📱7837612180 📞👉Call Girl Service In Zirakpur No A...
Call Girls Zirakpur👧 Book Now📱7837612180 📞👉Call Girl Service In Zirakpur No A...Call Girls Zirakpur👧 Book Now📱7837612180 📞👉Call Girl Service In Zirakpur No A...
Call Girls Zirakpur👧 Book Now📱7837612180 📞👉Call Girl Service In Zirakpur No A...
 
Falcon's Invoice Discounting: Your Path to Prosperity
Falcon's Invoice Discounting: Your Path to ProsperityFalcon's Invoice Discounting: Your Path to Prosperity
Falcon's Invoice Discounting: Your Path to Prosperity
 
Call Girls In Noida 959961⊹3876 Independent Escort Service Noida
Call Girls In Noida 959961⊹3876 Independent Escort Service NoidaCall Girls In Noida 959961⊹3876 Independent Escort Service Noida
Call Girls In Noida 959961⊹3876 Independent Escort Service Noida
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
 
Call Girls Jp Nagar Just Call 👗 7737669865 👗 Top Class Call Girl Service Bang...
Call Girls Jp Nagar Just Call 👗 7737669865 👗 Top Class Call Girl Service Bang...Call Girls Jp Nagar Just Call 👗 7737669865 👗 Top Class Call Girl Service Bang...
Call Girls Jp Nagar Just Call 👗 7737669865 👗 Top Class Call Girl Service Bang...
 
Russian Call Girls In Gurgaon ❤️8448577510 ⊹Best Escorts Service In 24/7 Delh...
Russian Call Girls In Gurgaon ❤️8448577510 ⊹Best Escorts Service In 24/7 Delh...Russian Call Girls In Gurgaon ❤️8448577510 ⊹Best Escorts Service In 24/7 Delh...
Russian Call Girls In Gurgaon ❤️8448577510 ⊹Best Escorts Service In 24/7 Delh...
 
Eluru Call Girls Service ☎ ️93326-06886 ❤️‍🔥 Enjoy 24/7 Escort Service
Eluru Call Girls Service ☎ ️93326-06886 ❤️‍🔥 Enjoy 24/7 Escort ServiceEluru Call Girls Service ☎ ️93326-06886 ❤️‍🔥 Enjoy 24/7 Escort Service
Eluru Call Girls Service ☎ ️93326-06886 ❤️‍🔥 Enjoy 24/7 Escort Service
 
SEO Case Study: How I Increased SEO Traffic & Ranking by 50-60% in 6 Months
SEO Case Study: How I Increased SEO Traffic & Ranking by 50-60%  in 6 MonthsSEO Case Study: How I Increased SEO Traffic & Ranking by 50-60%  in 6 Months
SEO Case Study: How I Increased SEO Traffic & Ranking by 50-60% in 6 Months
 
Katrina Personal Brand Project and portfolio 1
Katrina Personal Brand Project and portfolio 1Katrina Personal Brand Project and portfolio 1
Katrina Personal Brand Project and portfolio 1
 
unwanted pregnancy Kit [+918133066128] Abortion Pills IN Dubai UAE Abudhabi
unwanted pregnancy Kit [+918133066128] Abortion Pills IN Dubai UAE Abudhabiunwanted pregnancy Kit [+918133066128] Abortion Pills IN Dubai UAE Abudhabi
unwanted pregnancy Kit [+918133066128] Abortion Pills IN Dubai UAE Abudhabi
 
Russian Call Girls In Rajiv Chowk Gurgaon ❤️8448577510 ⊹Best Escorts Service ...
Russian Call Girls In Rajiv Chowk Gurgaon ❤️8448577510 ⊹Best Escorts Service ...Russian Call Girls In Rajiv Chowk Gurgaon ❤️8448577510 ⊹Best Escorts Service ...
Russian Call Girls In Rajiv Chowk Gurgaon ❤️8448577510 ⊹Best Escorts Service ...
 
Call Girls Ludhiana Just Call 98765-12871 Top Class Call Girl Service Available
Call Girls Ludhiana Just Call 98765-12871 Top Class Call Girl Service AvailableCall Girls Ludhiana Just Call 98765-12871 Top Class Call Girl Service Available
Call Girls Ludhiana Just Call 98765-12871 Top Class Call Girl Service Available
 

Cracking clojure

  • 2. Clojure • A Lisp on the JVM (also ClojureScript on JavaScript) • Dynamic (types, code, etc) • Functional language • Compiled (there is no interpreter) • Immutability and state management • Code is data • REPL - Read / Eval / Print / Loop • Interactive development 2
  • 3. It looks like this... (defn neighbors [[x y]] (for [dx [-1 0 1] dy (if (zero? dx) [-1 1] [-1 0 1])] [(+ dx x) (+ dy y)])) (defn live [n alive?] (or (= n 3) (and (= n 2) alive?))) (defn step [world] (set (for [[cell n] (frequencies (mapcat neighbors world)) :when (live n (world cell))] cell))) (defn life [initial-world] (iterate step initial-world)) 3
  • 11. Compiler 8
  • 19. map 12
  • 24. You 16
  • 26. 18
  • 27. 18
  • 28. Power 19
  • 29. Objects 20
  • 30. Maps as cheap objects 21
  • 31. Records 22
  • 32. Java package domain; int result = 1; result = prime * result + Float.floatToIntBits(alcohol); public class Beer { result = prime * result + ((beer == null) ? 0 : beer.hashCode()); private String beer; result = prime * result + ((brewery == null) ? 0 : private String brewery; brewery.hashCode()); private float alcohol; result = prime * result + ibu; private int ibu; return result; } public Beer(String beer, String brewery, float alcohol, int ibu) { super(); @Override this.beer = beer; public boolean equals(Object obj) { this.brewery = brewery; if (this == obj) this.alcohol = alcohol; return true; this.ibu = ibu; if (obj == null) } return false; if (getClass() != obj.getClass()) public String getBeer() { return false; return beer; Beer other = (Beer) obj; } if (Float.floatToIntBits(alcohol) != Float public String getBrewery() { .floatToIntBits(other.alcohol)) return brewery; return false; } if (beer == null) { public float getAlcohol() { if (other.beer != null) return alcohol; return false; } } else if (!beer.equals(other.beer)) public int getIbu() { return false; return ibu; if (brewery == null) { } if (other.brewery != null) public void setBeer(String beer) { return false; this.beer = beer; } else if (!brewery.equals(other.brewery)) } return false; public void setBrewery(String brewery) { if (ibu != other.ibu) this.brewery = brewery; return false; } return true; public void setAlcohol(float alcohol) { } this.alcohol = alcohol; } @Override public void setIbu(int ibu) { public String toString() { this.ibu = ibu; return "Beer [beer=" + beer + ", brewery=" + brewery + ", } alcohol=" + alcohol + ", ibu=" + ibu + "]"; } @Override } public int hashCode() { final int prime = 31; 23
  • 33. Java package domain; int result = 1; result = prime * result + Float.floatToIntBits(alcohol); public class Beer { result = prime * result + ((beer == null) ? 0 : beer.hashCode()); private String beer; result = prime * result + ((brewery == null) ? 0 : private String brewery; brewery.hashCode()); private float alcohol; Fields and types result = prime * result + ibu; private int ibu; } return result; Hashing public Beer(String beer, String brewery, float alcohol, int ibu) { super(); @Override this.beer = beer; public boolean equals(Object obj) { this.brewery = brewery; this.alcohol = alcohol; Construction if (this == obj) return true; Equality this.ibu = ibu; if (obj == null) } return false; if (getClass() != obj.getClass()) public String getBeer() { return false; return beer; Beer other = (Beer) obj; } if (Float.floatToIntBits(alcohol) != Float public String getBrewery() { .floatToIntBits(other.alcohol)) return brewery; return false; } public float getAlcohol() { Getters if (beer == null) { if (other.beer != null) return alcohol; return false; } } else if (!beer.equals(other.beer)) public int getIbu() { return false; return ibu; if (brewery == null) { } if (other.brewery != null) public void setBeer(String beer) { return false; this.beer = beer; } else if (!brewery.equals(other.brewery)) } return false; public void setBrewery(String brewery) { if (ibu != other.ibu) this.brewery = brewery; return false; } return true; public void setAlcohol(float alcohol) { this.alcohol = alcohol; Setters } } @Override public void setIbu(int ibu) { public String toString() { this.ibu = ibu; return "Beer [beer=" + beer + ", brewery=" + brewery + ", } alcohol=" + alcohol + ", ibu=" + ibu + "]"; Printing } @Override } public int hashCode() { final int prime = 31; 23
  • 37. Data - Clojure vs Java 26
  • 38. Data - Clojure vs Java 26
  • 43. Protocols 31
  • 44. State 32
  • 45. Atoms 33
  • 46. Refs 34
  • 47. Agents 35
  • 51. Macros 38
  • 52. Macros 39
  • 53. Review CONCURRENCY HOST DATA CODE 40
  • 54. Review CONCURRENCY HOST DATA CODE primitives 40
  • 55. Review CONCURRENCY HOST DATA CODE collections primitives 40
  • 56. Review CONCURRENCY HOST sequences DATA CODE collections primitives 40
  • 57. Review CONCURRENCY HOST laziness sequences DATA CODE collections primitives 40
  • 58. Review CONCURRENCY HOST laziness sequences FP DATA CODE collections primitives 40
  • 59. Review CONCURRENCY HOST laziness sequence library sequences FP DATA CODE collections primitives 40
  • 60. Review CONCURRENCY HOST laziness sequence library sequences FP records types DATA CODE collections primitives 40
  • 61. Review CONCURRENCY HOST laziness sequence library sequences multimethods FP protocols records types DATA CODE collections primitives 40
  • 62. Review refs agents atoms CONCURRENCY HOST state laziness sequence library sequences multimethods FP protocols records types DATA CODE collections primitives 40
  • 63. Review refs agents atoms CONCURRENCY HOST state laziness sequence library sequences multimethods FP protocols records types DATA CODE collections primitives destructuring 40
  • 64. Review refs agents atoms CONCURRENCY HOST state laziness sequence library sequences multimethods FP protocols records types DATA macros CODE collections primitives destructuring 40
  • 65. Review refs agents atoms CONCURRENCY HOST state laziness sequence library sequences multimethods FP protocols records types DATA macros CODE metadata collections primitives destructuring transients 40
  • 66. Review refs agents atoms CONCURRENCY HOST state laziness sequence library sequences multimethods FP protocols records types DATA macros CODE metadata collections namespaces primitives destructuring transients 40
  • 67. Review refs agents atoms CONCURRENCY HOST state laziness sequence library sequences multimethods FP protocols records types recursion DATA macros CODE metadata collections namespaces primitives destructuring transients 40
  • 68. Review refs agents atoms CONCURRENCY HOST state Java libs laziness sequence library Java sequences interop multimethods FP protocols records types recursion DATA macros CODE metadata collections namespaces primitives destructuring transients 40
  • 69. Review futures promises refs agents atoms pmap CONCURRENCY HOST state Java libs laziness sequence library Java sequences interop multimethods FP protocols records types recursion DATA macros CODE metadata collections namespaces primitives destructuring transients 40
  • 70. Conway's Life Life's rules: If alive and 2 or 3 neighbors Then stay alive Else die If dead and 3 neighbors Then come to life "Blinker" configuration Implementation courtesy of Christophe Grand http://clj-me.cgrand.net/2011/08/19/conways-game-of-life/ 41
  • 71.
  • 72.
  • 73. Thanks! • Twitter: @puredanger • Blog: http://tech.puredanger.com • Work: http://revelytix.com • My conferences – Strange Loop - http://thestrangeloop.com – Clojure/West - http://clojurewest.com If you want to pair on Clojure during Devoxx, ping me on Twitter! 43