SlideShare a Scribd company logo
1 of 51
Download to read offline
Refactoring to Macros
with Clojure
Dimitry Solovyov
@dimituri
Let's talk about
FUNCTIONAL PROGRAMMING
liftIO	
  $	
  atomicModifyIORef	
  sc	
  $	
  n	
  -­‐>	
  (n	
  +	
  1,	
  ())
d	
  <-­‐	
  f	
  v	
  >>:	
  onInner
liftIO	
  $	
  ds	
  `addDisposable`	
  d
>>:
Let's talk about
LISP
LISP in 3 minutes
via @bodiltv
object.method(a, b);
object.method( a b)
Clojure





It's a Lisp
Compiled
Dynamic types
Type hints
Macros
Clojure







It's a Lisp
Compiled
Dynamic types
Type hints
Macros
Optional static types
Optional 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 instruction
that expands automatically into a set of instructions
to 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 -­‐<>< -­‐<><:p
https://github.com/rplevy/swiss-arrows
-­‐<<:p
-­‐<<:p
Q: How do I get the result from a
chain 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 YOU'RE LOOKING FOR
IS 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 implement
java.lang.Comparable
java.util.RandomAccess
All Clojure functions implement
java.util.Comparator
java.lang.Runnable
java.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"
nil
user=>	
  (time	
  (dotimes	
  [i	
  1000000]	
  (len-­‐hinted	
  (str	
  i))))
"Elapsed	
  time:	
  113.317	
  msecs"
