range.zy 899 B

1234567891011121314151617181920212223242526272829303132333435
  1. // range loop components: mdef and hpair
  2. // mdef
  3. (mdef a b c (list 4 5 6))
  4. (assert (== a 4))
  5. (assert (== b 5))
  6. (assert (== c 6))
  7. // hpair
  8. (def h (hash a:3 b:9 c:27 d:-4))
  9. (assert (== (first (hpair h 0)) a:))
  10. (assert (== (first (hpair h 1)) b:))
  11. (assert (== (first (hpair h 2)) c:))
  12. (assert (== (first (hpair h 3)) d:))
  13. (assert (== (second (hpair h 0)) 3))
  14. (assert (== (second (hpair h 1)) 9))
  15. (assert (== (second (hpair h 2)) 27))
  16. (assert (== (second (hpair h 3)) -4))
  17. (defmac range [key value myHash & body]
  18. ^(let [n (len ~myHash)]
  19. (for [(def i 0) (< i n) (def i (+ i 1))]
  20. (begin
  21. (mdef (quote ~key) (quote ~value) (hpair ~myHash i))
  22. ~@body))))
  23. // verify that range over hashes works
  24. (def h (hash a:44 b:55 c:77 d:99))
  25. (def s "")
  26. (range k v h (set s (concat s " " (str k) "-maps->" (str v))))
  27. (assert (== s " a-maps->44 b-maps->55 c-maps->77 d-maps->99"))