let x = 42;
const y = [1,2,3];
y.splice(0,1); // [1]
y // [2, 3]
// woops
Runs on the JVM (cljs in the browser)
(defn add [a b]
(+ a b))
function add(a, b) {
return a + b;
}
(filter #{:red :green} [:red :green :blue])
const allowed = new Set(["green", "blue"]);
["red", "green", "blue"].filter(c => allowed.has(c));
(-> {:x { :y [ {:z #{"apple"}}]}}
(update-in [:x :y 0 :z] conj "banana"))
;; {:x {:y [{:z #{"apple" "banana"}}]}}
(map inc (range 0 100))
(concat
(map inc (range 0 25))
(map inc (range 25 50))
(map inc (range 50 75))
(map inc (range 75 100)))
(map inc (range 0 10000000))
(pmap inc (range 0 10000000))
(map inc (range 0 10000000))
"Elapsed time: 2274.327422 msecs"
(pmap inc (range 0 10000000))
"Elapsed time: 27530.360776 msecs"
(map sleep-100-ms-then-inc (range 0 1000))
(pmap sleep-100ms-then-inc (range 0 1000))
(map sleep-100-ms-then-inc (range 0 1000))
"Elapsed time: 100195.859968 msecs"
(pmap sleep-100ms-then-inc (range 0 1000))
"Elapsed time: 3218.437153 msecs"
coordinated | uncoordinated | |
---|---|---|
Sync | Ref | Atom |
Async | Agents |
(def me
(atom {:name "vitali"
:age 32
:languages #{:clojure :python, :smalltalk}}))
(deref me)
{:name "vitali",
:age 32,
:languages #{:smalltalk :clojure :python}}
@me
{:name "vitali",
:age 32,
:languages #{:smalltalk :clojure :python}}
;; age
(swap! me update :age inc)
{:name "vitali",
:age 33,
:languages #{:smalltalk :clojure :python}}
;; learn css
(swap! me update :languages conj :css)
{:name "vitali",
:age 33,
:languages #{:smalltalk :clojure :css :python}}
(def player (ref {:health 500 :attack 10 :items #{}}))
(def mob1 (ref {:health 100 :attack 2 :items #{"big-banana"}}))
(def mob2 (ref {:health 100 :attack 4 :items #{"banana"}}))
(defn attack! [attacker enemy]
(dosync
(let [attacker-value (:attack @attacker)
enemy-value (:attack @enemy)]
(alter enemy update :health - attacker-value)
(alter attacker update :health - (/ enemy-value 2)))))
(defn loot! [to from]
(dosync
(when-let [item (first (:items @from))]
(alter to update :items conj item)
(alter from update :items disj item))))
(attack! player mob1)
(attack! player mob2)
(loot! player mob2)
@player
{:health 497, :attack 10, :items #{"banana"}}
@mob2
{:health 90, :attack 4, :items #{}}
@mob1
{:health 90, :attack 2, :items #{"big-banana"}}
(def sleeper (agent 0))
(send-off sleeper sleep-100ms-then-inc)
@sleeper
=> 0
(await sleeper)
@sleeper
=> 1
defmodule Counter do
def loop(count) do
receive do
{:next} ->
IO.puts("Current count: #{count}")
loop(count + 1)
end
end
end
counter = spawn(Counter, :loop, [1])
#PID<0.47.0>
iex(2)> send(counter, {:next})
Current count: 1
iex(3)> send(counter, {:next})
Current count: 2
defmodule Demo do
def counter() do
receive do
value ->
IO.puts value
Process.sleep(1000)
send(self(), value + 1)
end
counter()
end
end
defmodule Demo do
def ctrl(t_pid) do
receive do
:start ->
t_pid = spawn(&counter/0)
send(t_pid, 0)
ctrl(t_pid)
:stop ->
Process.exit(t_pid, :kill)
ctrl(t_pid)
end
end
def counter() do
receive do
value ->
IO.puts value
Process.sleep(1000)
send(self(), value + 1)
end
counter()
end
end
(map inc (range 0 10000000))
(pmap inc (range 0 10000000))
__kernel void vector_add(__global const int* A,
__global const int* B,
__global int* C)
{
int id = get_global_id(0);
C[id] = A[id] + B[id];
}
OpenCL device
OpenCL device
OpenCL Context
Compute Unit
PE