Refactoring to Macros with Clojure
Upcoming SlideShare
Loading in...5
×
 

Refactoring to Macros with Clojure

on

  • 3,077 views

 

Statistics

Views

Total Views
3,077
Views on SlideShare
710
Embed Views
2,367

Actions

Likes
0
Downloads
4
Comments
0

6 Embeds 2,367

http://jug.lv 2360
http://translate.googleusercontent.com 2
http://assets.txmblr.com 2
http://www.bing.com 1
http://webcache.googleusercontent.com 1
https://www.google.lv 1

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Refactoring to Macros with Clojure Refactoring to Macros with Clojure Presentation Transcript

  • Refactoring to Macroswith ClojureDimitry Solovyov@dimituri
  • Lets talk aboutFUNCTIONAL PROGRAMMING
  • liftIO  $  atomicModifyIORef  sc  $  n  -­‐>  (n  +  1,  ())d  <-­‐  f  v  >>:  onInnerliftIO  $  ds  `addDisposable`  d
  • >>:
  • Lets talk aboutLISP
  • LISP in 3 minutesvia @bodiltv
  • object.method(a, b);
  • object.method( a b)
  • ClojureIts a LispCompiledDynamic typesType hintsMacros
  • ClojureIts a LispCompiledDynamic typesType hintsMacrosOptional static typesOptional Prolog
  • http://clojure.org/java_interop
  • HttpServer  server  =  HttpServer.create(address,  0);server.createContext(path,  handler);server.setExecutor(null);server.start();
  • HttpServer  server  =  HttpServer.create(address,  0);server.createContext(path,  handler);server.setExecutor(null);server.start();(doto  (HttpServer/create  address  0)    (.createContext  path  handler)    (.setExecutor  nil)    (.start))
  • macro |ˈmakrəәʊ|noun ( pl. macros )1 (also macro instruction) Computing a single instructionthat expands automatically into a set of instructionsto perform a particular task.
  • (let*  [G__360  (HttpServer/create  address  0)]    (.createContext  G__360  path  handler)    (.setExecutor  G__360  nil)    (.start  G__360)    G__360)(doto  (HttpServer/create  address  0)    (.createContext  path  handler)    (.setExecutor  nil)    (.start))
  • 根性
  • .println(Encoding.encodeBase64(Encoding.decodeUrl(Encoding.decodeBas
  • String  url  =  Encoding.decodeBase64(b);String  decodedUrl  =  Encoding.decodeUrl(url);String  encodedUrl  =  Encoding.encodeBase64(decodedUrl);System.out.println(encodedUrl);
  • String  url  =  Encoding.decodeBase64(b);String  decodedUrl  =  Encoding.decodeUrl(url);String  encodedUrl  =  Encoding.encodeBase64(decodedUrl);System.out.println(encodedUrl);(-­‐>  b    Encoding/decodeBase64    Encoding/decodeUrl    Encoding/encodeBase64    println)
  • -­‐>(-­‐>  stuff    (foo  ,,,  a)    (bar  ,,,  b  c  d)    (baz  ,,,  e  f))
  • -­‐>>-­‐>(-­‐>  stuff    (foo  ,,,  a)    (bar  ,,,  b  c  d)    (baz  ,,,  e  f))(-­‐>>  stuff    (foo  a  ,,,)    (bar  b  c  d  ,,,)    (baz  e  f  ,,,))
  • -­‐<>-­‐>>-­‐>(-­‐>  stuff    (foo  ,,,  a)    (bar  ,,,  b  c  d)    (baz  ,,,  e  f))(-­‐>>  stuff    (foo  a  ,,,)    (bar  b  c  d  ,,,)    (baz  e  f  ,,,))(-­‐<>  stuff    (foo  a  <>)    (bar  b  c  <>  d)    (baz  <>  e  f))
  • “Swiss Arrows”-­‐<>> -­‐?<> -­‐!> -­‐!>>-­‐!<> <<-­‐ -­‐< -­‐<:p-­‐<< -­‐<<:p -­‐<>< -­‐<><:phttps://github.com/rplevy/swiss-arrows
  • -­‐<<:p
  • -­‐<<:p
  • Q: How do I get the result from achain of computatoins that may fail?
  • PhoneNumber  number  =  phoneMap.get(person);if  (number  !=  null)  {        Carrier  carrier  =  carrierMap.get(number);        if  (carrier  !=  null)  {                return  addressMap.get(address);        }}return  null;
  • WHAT YOURE LOOKING FORIS AN OPTION MONAD
  • PhoneNumber  number  =  phoneMap.get(person);if  (number  !=  null)  {        Carrier  carrier  =  carrierMap.get(number);        if  (carrier  !=  null)  {                return  addressMap.get(address);        }}return  null;(some-­‐>>    (.get  phoneMap  person)    (.get  carrierMap)    (.get  addressMap))
  • Clojure vectors implementjava.lang.Comparablejava.util.RandomAccess
  • All Clojure functions implementjava.util.Comparatorjava.lang.Runnablejava.util.concurrent.Callable
  • List<Integer>  numbers  =  Arrays.asList(1,  3,  4,  8,  2);Collections.sort(numbers,  new  Comparator<Integer>()  {        @Override        public  int  compare(Integer  o1,  Integer  o2)  {                return  o2.compareTo(o1);        }});
  • List<Integer>  numbers  =  Arrays.asList(1,  3,  4,  8,  2);Collections.sort(numbers,  new  Comparator<Integer>()  {        @Override        public  int  compare(Integer  o1,  Integer  o2)  {                return  o2.compareTo(o1);        }});List<Integer>  numbers  =  Arrays.asList(1,  3,  4,  8,  2);Collections.sort(numbers,  (o1,  o2)  -­‐>  o2.compareTo(o1));
  • List<Integer>  numbers  =  Arrays.asList(1,  3,  4,  8,  2);Collections.sort(numbers,  new  Comparator<Integer>()  {        @Override        public  int  compare(Integer  o1,  Integer  o2)  {                return  o2.compareTo(o1);        }});(doto  (ArrayList.  [1  3  4  8  2])    (Collections/sort  (fn  [o1  o2]  (.compareTo  o2  o1))))List<Integer>  numbers  =  Arrays.asList(1,  3,  4,  8,  2);Collections.sort(numbers,  (o1,  o2)  -­‐>  o2.compareTo(o1));
  • List<Integer>  numbers  =  Arrays.asList(1,  3,  4,  8,  2);Collections.sort(numbers,  new  Comparator<Integer>()  {        @Override        public  int  compare(Integer  o1,  Integer  o2)  {                return  o2.compareTo(o1);        }});(doto  (ArrayList.  [1  3  4  8  2])    (Collections/sort  (fn  [o1  o2]  (.compareTo  o2  o1))))(sort  #(compare  %2  %1)  [1  3  4  8  2])List<Integer>  numbers  =  Arrays.asList(1,  3,  4,  8,  2);Collections.sort(numbers,  (o1,  o2)  -­‐>  o2.compareTo(o1));
  • (defn  len-­‐reflected  [s]    (.length  s))(defn  len-­‐hinted  ^String  [^String  s]    (.length  s))user=>  (time  (dotimes  [i  1000000]  (len-­‐reflected  (str  i))))"Elapsed  time:  3204.913  msecs"niluser=>  (time  (dotimes  [i  1000000]  (len-­‐hinted  (str  i))))"Elapsed  time:  113.317  msecs"nil
  • Polyglot projectswith Leiningen(defproject  clojure-­‐java  "1.0.0-­‐SNAPSHOT"    :description  "Example  Clojure+Java  project."    :min-­‐lein-­‐version  "2.0.0"    :source-­‐paths  ["src/clojure"]    :java-­‐source-­‐paths  ["src/java"]    :javac-­‐options  ["-­‐target"  "1.5"  "-­‐source"  "1.5"])http://leiningen.org
  • http://www.ldn.lvWorkshop: ClojureSaturday, May 18, 201310:00 amCitadeles iela 12, Rigahttp://riga.techhub.com/
  • (kthx-­‐bye))))))
  • (defmacro  doto    [x  &  forms]    (let  [gx  (gensym)]        `(let  [~gx  ~x]              ~@(map  (fn  [f]                                (if  (seq?  f)                                    `(~(first  f)  ~gx  ~@(next  f))                                    `(~f  ~gx)))                            forms)              ~gx)))
  • (ns  foo.core    (:gen-­‐class))  (defn  -­‐main    "I  dont  do  a  whole  lot  ...  yet."    [&  args]    (println  "Im  a  little  pony"))foo$  lein  runIm  a  little  pony
  • (defn  make-­‐example  []    (proxy  [Object]  []        (toString  []  "Im  a  little  pony")))user=>  (.toString  (make-­‐some-­‐example))"Im  a  little  pony"
  • class  Example  {        void  someMethod(String  x)  {                someMethod(x,  null)        }          void  someMethod(String  x,  String  y)  {                doSomethingWith(x,  y);        }}(proxy  [Example]  []    (toString        ([x]      (proxy-­‐super  someMethod  x))        ([x  y]  (do-­‐other-­‐stuff  this  x  y))))
  • (let  [^LoadingCache  cache  (doto  CacheBuilder/newBuilder                            (.maximumSize  1000)                            (.build  (reify  CacheLoader                                                (load  ^Graph  [^Key  key]  (create-­‐expensive-­‐graph  key)))))])LoadingCache<Key,  Graph>  cache  =  CacheBuilder.newBuilder()                .maximumSize(1000)                .build(new  CacheLoader<Key,  Graph>()  {                              public  Graph  load(Key  key)  {                                      return  createExpensiveGraph(key);                              }                      });LoadingCache<Key,  Graph>  cache  =  CacheBuilder.newBuilder()                .maximumSize(1000)                .build((key)  -­‐>  createExpensiveGraph(key));