SlideShare ist ein Scribd-Unternehmen logo
1 von 73
Downloaden Sie, um offline zu lesen
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

Weitere ähnliche Inhalte

Was ist angesagt?

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
 

Was ist angesagt? (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
 

Andere mochten auch

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
 

Andere mochten auch (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
 

Mehr von 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
 

Mehr von 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
 

Kürzlich hochgeladen

trending-flavors-and-ingredients-in-salty-snacks-us-2024_Redacted-V2.pdf
trending-flavors-and-ingredients-in-salty-snacks-us-2024_Redacted-V2.pdftrending-flavors-and-ingredients-in-salty-snacks-us-2024_Redacted-V2.pdf
trending-flavors-and-ingredients-in-salty-snacks-us-2024_Redacted-V2.pdfMintel Group
 
EUDR Info Meeting Ethiopian coffee exporters
EUDR Info Meeting Ethiopian coffee exportersEUDR Info Meeting Ethiopian coffee exporters
EUDR Info Meeting Ethiopian coffee exportersPeter Horsten
 
Horngren’s Financial & Managerial Accounting, 7th edition by Miller-Nobles so...
Horngren’s Financial & Managerial Accounting, 7th edition by Miller-Nobles so...Horngren’s Financial & Managerial Accounting, 7th edition by Miller-Nobles so...
Horngren’s Financial & Managerial Accounting, 7th edition by Miller-Nobles so...ssuserf63bd7
 
Supercharge Your eCommerce Stores-acowebs
Supercharge Your eCommerce Stores-acowebsSupercharge Your eCommerce Stores-acowebs
Supercharge Your eCommerce Stores-acowebsGOKUL JS
 
TriStar Gold Corporate Presentation - April 2024
TriStar Gold Corporate Presentation - April 2024TriStar Gold Corporate Presentation - April 2024
TriStar Gold Corporate Presentation - April 2024Adnet Communications
 
Church Building Grants To Assist With New Construction, Additions, And Restor...
Church Building Grants To Assist With New Construction, Additions, And Restor...Church Building Grants To Assist With New Construction, Additions, And Restor...
Church Building Grants To Assist With New Construction, Additions, And Restor...Americas Got Grants
 
PSCC - Capability Statement Presentation
PSCC - Capability Statement PresentationPSCC - Capability Statement Presentation
PSCC - Capability Statement PresentationAnamaria Contreras
 
20200128 Ethical by Design - Whitepaper.pdf
20200128 Ethical by Design - Whitepaper.pdf20200128 Ethical by Design - Whitepaper.pdf
20200128 Ethical by Design - Whitepaper.pdfChris Skinner
 
Effective Strategies for Maximizing Your Profit When Selling Gold Jewelry
Effective Strategies for Maximizing Your Profit When Selling Gold JewelryEffective Strategies for Maximizing Your Profit When Selling Gold Jewelry
Effective Strategies for Maximizing Your Profit When Selling Gold JewelryWhittensFineJewelry1
 
Memorándum de Entendimiento (MoU) entre Codelco y SQM
Memorándum de Entendimiento (MoU) entre Codelco y SQMMemorándum de Entendimiento (MoU) entre Codelco y SQM
Memorándum de Entendimiento (MoU) entre Codelco y SQMVoces Mineras
 
business environment micro environment macro environment.pptx
business environment micro environment macro environment.pptxbusiness environment micro environment macro environment.pptx
business environment micro environment macro environment.pptxShruti Mittal
 
1911 Gold Corporate Presentation Apr 2024.pdf
1911 Gold Corporate Presentation Apr 2024.pdf1911 Gold Corporate Presentation Apr 2024.pdf
1911 Gold Corporate Presentation Apr 2024.pdfShaun Heinrichs
 
WSMM Technology February.March Newsletter_vF.pdf
WSMM Technology February.March Newsletter_vF.pdfWSMM Technology February.March Newsletter_vF.pdf
WSMM Technology February.March Newsletter_vF.pdfJamesConcepcion7
 
digital marketing , introduction of digital marketing
digital marketing , introduction of digital marketingdigital marketing , introduction of digital marketing
digital marketing , introduction of digital marketingrajputmeenakshi733
 
Jewish Resources in the Family Resource Centre
Jewish Resources in the Family Resource CentreJewish Resources in the Family Resource Centre
Jewish Resources in the Family Resource CentreNZSG
 
Go for Rakhi Bazaar and Pick the Latest Bhaiya Bhabhi Rakhi.pptx
Go for Rakhi Bazaar and Pick the Latest Bhaiya Bhabhi Rakhi.pptxGo for Rakhi Bazaar and Pick the Latest Bhaiya Bhabhi Rakhi.pptx
Go for Rakhi Bazaar and Pick the Latest Bhaiya Bhabhi Rakhi.pptxRakhi Bazaar
 
Send Files | Sendbig.comSend Files | Sendbig.com
Send Files | Sendbig.comSend Files | Sendbig.comSend Files | Sendbig.comSend Files | Sendbig.com
Send Files | Sendbig.comSend Files | Sendbig.comSendBig4
 
Lucia Ferretti, Lead Business Designer; Matteo Meschini, Business Designer @T...
Lucia Ferretti, Lead Business Designer; Matteo Meschini, Business Designer @T...Lucia Ferretti, Lead Business Designer; Matteo Meschini, Business Designer @T...
Lucia Ferretti, Lead Business Designer; Matteo Meschini, Business Designer @T...Associazione Digital Days
 
Unveiling the Soundscape Music for Psychedelic Experiences
Unveiling the Soundscape Music for Psychedelic ExperiencesUnveiling the Soundscape Music for Psychedelic Experiences
Unveiling the Soundscape Music for Psychedelic ExperiencesDoe Paoro
 

Kürzlich hochgeladen (20)

trending-flavors-and-ingredients-in-salty-snacks-us-2024_Redacted-V2.pdf
trending-flavors-and-ingredients-in-salty-snacks-us-2024_Redacted-V2.pdftrending-flavors-and-ingredients-in-salty-snacks-us-2024_Redacted-V2.pdf
trending-flavors-and-ingredients-in-salty-snacks-us-2024_Redacted-V2.pdf
 
EUDR Info Meeting Ethiopian coffee exporters
EUDR Info Meeting Ethiopian coffee exportersEUDR Info Meeting Ethiopian coffee exporters
EUDR Info Meeting Ethiopian coffee exporters
 
Horngren’s Financial & Managerial Accounting, 7th edition by Miller-Nobles so...
Horngren’s Financial & Managerial Accounting, 7th edition by Miller-Nobles so...Horngren’s Financial & Managerial Accounting, 7th edition by Miller-Nobles so...
Horngren’s Financial & Managerial Accounting, 7th edition by Miller-Nobles so...
 
Supercharge Your eCommerce Stores-acowebs
Supercharge Your eCommerce Stores-acowebsSupercharge Your eCommerce Stores-acowebs
Supercharge Your eCommerce Stores-acowebs
 
TriStar Gold Corporate Presentation - April 2024
TriStar Gold Corporate Presentation - April 2024TriStar Gold Corporate Presentation - April 2024
TriStar Gold Corporate Presentation - April 2024
 
Church Building Grants To Assist With New Construction, Additions, And Restor...
Church Building Grants To Assist With New Construction, Additions, And Restor...Church Building Grants To Assist With New Construction, Additions, And Restor...
Church Building Grants To Assist With New Construction, Additions, And Restor...
 
PSCC - Capability Statement Presentation
PSCC - Capability Statement PresentationPSCC - Capability Statement Presentation
PSCC - Capability Statement Presentation
 
20200128 Ethical by Design - Whitepaper.pdf
20200128 Ethical by Design - Whitepaper.pdf20200128 Ethical by Design - Whitepaper.pdf
20200128 Ethical by Design - Whitepaper.pdf
 
Effective Strategies for Maximizing Your Profit When Selling Gold Jewelry
Effective Strategies for Maximizing Your Profit When Selling Gold JewelryEffective Strategies for Maximizing Your Profit When Selling Gold Jewelry
Effective Strategies for Maximizing Your Profit When Selling Gold Jewelry
 
Memorándum de Entendimiento (MoU) entre Codelco y SQM
Memorándum de Entendimiento (MoU) entre Codelco y SQMMemorándum de Entendimiento (MoU) entre Codelco y SQM
Memorándum de Entendimiento (MoU) entre Codelco y SQM
 
business environment micro environment macro environment.pptx
business environment micro environment macro environment.pptxbusiness environment micro environment macro environment.pptx
business environment micro environment macro environment.pptx
 
1911 Gold Corporate Presentation Apr 2024.pdf
1911 Gold Corporate Presentation Apr 2024.pdf1911 Gold Corporate Presentation Apr 2024.pdf
1911 Gold Corporate Presentation Apr 2024.pdf
 
WSMM Technology February.March Newsletter_vF.pdf
WSMM Technology February.March Newsletter_vF.pdfWSMM Technology February.March Newsletter_vF.pdf
WSMM Technology February.March Newsletter_vF.pdf
 
digital marketing , introduction of digital marketing
digital marketing , introduction of digital marketingdigital marketing , introduction of digital marketing
digital marketing , introduction of digital marketing
 
Jewish Resources in the Family Resource Centre
Jewish Resources in the Family Resource CentreJewish Resources in the Family Resource Centre
Jewish Resources in the Family Resource Centre
 
Go for Rakhi Bazaar and Pick the Latest Bhaiya Bhabhi Rakhi.pptx
Go for Rakhi Bazaar and Pick the Latest Bhaiya Bhabhi Rakhi.pptxGo for Rakhi Bazaar and Pick the Latest Bhaiya Bhabhi Rakhi.pptx
Go for Rakhi Bazaar and Pick the Latest Bhaiya Bhabhi Rakhi.pptx
 
WAM Corporate Presentation April 12 2024.pdf
WAM Corporate Presentation April 12 2024.pdfWAM Corporate Presentation April 12 2024.pdf
WAM Corporate Presentation April 12 2024.pdf
 
Send Files | Sendbig.comSend Files | Sendbig.com
Send Files | Sendbig.comSend Files | Sendbig.comSend Files | Sendbig.comSend Files | Sendbig.com
Send Files | Sendbig.comSend Files | Sendbig.com
 
Lucia Ferretti, Lead Business Designer; Matteo Meschini, Business Designer @T...
Lucia Ferretti, Lead Business Designer; Matteo Meschini, Business Designer @T...Lucia Ferretti, Lead Business Designer; Matteo Meschini, Business Designer @T...
Lucia Ferretti, Lead Business Designer; Matteo Meschini, Business Designer @T...
 
Unveiling the Soundscape Music for Psychedelic Experiences
Unveiling the Soundscape Music for Psychedelic ExperiencesUnveiling the Soundscape Music for Psychedelic Experiences
Unveiling the Soundscape Music for Psychedelic Experiences
 

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