iSoligorsk #3 2013
Upcoming SlideShare
Loading in...5
×
 

iSoligorsk #3 2013

on

  • 533 views

 

Statistics

Views

Total Views
533
Views on SlideShare
493
Embed Views
40

Actions

Likes
0
Downloads
2
Comments
0

1 Embed 40

http://solit.isoligorsk.org 40

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

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

    iSoligorsk #3 2013 iSoligorsk #3 2013 Presentation Transcript

    • Introduction to Clojure Soligorsk, 26.01.2013 Friedrich Boeckh
    • Clojure rocks & Sam Aaron is badassOvertone,http://blip.tv/clojure/sam-aaron-programming-music-with-overtone-5970273
    • ThoughtWorks 2012: Clojure is on the Radarhttp://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/
    • World singlesSean Corfieldhttp://corfield.org/articles/WorldSinglesWeb.pdf • 3,000,000 members • 1,000,000 emails / day • 80,000 logins / day • 2,000 concurrent users (average)
    • uSwitch.comhttp://skillsmatter.com/podcast/home/reviving-the-uswitch-back-office-with-clojure Ryan Greenhall
    • Clojure at a bankMalcolm Sparkshttp://blog.malcolmsparks.com/?p=17
    • Clojure at another bankJon Pitherhttp://www.pitheringabout.com/?p=693
    • Clojure at yet another bank
    • Clojure at Nokia Entertainmenthttp://skillsmatter.com/event/clojure/clojure-at-nokia-entertainment
    • Big Data Done Better – Mastodon C https://www.mastodonc.com/
    • Clojure gives Innovation a BoostStuart Halloway Justin Gethland & Rich Hickey
    • Russian wording in the following from Alex Ott´s talk at itsea 2012http://de.slideshare.net/alexott/clojure-lisp-for-the-modern-worldhttp://alexott.net/ru/clojure/clojure-intro/index.htmlhttp://alexott.net/en/clojure/video.html
    • Что такое 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• Активное сообщество разработчиков• Коммерческое применение• Коммерческая поддержка
    • Активное сообщество разработчиковSean Corfieldhttp://corfield.org/articles/WorldSinglesWeb.pdf, mid 2012
    • Почему Lisp?• Простой синтаксис - code as data structures homoiconic• Метапрограммирование (макросы) • Создание доменно-специфических языков DSLs • Генерация кода во время компиляции • Возможность избежать повторений• Генерация и выполнение кода в runtime• Интерактивная разработка REPL• Динамически типизированный
    • Lisp? 7 элементов + fn нотация John McCarthy 1927 - 2011Lisp Specification, 1958
    • Lisp? 7 элементов + fn нотацияPaul Graham, The Roots of Lisphttp://www.paulgraham.com/rootsoflisp.htmlhttp://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.
    • Богатство 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
    • Богатство Lisp наследия Conrad Barski (M.D.) http://vimeo.com/9605639
    • Богатство 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
    • Богатство 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
    • Богатство Lisp наследия – Emacs ??Marco Baringers SLIME Tutorial Video: http://www.youtube.com/watch?v=_B_4vhsmRRI ( underscore)
    • Отличия от других Lisp’ов• Немного упрощенный синтаксис -- меньше скобочек• Неизменяемые данные (immutability)• Добавлено больше базовых структур данных: вектора, отображения (maps) и т.д.• Расширяемый reader, но без reader macros• "Ленивые" коллекции (lazy-seq)• Мета-информация привязанная к данным и функциям• Case-sensitive names• Исключения вместо условий/рестартов• Полиморфизм: протоколы и мультиметоды• Software Transactional Memory System (STM)• Java Interoperation, http://clojure.org/java_interop ( underscore)
    • Полиморфизм на Clojure • Протоколы, мультиметоды …Stuart Sierra, https://www.ibm.com/developerworks/java/library/j-clojure-protocols/
    • Полиморфизм на Clojure• Протоколы, мультиметодыhttp://oredev.org/2012/sessions/expressing-yourself-polymorphism-in-clojure
    • Software Transactional Memory System (STM) Graig Andera http://pluralsight.com/training/Courses/TableOfContents/clojure-concurrency-tutorial
    • Почему ФП?• Функции в математическом смысле Lambda Calculus• Функции – объекты первого порядка first-class objects, higher-order functions (HOF)• Неизменяемость данных immutability • Отсутствие побочных эффектов • Возможность автоматической параллелизации кода • Меньше проблем с конкурентным выполнением кода
    • Lambda Calculus ~ “effectively programmable” Alonzo Church Stephen Kleene Alan Turing 1903 - 1995 1909 - 1994 1912 - 1954
    • Lambda Calculushttp://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/
    • Почему ФП? 1922 - 1990 You want functions?
    • Почему ФП?Get some:
    • Clojure / ФП - Namespaceshttp://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 doesnt exist.
    • 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
    • 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
    • Базовый синтаксис• Код – структуры данных• Выражение – список, первый элемент бывает: • Функция • Макрос • Специальная форма• Примеры: (def a 1) (defn square [x] (* x x)) (+ 1 2) (defmacro when [x & body] `(if ~x (do ~@body)))
    • Базовые типы данных• Числа (целые, рациональные, вещественные): 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"})
    • Коллекции• Разные коллекции: • Списки: (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)
    • 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
    • Последовательности • Последовательности (sequences): • Коллекции Clojure & Java, массивы, iterable, … • "Ленивые" операции над последовательностями • Могут быть бесконечными • Один набор операций над последовательностями: map, reduce / reductions, filter, … http://www.infoq.com/presentations/Clojure-ReducersTim McCormack, http://www.brainonfire.net/files/seqs-and-colls/main.html
    • 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
    • Clojure Destructuringhttp://blog.jayfields.com/2010/07/clojure-destructuring.html(def indexes [1 2 3]); #user/indexes(let [[x & more :as full-list] indexes] (println "x:" x "more:" more "full list:"full-list)); x: 1 more: (2 3) full list: [1 2 3](def point {:x 5 :y 7}); #user/point(let [{the-x :x the-y :y} point] (println "x:" the-x "y:" the-y)); x: 5 y: 7 Jay Fields
    • Clojure core.logic PrologDan Friedman William Byrd https://www.youtube.com/watch?v=A7de6pC-tnU
    • Clojure core.logichttp://www.vimeo.com/45128721 Prolog
    • 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
    • Библиотеки и репозитории• Простой доступ к библиотекам 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
    • Clojure + Leiningen + Emacs 24/emacs-liveon Windows 8Caveat: 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
    • 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/
    • 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
    • Lazy Searching;; define predicate on ordered, infinite seqs:;(defn in? [coll x] (cond (empty? coll) false (= x (first coll)) true (< x (first coll)) false :else (in? (rest coll) x)));; for convenience:;(def inf-seq-1 (map #(* % % %) (range)))(def inf-seq-2 (filter #(zero? (bit-and % (dec %))) (range)))(def inf-seq-3 (iterate inc 20))
    • Lazy Searching;; predicate in? can be used on a finite list of infinite seqs:;(take 10 (for [i (range)] [i (map #(in? % i) (list inf-seq-1 inf-seq-2 inf-seq-3))]));([0 (true true false)] [1 (true true false)] [2 (false true false)][3 (false false false)] [4 (false true false)] [5 (false false false)][6 (false false false)] [7 (false false false)] [8 (true true false)][9 (false false false)])(first (drop 64 (for [i (range)] [i (map #(in? % i) (list inf-seq-1 inf-seq-2 inf-seq-3))])));[64 (true true true)]
    • Lazy Searching;; for convenience:;(def three-infinite-seqs (list inf-seq-1 inf-seq-2 inf-seq-3));; predicate in? can be reduced on a finite list;; of infinite seqs;;; for convenience define in-seqs?:;(defn in-seqs? [x & colls] (reduce #(and %1 %2) (map #(in? % x) colls)))(apply in-seqs? 4 three-infinite-seqs);false(apply in-seqs? 64 three-infinite-seqs);true
    • Lazy Searching;; in-seqs? can be used to build a vector tuple in a for loop:(take 10 (for [i (range)] [i (apply in-seqs? i three-infinite-seqs)]));([0 false] [1 false] [2 false] [3 false] [4 false] [5 false][6 false] [7 false] [8 false] [9 false]);; hence, filtering on the infinite seq of tuples is possible:;(first (filter #(true? (% 1)) (for [i (range)] [i (apply in-seqs? i three-infinite-seqs)]));[64 true]
    • Lazy Searching;; finally, pick the solution from the right tupel:;((first (filter #(true? (% 1)) (for [i (range)] [i (apply in-seqs? i three-infinite-seqs)]))) 0);64;; Remark:;;;; optimize the solution by using -> / ->>;; http://kotka.de/blog/2010/04/Did_you_know_II.html
    • Balanced Primes – 1st;;;; 4clojure #116 (Prime Sandwich)(= 1103 (nth (filter balanced? (range)) 15))(defn prime? [n] (cond (< n 2) false (= n 2) true :else (= (1) (filter #(zero? (rem n %)) (range 1 (inc (Math/sqrt n)))))))(defn p-before [p] (last (filter prime? (range p))))(defn p-after [p] (first (filter prime? (drop (inc p) (range)))))(defn balanced? [p] (cond (< p 4) false (prime? p) (= p (/ (+ (p-before p) (p-after p)) 2)) :else false))
    • Balanced Primes – 2nd(= 1103 (nth (filter balanced? (range)) 15));; prime?, balanced? as before;; balanced? needs to be refreshed;; and (take 10 primes);;(def primes (filter prime? (range)))(defn p-before [p] (last (filter #(< % p) primes)))(defn p-after [p] (first (filter #(> % p) primes)))
    • Balanced Primes – 3rd(= 1103 (nth (filter balanced? (range)) 15));; prime?, primes, in? as before(def partitioned-primes (map #(vector (second %) (= (second %) (/ (+ (first %) (last %)) 2))) (partition 3 1 primes)))(def balanced-primes (map #(% 0) (filter #(true? (% 1)) partitioned-primes)))(defn balanced? [p] (in? balanced-primes p)))
    • Вопросы?Спасибо за внимание.