Skip to content

A peer-to-peer stack for code collaboration

1.3 KB
Raw Blame
;; A "counter" machine that exposes an `increment` mutator and a `get-value`
;; accessor.
;;
;; This file provides functions to interact with a counter machine
;;
;;     (counter/init-machine! "my-machine-id") ;; => :ok
;;     (counter/get-value "my-machine-id") ;; => 0
;;     (counter/increment! "my-machine-id") ;; => 1
;;     (counter/get-value "my-machine-id") ;; => 1
;;

(load! (find-module-file! "prelude.rad"))

(import prelude/test '[assert-equal] :unqualified)

(def counter/prelude
  '[
    (def counter (ref 0))
    (def increment
      "Increments the counter by one and returns the new counter."
      (fn []
        (def x (read-ref counter))
        (write-ref counter (+ x 1))))
    (def get-value
      "Return the current value of the counter"
      (fn []
        (read-ref counter)))
  ]
)

(def counter/init-machine!
  "Send the counter application prelude to a machine"
  (fn [machine-id]
    (daemon/send! machine-id counter/prelude)
    :done
))

(def counter/increment!
  "Increment the counter of amachineand return the new counter value"
  (fn [machine-id]
    (first (daemon/send! machine-id '[(increment)]))
    ))

(def counter/get-value
  "Get the current counter value of a machine"
  (fn [machine-id]
    (daemon/query! machine-id '(get-value))
    ))