Clojure core.async for computing data

I started using the core.async clojure library. I realized that CSP concepts, channels, go blocks are very easy to use. However, I am not sure if I use them correctly. I have the following code -

(def x-ch (chan))
(def y-ch (chan))
(def w1-ch (chan))
(def w2-ch (chan))

; they all return matrices
(go (>! x-ch (Mat/* x (map #(/ 1.0 %) (max-fold x)))))
(go (>! y-ch (Mat/* y (map #(/ 1.0 %) (max-fold y)))))
(go (>! w1-ch (gen-matrix 200 300)))
(go (>! w2-ch (gen-matrix 300 100)))

(let [x1 (<!! (go (<! x-ch)))
        y1 (<!! (go (<! y-ch)))
        w1 (<!! (go (<! w1-ch)))
        w2 (<!! (go (<! w2-ch)))]

    ;; do stuff w/ x1 y1 w1 w2
)

() x y. , . . . go , go . let, . <!! , .

, , , , . ?

+4
3

go , . 4 , , . , , .

(let [x1-ch (go (Mat/* x (map #(/ 1.0 %) (max-fold x))))
      y1-ch (go (Mat/* y (map #(/ 1.0 %) (max-fold y))))
      w1-ch (go (gen-matrix 200 300))
      w2-ch (go (gen-matrix 300 100))
      x1 (<!! x1-ch)
      y1 (<!! y1-ch)
      w1 (<!! w1-ch)
      w2 (<!! w2-ch)]
  ;; do stuff w/ x1 y1 w1 w2
  )
+3

future .

:

 (def f 
   (future 
     (Thread/sleep 10000) 
     (println "done") 
     100))

, , , 10 "".

, :

(deref f)
; or @f

.

, deref 10 , , .

, , , . :

 (future 
    (Mat/* x (map #(/ 1.0 %) (max-fold x))))
+5

, , Clojure Reducers, Aphyr Tesser. , , . , . go, , , .

+1

Source: https://habr.com/ru/post/1612171/


All Articles