Changes between Version 42 and Version 43 of Clojure Client Tutorial


Ignore:
Timestamp:
Aug 8, 2011 12:52:01 PM (13 years ago)
Author:
kedwar10
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • Clojure Client Tutorial

    v42 v43  
    340340
    341341{{{
    342 client.core=> (defn scan-area2 [quagent radius]
     342client.core=> (defn scan-area [quagent radius]
    343343                (send-and-get quagent :radar :now [radius]
    344344                              {}
     
    366366
    367367{{{
    368 client.core=> (apply (partial move :Bob) (take 2 (second (get (scan-area2 :Bob 8000) "quagent_item_gold"))))
     368client.core=> (apply (partial move :Bob) (take 2 (second (get (scan-area :Bob 8000) "quagent_item_gold"))))
    369369[]
    370370}}}
    371371
     372Now suppose that scanning the area takes an inordinate amount of time and blocking until it completes is no longer practical.  By using the "send-and-get-later" function, the results can be computed in a new thread and dereferenced later.
     373
     374{{{
     375client.core=> (defn scan-area2 [quagent radius]
     376                (send-and-get-later quagent :radar :now [radius]
     377                                    {}
     378                                    (fn [prev [_ item-type & pos]]
     379                                      (merge-with concat prev {item-type (list (seq->doubles pos))}))))
     380#'client.core/scan-area2
     381client.core=> (def items (scan-area2 :Bob 8000))
     382#'client.core/items
     383client.core=> (type items)
     384clojure.core$future_call$reify__5508
     385client.core=> (future-done? items)
     386true
     387client.core=> @items
     388{"quagent_item_gold" ([375.085327 56.309933 1.374918] [1019.278626 42.455196 0.505915] ... (output truncated)
     389client.core=> (pp)
     390{"quagent_item_gold"
     391 ([375.085327 56.309933 1.374918]
     392  [1019.278626 42.455196 0.505915]
     393  [905.141357 8.130102 0.569713]),
     394 "quagent_item_treasure"
     395 ([572.16864 20.462269 0.901278] [697.711304 63.434952 0.739097]),
     396 "info_player_deathmatch"
     397 ([32.000244 -90.0 0.223811] [0.125 0.0 90.0]),
     398 "player" ([611.700012 90.081947 0.0] [32.0 -90.0 0.0])}
     399nil
     400}}}
     401
     402(Note that if you try to dereference a future before it completes, it will block the current thread until it does.)
     403
     404Now let's suppose that every time the quagent reports finding an item, it should print out the distance that item.  This can be accomplished with the "send-and-watch" function.
     405
     406{{{
     407client.core=> (defn scan-area3 [quagent radius] (send-and-watch quagent :radar :now [radius] nil (fn [prev data] (rest data)) (fn [k r o n] (println "Item:" (first n) "Distance:" (second n)))))
     408#'client.core/scan-area3
     409client.core=> (scan-area3 :Bob 8000)                                                                                                                                                             
     410:watcher311
     411Item: player Distance: 611.700012
     412Item: player Distance: 32.000000
     413Item: info_player_deathmatch Distance: 32.000244
     414Item: quagent_item_treasure Distance: 572.168640
     415Item: quagent_item_gold Distance: 375.085327
     416Item: quagent_item_gold Distance: 1019.278626
     417Item: quagent_item_treasure Distance: 697.711304
     418Item: quagent_item_gold Distance: 905.141357
     419Item: info_player_deathmatch Distance: 0.125000
     420}}}
     421
     422This function returns a watcher key that can be used to remove the watcher if desired.  Note that this example only makes used the (n)ew argument.
    372423----
    373424