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

Enhancing and Restoring Safety & Quality Cultures - Dave Litwiller - May 2024...
Enhancing and Restoring Safety & Quality Cultures - Dave Litwiller - May 2024...Enhancing and Restoring Safety & Quality Cultures - Dave Litwiller - May 2024...
Enhancing and Restoring Safety & Quality Cultures - Dave Litwiller - May 2024...Dave Litwiller
 
Chandigarh Escorts Service 📞8868886958📞 Just📲 Call Nihal Chandigarh Call Girl...
Chandigarh Escorts Service 📞8868886958📞 Just📲 Call Nihal Chandigarh Call Girl...Chandigarh Escorts Service 📞8868886958📞 Just📲 Call Nihal Chandigarh Call Girl...
Chandigarh Escorts Service 📞8868886958📞 Just📲 Call Nihal Chandigarh Call Girl...Sheetaleventcompany
 
Cracking the Cultural Competence Code.pptx
Cracking the Cultural Competence Code.pptxCracking the Cultural Competence Code.pptx
Cracking the Cultural Competence Code.pptxWorkforce Group
 
Quick Doctor In Kuwait +2773`7758`557 Kuwait Doha Qatar Dubai Abu Dhabi Sharj...
Quick Doctor In Kuwait +2773`7758`557 Kuwait Doha Qatar Dubai Abu Dhabi Sharj...Quick Doctor In Kuwait +2773`7758`557 Kuwait Doha Qatar Dubai Abu Dhabi Sharj...
Quick Doctor In Kuwait +2773`7758`557 Kuwait Doha Qatar Dubai Abu Dhabi Sharj...daisycvs
 
Call Girls Service In Old Town Dubai ((0551707352)) Old Town Dubai Call Girl ...
Call Girls Service In Old Town Dubai ((0551707352)) Old Town Dubai Call Girl ...Call Girls Service In Old Town Dubai ((0551707352)) Old Town Dubai Call Girl ...
Call Girls Service In Old Town Dubai ((0551707352)) Old Town Dubai Call Girl ...allensay1
 
Nelamangala Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Nelamangala Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...Nelamangala Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Nelamangala Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...amitlee9823
 
👉Chandigarh Call Girls 👉9878799926👉Just Call👉Chandigarh Call Girl In Chandiga...
👉Chandigarh Call Girls 👉9878799926👉Just Call👉Chandigarh Call Girl In Chandiga...👉Chandigarh Call Girls 👉9878799926👉Just Call👉Chandigarh Call Girl In Chandiga...
👉Chandigarh Call Girls 👉9878799926👉Just Call👉Chandigarh Call Girl In Chandiga...rajveerescorts2022
 
Mysore Call Girls 8617370543 WhatsApp Number 24x7 Best Services
Mysore Call Girls 8617370543 WhatsApp Number 24x7 Best ServicesMysore Call Girls 8617370543 WhatsApp Number 24x7 Best Services
Mysore Call Girls 8617370543 WhatsApp Number 24x7 Best ServicesDipal Arora
 
Call Girls In DLf Gurgaon ➥99902@11544 ( Best price)100% Genuine Escort In 24...
Call Girls In DLf Gurgaon ➥99902@11544 ( Best price)100% Genuine Escort In 24...Call Girls In DLf Gurgaon ➥99902@11544 ( Best price)100% Genuine Escort In 24...
Call Girls In DLf Gurgaon ➥99902@11544 ( Best price)100% Genuine Escort In 24...lizamodels9
 
Famous Olympic Siblings from the 21st Century
Famous Olympic Siblings from the 21st CenturyFamous Olympic Siblings from the 21st Century
Famous Olympic Siblings from the 21st Centuryrwgiffor
 
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
 
Value Proposition canvas- Customer needs and pains
Value Proposition canvas- Customer needs and painsValue Proposition canvas- Customer needs and pains
Value Proposition canvas- Customer needs and painsP&CO
 
Call Girls Hebbal Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Hebbal Just Call 👗 7737669865 👗 Top Class Call Girl Service BangaloreCall Girls Hebbal Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Hebbal Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangaloreamitlee9823
 
How to Get Started in Social Media for Art League City
How to Get Started in Social Media for Art League CityHow to Get Started in Social Media for Art League City
How to Get Started in Social Media for Art League CityEric T. Tung
 
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
 
Call Now ☎️🔝 9332606886🔝 Call Girls ❤ Service In Bhilwara Female Escorts Serv...
Call Now ☎️🔝 9332606886🔝 Call Girls ❤ Service In Bhilwara Female Escorts Serv...Call Now ☎️🔝 9332606886🔝 Call Girls ❤ Service In Bhilwara Female Escorts Serv...
Call Now ☎️🔝 9332606886🔝 Call Girls ❤ Service In Bhilwara Female Escorts Serv...Anamikakaur10
 