nil
Polyglot projects
with 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.lv
Workshop: Clojure
Saturday, May 18, 2013
10:00 am
Citadeles iela 12, Riga
http://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	
  don't	
  do	
  a	
  whole	
  lot	
  ...	
  yet."
	
  	
  [&	
  args]
	
  	
  (println	
  "I'm	
  a	
  little	
  pony"))
foo$	
  lein	
  run
I'm	
  a	
  little	
  pony
(defn	
  make-­‐example	
  []
	
  	
  (proxy	
  [Object]	
  []
	
  	
  	
  	
  (toString	
  []	
  "I'm	
  a	
  little	
  pony")))
user=>	
  (.toString	
  (make-­‐some-­‐example))
"I'm	
  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));

More Related Content

What's hot

Mozilla とブラウザゲーム
Mozilla とブラウザゲームMozilla とブラウザゲーム
Mozilla とブラウザゲームNoritada Shimizu
 
The Ring programming language version 1.5.4 book - Part 40 of 185
The Ring programming language version 1.5.4 book - Part 40 of 185The Ring programming language version 1.5.4 book - Part 40 of 185
The Ring programming language version 1.5.4 book - Part 40 of 185Mahmoud Samir Fayed
 
Tuga it 2016 - What's New In C# 6
Tuga it 2016 - What's New In C# 6Tuga it 2016 - What's New In C# 6
Tuga it 2016 - What's New In C# 6Paulo Morgado
 
6. Generics. Collections. Streams
6. Generics. Collections. Streams6. Generics. Collections. Streams
6. Generics. Collections. StreamsDEVTYPE
 
Groovy ネタ NGK 忘年会2009 ライトニングトーク
Groovy ネタ NGK 忘年会2009 ライトニングトークGroovy ネタ NGK 忘年会2009 ライトニングトーク
Groovy ネタ NGK 忘年会2009 ライトニングトークTsuyoshi Yamamoto
 
ITT 2015 - Saul Mora - Object Oriented Function Programming
ITT 2015 - Saul Mora - Object Oriented Function ProgrammingITT 2015 - Saul Mora - Object Oriented Function Programming
ITT 2015 - Saul Mora - Object Oriented Function ProgrammingIstanbul Tech Talks
 
Python postgre sql a wonderful wedding
Python postgre sql   a wonderful weddingPython postgre sql   a wonderful wedding
Python postgre sql a wonderful weddingStéphane Wirtel
 
What's new in C# 6 - NetPonto Porto 20160116
What's new in C# 6  - NetPonto Porto 20160116What's new in C# 6  - NetPonto Porto 20160116
What's new in C# 6 - NetPonto Porto 20160116Paulo Morgado
 
Python Performance 101
Python Performance 101Python Performance 101
Python Performance 101Ankur Gupta
 
"PostgreSQL and Python" Lightning Talk @EuroPython2014
"PostgreSQL and Python" Lightning Talk @EuroPython2014"PostgreSQL and Python" Lightning Talk @EuroPython2014
"PostgreSQL and Python" Lightning Talk @EuroPython2014Henning Jacobs
 
Template Haskell とか
Template Haskell とかTemplate Haskell とか
Template Haskell とかHiromi Ishii
 
Haskell in the Real World
Haskell in the Real WorldHaskell in the Real World
Haskell in the Real Worldosfameron
 

What's hot (18)

Mozilla とブラウザゲーム
Mozilla とブラウザゲームMozilla とブラウザゲーム
Mozilla とブラウザゲーム
 
The Ring programming language version 1.5.4 book - Part 40 of 185
The Ring programming language version 1.5.4 book - Part 40 of 185The Ring programming language version 1.5.4 book - Part 40 of 185
The Ring programming language version 1.5.4 book - Part 40 of 185
 
Tuga it 2016 - What's New In C# 6
Tuga it 2016 - What's New In C# 6Tuga it 2016 - What's New In C# 6
Tuga it 2016 - What's New In C# 6
 
6. Generics. Collections. Streams
6. Generics. Collections. Streams6. Generics. Collections. Streams
6. Generics. Collections. Streams
 
Groovy ネタ NGK 忘年会2009 ライトニングトーク
Groovy ネタ NGK 忘年会2009 ライトニングトークGroovy ネタ NGK 忘年会2009 ライトニングトーク
Groovy ネタ NGK 忘年会2009 ライトニングトーク
 
ITT 2015 - Saul Mora - Object Oriented Function Programming
ITT 2015 - Saul Mora - Object Oriented Function ProgrammingITT 2015 - Saul Mora - Object Oriented Function Programming
ITT 2015 - Saul Mora - Object Oriented Function Programming
 
Python postgre sql a wonderful wedding
Python postgre sql   a wonderful weddingPython postgre sql   a wonderful wedding
Python postgre sql a wonderful wedding
 
What's New In C# 7
What's New In C# 7What's New In C# 7
What's New In C# 7
 
What's new in C# 6 - NetPonto Porto 20160116
What's new in C# 6  - NetPonto Porto 20160116What's new in C# 6  - NetPonto Porto 20160116
What's new in C# 6 - NetPonto Porto 20160116
 
Collection Core Concept
Collection Core ConceptCollection Core Concept
Collection Core Concept
 
Hammurabi
HammurabiHammurabi
Hammurabi
 
Comparing JVM languages
Comparing JVM languagesComparing JVM languages
Comparing JVM languages
 
Python Performance 101
Python Performance 101Python Performance 101
Python Performance 101
 
"PostgreSQL and Python" Lightning Talk @EuroPython2014
"PostgreSQL and Python" Lightning Talk @EuroPython2014"PostgreSQL and Python" Lightning Talk @EuroPython2014
"PostgreSQL and Python" Lightning Talk @EuroPython2014
 
Template Haskell とか
Template Haskell とかTemplate Haskell とか
Template Haskell とか
 
Haskell in the Real World
Haskell in the Real WorldHaskell in the Real World
Haskell in the Real World
 
2014-11-01 01 Денис Нелюбин. О сортах кофе
2014-11-01 01 Денис Нелюбин. О сортах кофе2014-11-01 01 Денис Нелюбин. О сортах кофе
2014-11-01 01 Денис Нелюбин. О сортах кофе
 
EcmaScript 6
EcmaScript 6 EcmaScript 6
EcmaScript 6
 

Viewers also liked

Class Hour - Arturs Sakalis - Odnoklassniki
Class Hour - Arturs Sakalis - OdnoklassnikiClass Hour - Arturs Sakalis - Odnoklassniki
Class Hour - Arturs Sakalis - OdnoklassnikiSociality Rocks!
 
Rubylight Pattern-Matching Solutions
Rubylight Pattern-Matching SolutionsRubylight Pattern-Matching Solutions
Rubylight Pattern-Matching SolutionsDmitry Buzdin
 
Rubylight programming contest
Rubylight programming contestRubylight programming contest
Rubylight programming contestDmitry Buzdin
 
Boletim de ocupação hoteleira - BOH, EMBRATUR
Boletim de ocupação hoteleira - BOH, EMBRATURBoletim de ocupação hoteleira - BOH, EMBRATUR
Boletim de ocupação hoteleira - BOH, EMBRATUREcoHospedagem
 
Rubylight JUG Contest Results Part II
Rubylight JUG Contest Results Part IIRubylight JUG Contest Results Part II
Rubylight JUG Contest Results Part IIDmitry Buzdin
 
Riding Redis @ask.fm
Riding Redis @ask.fmRiding Redis @ask.fm
Riding Redis @ask.fmDmitry Buzdin
 
Архитектура Ленты на Одноклассниках
Архитектура Ленты на ОдноклассникахАрхитектура Ленты на Одноклассниках
Архитектура Ленты на ОдноклассникахDmitry Buzdin
 
Getting Started with your User Pools in Amazon Cognito - AWS June 2016 Webina...
Getting Started with your User Pools in Amazon Cognito - AWS June 2016 Webina...Getting Started with your User Pools in Amazon Cognito - AWS June 2016 Webina...
Getting Started with your User Pools in Amazon Cognito - AWS June 2016 Webina...Amazon Web Services
 
NATURAL HISTORY OF DISEASE
NATURAL HISTORY OF DISEASENATURAL HISTORY OF DISEASE
NATURAL HISTORY OF DISEASESoumya Sahoo
 

Viewers also liked (9)

Class Hour - Arturs Sakalis - Odnoklassniki
Class Hour - Arturs Sakalis - OdnoklassnikiClass Hour - Arturs Sakalis - Odnoklassniki
Class Hour - Arturs Sakalis - Odnoklassniki
 
Rubylight Pattern-Matching Solutions
Rubylight Pattern-Matching SolutionsRubylight Pattern-Matching Solutions
Rubylight Pattern-Matching Solutions
 
Rubylight programming contest
Rubylight programming contestRubylight programming contest
Rubylight programming contest
 
Boletim de ocupação hoteleira - BOH, EMBRATUR
Boletim de ocupação hoteleira - BOH, EMBRATURBoletim de ocupação hoteleira - BOH, EMBRATUR
Boletim de ocupação hoteleira - BOH, EMBRATUR
 
Rubylight JUG Contest Results Part II
Rubylight JUG Contest Results Part IIRubylight JUG Contest Results Part II
Rubylight JUG Contest Results Part II
 
Riding Redis @ask.fm
Riding Redis @ask.fmRiding Redis @ask.fm
Riding Redis @ask.fm
 
Архитектура Ленты на Одноклассниках
Архитектура Ленты на ОдноклассникахАрхитектура Ленты на Одноклассниках
Архитектура Ленты на Одноклассниках
 
Getting Started with your User Pools in Amazon Cognito - AWS June 2016 Webina...
Getting Started with your User Pools in Amazon Cognito - AWS June 2016 Webina...Getting Started with your User Pools in Amazon Cognito - AWS June 2016 Webina...
Getting Started with your User Pools in Amazon Cognito - AWS June 2016 Webina...
 
NATURAL HISTORY OF DISEASE
NATURAL HISTORY OF DISEASENATURAL HISTORY OF DISEASE
NATURAL HISTORY OF DISEASE
 

Similar to Refactoring to Macros with Clojure

Wprowadzenie do technologii Big Data / Intro to Big Data Ecosystem
Wprowadzenie do technologii Big Data / Intro to Big Data EcosystemWprowadzenie do technologii Big Data / Intro to Big Data Ecosystem
Wprowadzenie do technologii Big Data / Intro to Big Data EcosystemSages
 
Scala @ TechMeetup Edinburgh
Scala @ TechMeetup EdinburghScala @ TechMeetup Edinburgh
Scala @ TechMeetup EdinburghStuart Roebuck
 
Introduction to Scalding and Monoids
Introduction to Scalding and MonoidsIntroduction to Scalding and Monoids
Introduction to Scalding and MonoidsHugo Gävert
 
mobl presentation @ IHomer
mobl presentation @ IHomermobl presentation @ IHomer
mobl presentation @ IHomerzefhemel
 
Python 101 language features and functional programming
Python 101 language features and functional programmingPython 101 language features and functional programming
Python 101 language features and functional programmingLukasz Dynowski
 
Map/reduce, geospatial indexing, and other cool features (Kristina Chodorow)
Map/reduce, geospatial indexing, and other cool features (Kristina Chodorow)Map/reduce, geospatial indexing, and other cool features (Kristina Chodorow)
Map/reduce, geospatial indexing, and other cool features (Kristina Chodorow)MongoSF
 
Modern technologies in data science
Modern technologies in data science Modern technologies in data science
Modern technologies in data science Chucheng Hsieh
 
T3chFest 2016 - The polyglot programmer
T3chFest 2016 - The polyglot programmerT3chFest 2016 - The polyglot programmer
T3chFest 2016 - The polyglot programmerDavid Muñoz Díaz
 
mobl - model-driven engineering lecture
mobl - model-driven engineering lecturemobl - model-driven engineering lecture
mobl - model-driven engineering lecturezefhemel
 

Similar to Refactoring to Macros with Clojure (20)

Groovy
GroovyGroovy
Groovy
 
Pune Clojure Course Outline
Pune Clojure Course OutlinePune Clojure Course Outline
Pune Clojure Course Outline
 
Introduction to Groovy
Introduction to GroovyIntroduction to Groovy
Introduction to Groovy
 
Cpp tutorial
Cpp tutorialCpp tutorial
Cpp tutorial
 
Wprowadzenie do technologii Big Data / Intro to Big Data Ecosystem
Wprowadzenie do technologii Big Data / Intro to Big Data EcosystemWprowadzenie do technologii Big Data / Intro to Big Data Ecosystem
Wprowadzenie do technologii Big Data / Intro to Big Data Ecosystem
 
Hw09 Hadoop + Clojure
Hw09   Hadoop + ClojureHw09   Hadoop + Clojure
Hw09 Hadoop + Clojure
 
Hadoop + Clojure
Hadoop + ClojureHadoop + Clojure
Hadoop + Clojure
 
Spark workshop
Spark workshopSpark workshop
Spark workshop
 
Scala @ TechMeetup Edinburgh
Scala @ TechMeetup EdinburghScala @ TechMeetup Edinburgh
Scala @ TechMeetup Edinburgh
 
Introduction to Scalding and Monoids
Introduction to Scalding and MonoidsIntroduction to Scalding and Monoids
Introduction to Scalding and Monoids
 
mobl presentation @ IHomer
mobl presentation @ IHomermobl presentation @ IHomer
mobl presentation @ IHomer
 
Python 101 language features and functional programming
Python 101 language features and functional programmingPython 101 language features and functional programming
Python 101 language features and functional programming
 
Spark_Documentation_Template1
Spark_Documentation_Template1Spark_Documentation_Template1
Spark_Documentation_Template1
 
Map/reduce, geospatial indexing, and other cool features (Kristina Chodorow)
Map/reduce, geospatial indexing, and other cool features (Kristina Chodorow)Map/reduce, geospatial indexing, and other cool features (Kristina Chodorow)
Map/reduce, geospatial indexing, and other cool features (Kristina Chodorow)
 
Modern technologies in data science
Modern technologies in data science Modern technologies in data science
Modern technologies in data science
 
CppTutorial.ppt
CppTutorial.pptCppTutorial.ppt
CppTutorial.ppt
 
T3chFest 2016 - The polyglot programmer
T3chFest 2016 - The polyglot programmerT3chFest 2016 - The polyglot programmer
T3chFest 2016 - The polyglot programmer
 
mobl - model-driven engineering lecture
mobl - model-driven engineering lecturemobl - model-driven engineering lecture
mobl - model-driven engineering lecture
 
A Shiny Example-- R
A Shiny Example-- RA Shiny Example-- R
A Shiny Example-- R
 
Scala in Places API
Scala in Places APIScala in Places API
Scala in Places API
 

More from Dmitry Buzdin

How Payment Cards Really Work?
How Payment Cards Really Work?How Payment Cards Really Work?
How Payment Cards Really Work?Dmitry Buzdin
 
Как построить свой фреймворк для автотестов?
Как построить свой фреймворк для автотестов?Как построить свой фреймворк для автотестов?
Как построить свой фреймворк для автотестов?Dmitry Buzdin
 
How to grow your own Microservice?
How to grow your own Microservice?How to grow your own Microservice?
How to grow your own Microservice?Dmitry Buzdin
 
How to Build Your Own Test Automation Framework?
How to Build Your Own Test Automation Framework?How to Build Your Own Test Automation Framework?
How to Build Your Own Test Automation Framework?Dmitry Buzdin
 
Delivery Pipeline for Windows Machines
Delivery Pipeline for Windows MachinesDelivery Pipeline for Windows Machines
Delivery Pipeline for Windows MachinesDmitry Buzdin
 
Big Data Processing Using Hadoop Infrastructure
Big Data Processing Using Hadoop InfrastructureBig Data Processing Using Hadoop Infrastructure
Big Data Processing Using Hadoop InfrastructureDmitry Buzdin
 
Developing Useful APIs
Developing Useful APIsDeveloping Useful APIs
Developing Useful APIsDmitry Buzdin
 
Continuous Delivery
Continuous Delivery Continuous Delivery
Continuous Delivery Dmitry Buzdin
 
Introduction to DevOps
Introduction to DevOpsIntroduction to DevOps
Introduction to DevOpsDmitry Buzdin
 
Thread Dump Analysis
Thread Dump AnalysisThread Dump Analysis
Thread Dump AnalysisDmitry Buzdin
 
Pragmatic Java Test Automation
Pragmatic Java Test AutomationPragmatic Java Test Automation
Pragmatic Java Test AutomationDmitry Buzdin
 
Web polyglot programming
Web polyglot programmingWeb polyglot programming
Web polyglot programmingDmitry Buzdin
 
Code Structural Analysis
Code Structural AnalysisCode Structural Analysis
Code Structural AnalysisDmitry Buzdin
 

More from Dmitry Buzdin (20)

How Payment Cards Really Work?
How Payment Cards Really Work?How Payment Cards Really Work?
How Payment Cards Really Work?
 
Как построить свой фреймворк для автотестов?
Как построить свой фреймворк для автотестов?Как построить свой фреймворк для автотестов?
Как построить свой фреймворк для автотестов?
 
How to grow your own Microservice?
How to grow your own Microservice?How to grow your own Microservice?
How to grow your own Microservice?
 
How to Build Your Own Test Automation Framework?
How to Build Your Own Test Automation Framework?How to Build Your Own Test Automation Framework?
How to Build Your Own Test Automation Framework?
 
Delivery Pipeline for Windows Machines
Delivery Pipeline for Windows MachinesDelivery Pipeline for Windows Machines
Delivery Pipeline for Windows Machines
 
Big Data Processing Using Hadoop Infrastructure
Big Data Processing Using Hadoop InfrastructureBig Data Processing Using Hadoop Infrastructure
Big Data Processing Using Hadoop Infrastructure
 
JOOQ and Flyway
JOOQ and FlywayJOOQ and Flyway
JOOQ and Flyway
 
Developing Useful APIs
Developing Useful APIsDeveloping Useful APIs
Developing Useful APIs
 
Whats New in Java 8
Whats New in Java 8Whats New in Java 8
Whats New in Java 8
 
Dart Workshop
Dart WorkshopDart Workshop
Dart Workshop
 
Continuous Delivery
Continuous Delivery Continuous Delivery
Continuous Delivery
 
Introduction to DevOps
Introduction to DevOpsIntroduction to DevOps
Introduction to DevOps
 
Thread Dump Analysis
Thread Dump AnalysisThread Dump Analysis
Thread Dump Analysis
 
Pragmatic Java Test Automation
Pragmatic Java Test AutomationPragmatic Java Test Automation
Pragmatic Java Test Automation
 
Mlocjs buzdin
Mlocjs buzdinMlocjs buzdin
Mlocjs buzdin
 
Web polyglot programming
Web polyglot programmingWeb polyglot programming
Web polyglot programming
 
Code Structural Analysis
Code Structural AnalysisCode Structural Analysis
Code Structural Analysis
 
Google Guava
Google GuavaGoogle Guava
Google Guava
 
Jug Intro 20
Jug Intro 20Jug Intro 20
Jug Intro 20
 
Jug intro 18
Jug intro 18Jug intro 18
Jug intro 18
 

Recently uploaded

The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersRaghuram Pandurangan
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfPrecisely
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfMounikaPolabathina
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxBkGupta21
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESmohitsingh558521
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 

Recently uploaded (20)

The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information Developers
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdf
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptx
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 

Refactoring to Macros with Clojure

  • 1. Refactoring to Macros with Clojure Dimitry Solovyov @dimituri
  • 3.
  • 4. liftIO  $  atomicModifyIORef  sc  $  n  -­‐>  (n  +  1,  ()) d  <-­‐  f  v  >>:  onInner liftIO  $  ds  `addDisposable`  d
  • 5.
  • 6.
  • 7.
  • 8. >>:
  • 10. LISP in 3 minutes via @bodiltv
  • 13.
  • 15. Clojure        It's a Lisp Compiled Dynamic types Type hints Macros Optional static types Optional Prolog
  • 17. HttpServer  server  =  HttpServer.create(address,  0); server.createContext(path,  handler); server.setExecutor(null); server.start();
  • 18. 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))
  • 19. macro |ˈmakrəәʊ| noun ( pl. macros ) 1 (also macro instruction) Computing a single instruction that expands automatically into a set of instructions to perform a particular task.
  • 20. (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))
  • 23. String  url  =  Encoding.decodeBase64(b); String  decodedUrl  =  Encoding.decodeUrl(url); String  encodedUrl  =  Encoding.encodeBase64(decodedUrl); System.out.println(encodedUrl);
  • 24. 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)
  • 25. -­‐> (-­‐>  stuff    (foo  ,,,  a)    (bar  ,,,  b  c  d)    (baz  ,,,  e  f))
  • 26. -­‐>>-­‐> (-­‐>  stuff    (foo  ,,,  a)    (bar  ,,,  b  c  d)    (baz  ,,,  e  f)) (-­‐>>  stuff    (foo  a  ,,,)    (bar  b  c  d  ,,,)    (baz  e  f  ,,,))
  • 27. -­‐<>-­‐>>-­‐> (-­‐>  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))
  • 28. “Swiss Arrows” -­‐<>> -­‐?<> -­‐!> -­‐!>> -­‐!<> <<-­‐ -­‐< -­‐<:p -­‐<< -­‐<<:p -­‐<>< -­‐<><:p https://github.com/rplevy/swiss-arrows
  • 31. Q: How do I get the result from a chain of computatoins that may fail?
  • 32. PhoneNumber  number  =  phoneMap.get(person); if  (number  !=  null)  {        Carrier  carrier  =  carrierMap.get(number);        if  (carrier  !=  null)  {                return  addressMap.get(address);        } } return  null;
  • 33. WHAT YOU'RE LOOKING FOR IS AN OPTION MONAD
  • 34.
  • 35. 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))
  • 37. All Clojure functions implement java.util.Comparator java.lang.Runnable java.util.concurrent.Callable
  • 38. 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);        } });
  • 39. 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));
  • 40. 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));
  • 41. 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));
  • 42.
  • 43. (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" nil user=>  (time  (dotimes  [i  1000000]  (len-­‐hinted  (str  i)))) "Elapsed  time:  113.317  msecs" nil
  • 44. Polyglot projects with 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
  • 45. http://www.ldn.lv Workshop: Clojure Saturday, May 18, 2013 10:00 am Citadeles iela 12, Riga http://riga.techhub.com/
  • 47. (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)))
  • 48. (ns  foo.core    (:gen-­‐class))   (defn  -­‐main    "I  don't  do  a  whole  lot  ...  yet."    [&  args]    (println  "I'm  a  little  pony")) foo$  lein  run I'm  a  little  pony
  • 49. (defn  make-­‐example  []    (proxy  [Object]  []        (toString  []  "I'm  a  little  pony"))) user=>  (.toString  (make-­‐some-­‐example)) "I'm  a  little  pony"
  • 50. 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))))
  • 51. (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));