flan/test/programs/clone-slice.flan

31 lines
954 B
Plaintext

;;;; (clone xs) on a slice: the elements copied into a block from the context
;;;; allocator, or from one named, and answered as a slice over it. Writing
;;;; through the copy leaves the original alone, and the other way round.
(defstruct P [x i32 y f64])
(defn show [xs [i32]] ()
(dotimes [i (length xs)]
(print (at xs i))
(print " "))
(println ""))
(defn main [] i32
(let [arr [1 2 3 4 5]
c (clone (slice arr 1 4))]
(set (at c 0) 20)
(set (at arr 2) 30)
(show c)
(show (slice arr)))
;; From a Vec's view, into a named arena; wider elements than a byte.
(let [a (arena-new 1024)
v (vec-new P)]
(push v (P {.x 1 .y 1.5}))
(push v (P {.x 2 .y 2.5}))
(let [c (clone (slice v) a)]
(set (.x (at v 0)) 9)
(println (.x (at c 0)) (.y (at c 1)) (length c))))
;; An empty slice clones to an empty slice.
(let [e (clone (slice (bytes-view "abc") 1 1))]
(println (length e)))
0)