-
Notifications
You must be signed in to change notification settings - Fork 173
/
Copy pathupdating.carp
43 lines (36 loc) · 1.05 KB
/
updating.carp
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
36
37
38
39
40
41
42
43
;; A slightly bigger example of how to work with structs and arrays that change
(use IO)
(Project.no-echo)
(deftype Pos
[x Float
y Float])
(defn incf [x]
(Float.+ x 1.0f))
(defmodule Pos
(defn move [pos]
(=> pos
(Pos.update-x &incf)
(Pos.update-y &incf))))
(deftype Monster
[pos Pos
hp Int
name String])
(defmodule Monster
(defn init-random [name]
(Monster.init
(Pos.init (Float.random-between 0.0f 100.0f)
(Float.random-between 0.0f 100.0f))
100
@name))
(defn move [monster]
(Monster.update-pos monster &Pos.move)))
(defn main []
(do
; here we always use the same seed for determinism, for simple random
; numbers that change you can for instance use (System.time)
(Random.seed-from 0.1)
(let [monsters (Array.copy-map &Monster.init-random &[@"Pegasus" @"Dragon" @"Devil"])]
(do
(println (ref (Array.str &monsters)))
(let [new-monsters (Array.endo-map &Monster.move monsters)]
(println (ref (Array.str &new-monsters))))))))