2. Clojure rocks & Sam Aaron is badass
Overtone,
http://blip.tv/clojure/sam-aaron-programming-music-with-overtone-5970273
3. ThoughtWorks 2012:
Clojure is on the Radar
http://www.thoughtworks.com/articles
/technology-radar-october-2012
… even Microsoft cares
… as always - it does not make
everybody happy:
http://cafe.elharo.com/programming/java-
programming/why-functional-programming-in-java-is-
dangerous/
12. Russian wording in the following
from Alex Ott´s talk at itsea 2012
http://de.slideshare.net/alexott/clojure-lisp-for-the-modern-world
http://alexott.net/ru/clojure/clojure-intro/index.html
http://alexott.net/en/clojure/video.html
13. Что такое Clojure?
• Rich Hickey, 17.10.2007, с v1.4, RC v1.5
• Lisp-ообразный
• Функциональный
• Конкурентное программирование
http://stackoverflow.com/questions/1050222/concurrency-vs-
parallelism-what-is-the-difference
• Многоплатформенный (JVM, .Net, JavaScript, etc.)
• Открытый исходный код и либеральная лицензия
Eclipse Public License 1.0
• Активное сообщество разработчиков
• Коммерческое применение
• Коммерческая поддержка
15. Почему Lisp?
• Простой синтаксис - code as data structures homoiconic
• Метапрограммирование (макросы)
• Создание доменно-специфических языков DSLs
• Генерация кода во время компиляции
• Возможность избежать повторений
• Генерация и выполнение кода в runtime
• Интерактивная разработка REPL
• Динамически типизированный
16. Lisp? 7 элементов + fn нотация
John McCarthy
1927 - 2011
Lisp Specification, 1958
17. Lisp? 7 элементов + fn нотация
Paul Graham, The Roots of Lisp
http://www.paulgraham.com/rootsoflisp.html
http://www.cse.sc.edu/~mgv/csce531sp07/jmc.pdf
• (quote x) returns x
• (atom x)returns the atom t (true) if the value of x is
an atom or the empty list. Otherwise it returns ().
• (eq x y) returns t if the values of x and y are the
same atom or both the empty list, and () otherwise.
• (car x) expects the value of x to be a list, and returns its first element.
• (cdr x) expects the value of x to be a list, and returns everything after
the first element.
• (cons x y) expects the value of y to be a list, and returns a list
containing the value of x followed by the elements of the value of y.
• (cond (p1 e1) ... (pn en)) is evaluated as follows. The p
expressions are evaluated in order until one returns t (true). When one is
found, the value of the corresponding e expression is returned as the value
of the whole cond expression.
• A function is expressed as (lambda (p1 ... pn) e), where
p1 ... pn are atoms, called parameters, and e is an expression.
18. Богатство Lisp наследия
• Hal Abelson, Jerry Sussman and Julie Sussman, Structure and
Interpretation of Computer Programs (SICP),
http://mitpress.mit.edu/sicp/,
http://newstar.rinet.ru/~goga/sicp/sicp.pdf (russ.),
http://ocw.mit.edu/courses/electrical-engineering-and-
computer-science/6-001-structure-and-interpretation-of-
computer-programs-spring-2005/video-lectures/
• Peter Seibel, Practical Common Lisp,
http://www.gigamonkeys.com/book/
• Paul Graham, On Lisp, http://www.paulgraham.com/onlisp.html,
http://lib.store.yahoo.net/lib/paulgraham/onlisp.pdf
20. Богатство Lisp наследия
• VLSI design, Boeing, US government & military, ITA Software (in
2010 acquired by Google)
• NASA, The Remote Agent Experiment - Debugging code from
60 million miles away, Ron Garret, 14 February 2012
• Paul Graham, Beating the Averages,
http://www.paulgraham.com/avg.html,
http://lib.store.yahoo.net/lib/paulgraham/bbnexcerpts.txt
• Andreas Lehmann, Common Lisp Raytracer, (source code
transcript e. g. here https://github.com/fbmnds/clrt),
videos and PDFs
http://rudairandamacha.blogspot.de/2012/09/writing-simple-
raytracer-in-common-lisp.html
21. Богатство Lisp наследия
• Peter Norvig, Paradigms of Artificial Intelligence Programming:
Case Studies in Common Lisp (PAIP),
http://norvig.com/paip.html
• Daniel P. Friedman, Matthias Felleisen, The Little Schemer,
http://mitpress.mit.edu/books/little-schemer
• Overview by Vsevolod Dyomkin,
http://lisp-univ-etc.blogspot.com/2012/12/lisp-books.html
• Rich Hickey, Books that influenced Clojure,
http://www.amazon.com/gp/richpub/listmania/fullview/R3LG3Z
BZS4GCTH
22. Богатство Lisp наследия – Emacs ??
Marco Baringer's SLIME Tutorial Video:
http://www.youtube.com/watch?v=_B_4vhsmRRI
( underscore)
23. Отличия от других Lisp’ов
• Немного упрощенный синтаксис -- меньше скобочек
• Неизменяемые данные (immutability)
• Добавлено больше базовых структур данных: вектора,
отображения (maps) и т.д.
• Расширяемый reader, но без reader macros
• "Ленивые" коллекции (lazy-seq)
• Мета-информация привязанная к данным и функциям
• Case-sensitive names
• Исключения вместо условий/рестартов
• Полиморфизм: протоколы и мультиметоды
• Software Transactional Memory System (STM)
• Java Interoperation, http://clojure.org/java_interop
( underscore)
24. Полиморфизм на Clojure
• Протоколы, мультиметоды
…
Stuart Sierra, https://www.ibm.com/developerworks/java/library/j-clojure-protocols/
25. Полиморфизм на Clojure
• Протоколы, мультиметоды
http://oredev.org/2012/sessions/expressing-yourself-polymorphism-in-clojure
26. Software Transactional Memory System (STM)
Graig Andera
http://pluralsight.com/training/Courses/TableOfContents/clojure-concurrency-tutorial
27. Почему ФП?
• Функции в математическом смысле
Lambda Calculus
• Функции – объекты первого порядка
first-class objects, higher-order functions (HOF)
• Неизменяемость данных immutability
• Отсутствие побочных эффектов
• Возможность автоматической параллелизации кода
• Меньше проблем с конкурентным выполнением кода
28. Lambda Calculus ~ “effectively programmable”
Alonzo Church Stephen Kleene Alan Turing
1903 - 1995 1909 - 1994 1912 - 1954
29. Lambda Calculus
http://www.infoq.com/presentations/Y-Combinator
(defn Y
[h]
Jim Weirich (Y-rich)
((fn [f] (f f)) (fn [f]
(h (fn [n]
((f f) n))))))
(def fact
(Y (fn [g]
(fn [n]
(if (< n 2) 1 (* n (g (dec n))))))))
transcript by Michael Kohl,
http://citizen428.net/blog/2010/12/14/clojure-deriving-the-y-combinator-in-7-stolen-steps/
32. Clojure / ФП - Namespaces
http://clojure.org/namespaces
• Namespaces are mappings from simple (unqualified) symbols
to Vars and/or Classes.
• Namespaces are first-class, they can be enumerated etc.
• Namespaces are also dynamic, they can be created, removed
and modified at runtime, at the Repl etc.
• The best way to set up a new namespace at the top of a Clojure
source file is to use the ns macro.
• The current namespace, *ns* can and should be set only with
a call to in-ns or the ns macro, both of which create the
namespace if it doesn't exist.
33. Clojure / ФП - Namespaces
• clojure.core - Fundamental library of the Clojure language
• clojure.data - Non-core data functions.
• clojure.inspector - Graphical object inspector for Clojure data
structures.
• clojure.java.browse - Start a web browser from Clojure
• clojure.java.io
• clojure.java.javadoc - A repl helper to quickly open javadocs.
• clojure.java.shell
• clojure.main
• clojure.pprint - A Pretty Printer for Clojure
• clojure.reflect - Reflection on Host Types
• clojure.repl - Utilities meant to be used interactively at the REPL
34. Clojure / ФП - Namespaces
• clojure.set
• clojure.stacktrace - Print stack traces oriented towards Clojure, not
Java.
• clojure.string
• clojure.template - Macros that expand to repeated copies of a
template expression.
• clojure.test - A unit testing framework.
• clojure.walk
• clojure.xml - XML reading/writing.
• clojure.zip - Functional hierarchical zipper, with navigation, editing,
and enumeration. See Huet
35. Базовый синтаксис
• Код – структуры данных
• Выражение – список, первый элемент бывает:
• Функция
• Макрос
• Специальная форма
• Примеры:
(def a 1) (defn square [x] (* x x))
(+ 1 2) (defmacro when [x & body]
`(if ~x (do ~@body)))
36. Базовые типы данных
• Числа (целые, рациональные, вещественные):
42, 1/3, 12345678901234567N, 4.2, 42.2M
• Строки: "string... "
• Знаки (characters): a, newline, …
• Регулярные выражения: #"d+"
• Логические значения: true, false
• как null в Java nil
• Симбол (symbol): name
• keyword: :name,
(:name {:name "test"})
37. Коллекции
• Разные коллекции:
• Списки: '(1 2 3 "abc") (quoting)
• Вектора: [1 2 3] (no quoting)
• Отображения (maps): {:k1 1234 :k2 "value"}
• Множества: #{:val1 "text" 1 2 10}
• persistent & transient коллекции
• Вектора, отображения и множества сами являются
функциями:
([1 2 3] 2) ({:a 1 :b 2 :c 3} :b)
=> 3 => 2
(no quoting) (no quoting)
38. Persistent collections
• Новая копия данных при
изменении
• Затрагивается только
измененная часть
• Производительность
сравнима с коллекциями
Java
Rich Hickey to the Western Mass.
Developers Group on Clojure and
concurrency.
(2,5h – well invested time!)
http://blip.tv/clojure/clojure-concurrency-819147
http://www.youtube.com/user/ClojureTV
39. Последовательности
• Последовательности (sequences):
• Коллекции Clojure & Java, массивы, iterable, …
• "Ленивые" операции над последовательностями
• Могут быть бесконечными
• Один набор операций над последовательностями:
map, reduce / reductions, filter, …
http://www.infoq.com/presentations/Clojure-Reducers
Tim McCormack, http://www.brainonfire.net/files/seqs-and-colls/main.html
40. Google Lectures on MapReduce
http://www.youtube.com/watch?v=yjPBkvYh-ss
http://www.youtube.com/watch?v=-vD6PUdf3Js
http://www.youtube.com/watch?v=5Eib_H_zCEY
http://www.youtube.com/watch?v=1ZDybXl212Q
Aaron Kimball http://www.youtube.com/watch?v=BT-piFBP4fE
44. IDE & средства сборки кода
• Поддержка в IDE/редакторах:
– Eclipse (Counterclockwise)
– Netbeans (Enclojure)
– IntelliJ IDEA (La Clojure)
– Emacs + SLIME (depricated) или nRepl / ritz-repl
• Ubuntu: https://launchpad.net/~cassou/+archive/emacs
• Windows: ftp://ftp.gnu.org/gnu/emacs/windows/, cygwin.com
– VimClojure
– LightTable, Textmate, Sublime Text 2, Jedit
• Средства сборки кода:
– Поддержка Clojure в Maven, Ant, Cake, Gradle
– самый популярный: Leiningen (спасибо, Phil)
Phil Hagelberg
45. Библиотеки и репозитории
• Простой доступ к библиотекам JVM
• Библиотеки написанные на Clojure:
– Web-разработка: Compojure, Ring, Scriptjure
– RDBMS: ClojureQL, clojure.java.jdbc, Korma
– NoSQL: Monger, Clouch, …
– GUI: Seesaw
http://darevay.com/talks/clojurewest2012/#/title-slide
– Логическое программирование: core.logic
• Репозитории: Maven Central, Clojars.org
46. Clojure + Leiningen + Emacs 24/emacs-live
on Windows 8
Caveat: emacs-live does not yet collaborate on Windows with ritz-repl,
i.e.emacs-live provides not the full SLIME functionality as per mid Jan. 2013
• Install Java JDK from http://www.java.com
• Install cygwin, use defaults; use cygwin package manager to install git
• Install Leiningen 2.0 in cygwin, this includes Clojure:
– $ cd; git clone
https://raw.github.com/technomancy/leiningen/preview/bin/lein
– Place it on your $PATH, preferably use ~/bin
– $ chmod 755 ~/bin/lein
– $ lein self-install
• Install Emacs 24 + emacs-live in Windows:
• Download Emacs 24 from ftp://ftp.gnu.org/gnu/emacs/windows/
• Download emacs-live from https://github.com/overtone/emacs-live
• Move directory emacs-live to
C:Users%your user name%AppDataRoaming.emacs.d
47. Let´s start: 4clojure.com
See also:
http://www.lisperati.com/clojure-spels/casting.html
https://github.com/relevance/labrepl
http://clojurekoans.com/
http://tryclj.com/
48. Lazy Searching
;;;; 4clojure #108
;; find f satisfying:
;
(= 64 (f (map #(* % % %) (range)) ;; perfect cubes
(filter #(zero? (bit-and % (dec %))) (range)) ;; powers of 2
(iterate inc 20))) ;; at least as large as 20
;; caution: the function for powers of 2 becomes
;; highly inefficient for n > 28:
;
(time (last (take 28 (filter #(zero? (bit-and % (dec %))) (range)))))
;"Elapsed time: 25954.456087 msecs"
;67108864