/
selective_gen_server.clj
35 lines (30 loc) · 1.22 KB
/
selective_gen_server.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
(ns co.paralleluniverse.pulsar.examples.selective-gen-server
"An implementation of the selective example using gen-server"
(:use [co.paralleluniverse.pulsar core actors])
(:refer-clojure :exclude [promise await])
(:import [co.paralleluniverse.strands Strand]))
(def adder (gen-server (reify Server
(init [_])
(terminate [_ cause])
(handle-call [_ from id [command a b]]
(case command
:add (+ a b))))))
(defn computer [adder]
(gen-server (reify Server
(init [_])
(terminate [_ cause])
(handle-call [_ from id [command a b c d]]
(case command
:compute (call-timed! adder 10 :ms :add (* a b) (* c d)))))))
(defsfn curious [nums computer]
(when (seq nums)
(let [[a b c d] (take 4 nums)
res (call! computer :compute a b c d)]
(println a b c d "->" res)
(recur (drop 4 nums) computer))))
(defn -main []
(let [ad (spawn adder)
cp (spawn (computer ad))
cr (spawn curious (take 20 (repeatedly #(rand-int 10))) cp)]
(join cr)
:ok))