~jojo/Carth

ref: 55fb4f948f1f3797078b584dc60b4f7dd68b37ed Carth/std/queue.carth -rw-r--r-- 526 bytes
55fb4f94JoJo Check `cast` in Infer instead of Gen 4 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
(import list)

;; FIFO (First In, First Out) queue
(data (Queue a)
  (Queue (List a) (List a)))

(define queue/empty
  (Queue Nil Nil))

(define (queue/push x (Queue in out))
  (Queue (list/cons x in) out))

(define queue/pop
  (fmatch (case (Queue Nil Nil)
                None)
          (case (Queue in Nil)
                (let1 [x out] (unwrap! (list/uncons (list/reverse in)))
                  (Some [(Queue Nil out) x])))
          (case (Queue in (LCons (Box [x out'])))
                (Some [(Queue in out') x]))))