B.COM Unit – 4 ( CORPORATE SOCIAL RESPONSIBILITY ( CSR ).pptx
B.COM Unit – 4 ( CORPORATE SOCIAL RESPONSIBILITY ( CSR ).pptxB.COM Unit – 4 ( CORPORATE SOCIAL RESPONSIBILITY ( CSR ).pptx
B.COM Unit – 4 ( CORPORATE SOCIAL RESPONSIBILITY ( CSR ).pptxpriyanshujha201
 
FULL ENJOY Call Girls In Majnu Ka Tilla, Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Majnu Ka Tilla, Delhi Contact Us 8377877756FULL ENJOY Call Girls In Majnu Ka Tilla, Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Majnu Ka Tilla, Delhi Contact Us 8377877756dollysharma2066
 

Último (20)

Enhancing and Restoring Safety & Quality Cultures - Dave Litwiller - May 2024...
Enhancing and Restoring Safety & Quality Cultures - Dave Litwiller - May 2024...Enhancing and Restoring Safety & Quality Cultures - Dave Litwiller - May 2024...
Enhancing and Restoring Safety & Quality Cultures - Dave Litwiller - May 2024...
 
Chandigarh Escorts Service 📞8868886958📞 Just📲 Call Nihal Chandigarh Call Girl...
Chandigarh Escorts Service 📞8868886958📞 Just📲 Call Nihal Chandigarh Call Girl...Chandigarh Escorts Service 📞8868886958📞 Just📲 Call Nihal Chandigarh Call Girl...
Chandigarh Escorts Service 📞8868886958📞 Just📲 Call Nihal Chandigarh Call Girl...
 
Cracking the Cultural Competence Code.pptx
Cracking the Cultural Competence Code.pptxCracking the Cultural Competence Code.pptx
Cracking the Cultural Competence Code.pptx
 
Quick Doctor In Kuwait +2773`7758`557 Kuwait Doha Qatar Dubai Abu Dhabi Sharj...
Quick Doctor In Kuwait +2773`7758`557 Kuwait Doha Qatar Dubai Abu Dhabi Sharj...Quick Doctor In Kuwait +2773`7758`557 Kuwait Doha Qatar Dubai Abu Dhabi Sharj...
Quick Doctor In Kuwait +2773`7758`557 Kuwait Doha Qatar Dubai Abu Dhabi Sharj...
 
Call Girls Service In Old Town Dubai ((0551707352)) Old Town Dubai Call Girl ...
Call Girls Service In Old Town Dubai ((0551707352)) Old Town Dubai Call Girl ...Call Girls Service In Old Town Dubai ((0551707352)) Old Town Dubai Call Girl ...
Call Girls Service In Old Town Dubai ((0551707352)) Old Town Dubai Call Girl ...
 
Nelamangala Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Nelamangala Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...Nelamangala Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Nelamangala Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
 
👉Chandigarh Call Girls 👉9878799926👉Just Call👉Chandigarh Call Girl In Chandiga...
👉Chandigarh Call Girls 👉9878799926👉Just Call👉Chandigarh Call Girl In Chandiga...👉Chandigarh Call Girls 👉9878799926👉Just Call👉Chandigarh Call Girl In Chandiga...
👉Chandigarh Call Girls 👉9878799926👉Just Call👉Chandigarh Call Girl In Chandiga...
 
Mysore Call Girls 8617370543 WhatsApp Number 24x7 Best Services
Mysore Call Girls 8617370543 WhatsApp Number 24x7 Best ServicesMysore Call Girls 8617370543 WhatsApp Number 24x7 Best Services
Mysore Call Girls 8617370543 WhatsApp Number 24x7 Best Services
 
Call Girls In DLf Gurgaon ➥99902@11544 ( Best price)100% Genuine Escort In 24...
Call Girls In DLf Gurgaon ➥99902@11544 ( Best price)100% Genuine Escort In 24...Call Girls In DLf Gurgaon ➥99902@11544 ( Best price)100% Genuine Escort In 24...
Call Girls In DLf Gurgaon ➥99902@11544 ( Best price)100% Genuine Escort In 24...
 
Famous Olympic Siblings from the 21st Century
Famous Olympic Siblings from the 21st CenturyFamous Olympic Siblings from the 21st Century
Famous Olympic Siblings from the 21st Century
 
(Anamika) VIP Call Girls Napur Call Now 8617697112 Napur Escorts 24x7
(Anamika) VIP Call Girls Napur Call Now 8617697112 Napur Escorts 24x7(Anamika) VIP Call Girls Napur Call Now 8617697112 Napur Escorts 24x7
(Anamika) VIP Call Girls Napur Call Now 8617697112 Napur Escorts 24x7
 
Falcon Invoice Discounting platform in india
Falcon Invoice Discounting platform in indiaFalcon Invoice Discounting platform in india
Falcon Invoice Discounting platform in india
 
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...
 
Value Proposition canvas- Customer needs and pains
Value Proposition canvas- Customer needs and painsValue Proposition canvas- Customer needs and pains
Value Proposition canvas- Customer needs and pains
 
Call Girls Hebbal Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Hebbal Just Call 👗 7737669865 👗 Top Class Call Girl Service BangaloreCall Girls Hebbal Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Hebbal Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
 
How to Get Started in Social Media for Art League City
How to Get Started in Social Media for Art League CityHow to Get Started in Social Media for Art League City
How to Get Started in Social Media for Art League City
 
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
 
Call Now ☎️🔝 9332606886🔝 Call Girls ❤ Service In Bhilwara Female Escorts Serv...
Call Now ☎️🔝 9332606886🔝 Call Girls ❤ Service In Bhilwara Female Escorts Serv...Call Now ☎️🔝 9332606886🔝 Call Girls ❤ Service In Bhilwara Female Escorts Serv...
Call Now ☎️🔝 9332606886🔝 Call Girls ❤ Service In Bhilwara Female Escorts Serv...
 
B.COM Unit – 4 ( CORPORATE SOCIAL RESPONSIBILITY ( CSR ).pptx
B.COM Unit – 4 ( CORPORATE SOCIAL RESPONSIBILITY ( CSR ).pptxB.COM Unit – 4 ( CORPORATE SOCIAL RESPONSIBILITY ( CSR ).pptx
B.COM Unit – 4 ( CORPORATE SOCIAL RESPONSIBILITY ( CSR ).pptx
 
FULL ENJOY Call Girls In Majnu Ka Tilla, Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Majnu Ka Tilla, Delhi Contact Us 8377877756FULL ENJOY Call Girls In Majnu Ka Tilla, Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Majnu Ka Tilla, Delhi Contact Us 8377877756
 

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