 
 (ns cljs-examples
   (:require [cljs.core.async :refer [chan put! take! timeout] :as async]
+            [clojure.walk :refer [prewalk]]
             [goog.net.XhrIo])
   (:require-macros [cljs.core.async.macros :refer [go]]))
 


(<!! sbc) ;; returns 2

;;; Custom Buffers ;;;;

; Need not be thread-safe. They will only be accessed by a single
; thread at a time

(require '[clojure.core.async.impl.protocols :as impl])
(import '[java.util LinkedList])

(defn debug-buffer [n a]
  (let [buf (LinkedList.)
        dbb (reify
              impl/Buffer
              (full? [this]
                (= (.size buf) n))
              (remove! [this]
                (swap! a dec)
                (.removeLast buf))
              (add! [this itm]
                (swap! a inc)
                (.addFirst buf itm))
              clojure.lang.Counted
              (count [this]
                (.size buf)))]
    dbb))

(def a-size (atom 0))

(def dbbc (chan (debug-buffer 20 a-size)))

(>!! dbbc 0)
(>!! dbbc 1)
(>!! dbbc 2)

(println @a-size)

(println (<!! dbbc) @a-size)

;;;; Alt & Timeout ;;;;



(avg [1 2 3 4 5])

(defn avg-cast-popularity [id]
  (go
   (let [cast (->> (movie-cast id)
                   <!
                   :cast
                   (clojure.core/map :id)
                   (clojure.core/map people-by-id)
                   (async/map vector)
                   <!
                   (clojure.core/map :popularity)
                   avg)]
     cast)))

