infoShare 2014: Maciej Saganowski, Designing Mobile Services.
infoShare 2011 - Jacek Laskowski - Programowanie Funkcjonalne Funkcyjnie z Clojure Praktycznie
1. Programowanie funkcjonalne
funkcyjnie z Clojure praktycznie
O budowaniu współbieżnej aplikacji graficznej
Jacek Laskowski
jacek@japila.pl
http://www.JacekLaskowski.pl
wersja 1.0, 12.05.2011
2. Ja...cek Laskowski
• Entuzjasta Java EE, OSGi, SCA oraz programowania funkcyjnego (Clojure)
• Założyciel i lider Warszawa JUG
• Organizator Confitura 2011
• Członek zespołu NetBeans DreamTeam
• Blogger na http://JacekLaskowski.pl
• Blogger na http://blog.japila.pl
• Twittuje jako @jaceklaskowski
• Członek zespołów Apache Geronimo i Apache OpenEJB
• Specjalista produktów IBM WebSphere w IBM Polska
5. Źródła inspiracji
• Programming Concurrency on the JVM: Mastering
Synchronization, STM, and Actors
http://www.jaceklaskowski.pl/wiki/
Book_review:_Programming_Concurrency_on_the_JVM
• Java Concurrency in Practice
http://jcip.net
6. $ java -version
java version "1.6.0_24"
Java(TM) SE Runtime Environment (build 1.6.0_24-b07-334-10M3326)
Java HotSpot(TM) 64-Bit Server VM (build 19.1-b02-334, mixed mode)
$ clj
user=> (clojure-version)
"1.3.0-alpha6"
7. user=> ; przestrzeń nazewnicza (ang. namespace) w Clojure
user=> ; mapa symboli na ich odpowiedniki pełne - odnośniki i klasy
user=> (ns gui)
nil
gui=>
gui=> (def nazwa-konferencji "InfoShare")
#'gui/nazwa-konferencji
gui=> (import javax.swing.JFrame)
javax.swing.JFrame
gui=> (use 'clojure.contrib.swing-utils)
nil
gui=> (ns-interns 'gui)
{nazwa-konferencji #'gui/nazwa-konferencji}
21. • Clojure jest językiem funkcyjnym
• Clojure hołduje funkcjom czystym (ang. pure functions)
• Tożsamość (ang. identity) = logiczny byt, symbol
• Stan (ang. state) = wartość
• Czas (ang. time) = migawka
• Clojure oddziela tożsamość od stanu = upraszcza współbieżność
29. Clojure a stan
• 4 rodzaje referencji (odwołań do pamięci) w Clojure
• Vars - dynamiczna referencja do zmiennej wartości (per wątek)
• Refs - transakcyjny Var dla wielu wątków
• Agents - asynchroniczny i niezależny
• Atoms - synchroniczny i niezależny stan
32. $ grep agent ants.clj
"create an ant at the location, returning an ant agent on the location"
(agent loc))))
"places initial food and ants, returns seq of ant agents"
;ant agent functions
;an ant agent tracks the location of an ant, and controls the behavior of
"the main function for the ant agent"
(send-off *agent* #'behave))
(def animator (agent nil))
(send-off *agent* #'animation))
(def evaporator (agent nil))
(send-off *agent* #'evaporation))
35. user=> (doc future)
-------------------------
clojure.core/future
([& body])
Macro
Takes a body of expressions and yields a future object that will
invoke the body in another thread, and will cache the result and
return it on all subsequent calls to deref/@. If the computation has
not yet finished, calls to deref/@ will block.
36. user=> (source future-cancel)
(defn future-cancel
"Cancels the future, if possible."
{:added "1.1"
:static true}
[^java.util.concurrent.Future f] (.cancel f true))
37. user=> (doc pmap)
-------------------------
clojure.core/pmap
([f coll] [f coll & colls])
Like map, except f is applied in parallel. Semi-lazy in that the
parallel computation stays ahead of the consumption, but doesn't
realize the entire result unless required. Only useful for
computationally intensive functions where the time of f dominates
the coordination overhead.
user=> (doc pcalls)
-------------------------
clojure.core/pcalls
([& fns])
Executes the no-arg fns in parallel, returning a lazy sequence of
their values
39. $ javap -classpath clojure.jar clojure.lang.Agent
Compiled from "Agent.java"
public class clojure.lang.Agent extends clojure.lang.ARef{
java.util.concurrent.atomic.AtomicReference aq;
public static final java.util.concurrent.ExecutorService
pooledExecutor;
public static final java.util.concurrent.ExecutorService soloExecutor;
public java.lang.Object dispatch(clojure.lang.IFn, clojure.lang.ISeq,
boolean);
static void dispatchAction(clojure.lang.Agent$Action);
void enqueue(clojure.lang.Agent$Action);
public int getQueueCount();
public static int releasePendingSends();
...
42. Programowanie funkcjonalne
funkcyjnie z Clojure praktycznie
O budowaniu współbieżnej aplikacji graficznej
Jacek Laskowski
jacek@japila.pl
http://www.JacekLaskowski.pl
wersja 1.0, 12.05.2011