blob: ce66391c8f18e08b246802500cb2e4ea1a843fab (
plain)
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
(.module:
[lux #*
[control
[equivalence (#+ Equivalence)]
[functor (#+ Functor)]]
[data
[collection
["." list ("list/." monoid functor)]]]])
(type: #export (Queue a)
{#front (List a)
#rear (List a)})
(def: #export empty
Queue
{#front (list)
#rear (list)})
(def: #export (from-list entries)
(All [a] (-> (List a) (Queue a)))
{#front entries
#rear (list)})
(def: #export (to-list queue)
(All [a] (-> (Queue a) (List a)))
(let [(^slots [#front #rear]) queue]
(list/compose front (list.reverse rear))))
(def: #export peek
(All [a] (-> (Queue a) (Maybe a)))
(|>> (get@ #front) list.head))
(def: #export (size queue)
(All [a] (-> (Queue a) Nat))
(let [(^slots [#front #rear]) queue]
(n/+ (list.size front)
(list.size rear))))
(def: #export empty?
(All [a] (-> (Queue a) Bit))
(|>> (get@ #front) list.empty?))
(def: #export (member? Equivalence<a> queue member)
(All [a] (-> (Equivalence a) (Queue a) a Bit))
(let [(^slots [#front #rear]) queue]
(or (list.member? Equivalence<a> front member)
(list.member? Equivalence<a> rear member))))
(def: #export (pop queue)
(All [a] (-> (Queue a) (Queue a)))
(case (get@ #front queue)
(^ (list)) ## Empty...
queue
(^ (list _)) ## Front has dried up...
(|> queue
(set@ #front (list.reverse (get@ #rear queue)))
(set@ #rear (list)))
(^ (list& _ front')) ## Consume front!
(|> queue
(set@ #front front'))))
(def: #export (push val queue)
(All [a] (-> a (Queue a) (Queue a)))
(case (get@ #front queue)
#.Nil
(set@ #front (list val) queue)
_
(update@ #rear (|>> (#.Cons val)) queue)))
(structure: #export (equivalence Equivalence<a>)
(All [a] (-> (Equivalence a) (Equivalence (Queue a))))
(def: (= qx qy)
(:: (list.equivalence Equivalence<a>) = (to-list qx) (to-list qy))))
(structure: #export functor (Functor Queue)
(def: (map f fa)
{#front (|> fa (get@ #front) (list/map f))
#rear (|> fa (get@ #rear) (list/map f))}))
|