aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/data/collection/stack.lux
blob: 6a7e5a215148a59863926f0e209c0b7988640639 (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
(.module:
  [lux #*
   [data
    [collection
     ["." list]]]])

## [Types]
(type: #export (Stack a)
  (List a))

## [Values]
(def: #export empty
  Stack
  (list))

(def: #export (size stack)
  (All [a] (-> (Stack a) Nat))
  (list.size stack))

(def: #export (empty? stack)
  (All [a] (-> (Stack a) Bit))
  (list.empty? stack))

(def: #export (peek stack)
  (All [a] (-> (Stack a) (Maybe a)))
  (case stack
    #.Nil
    #.None
    
    (#.Cons value _)
    (#.Some value)))

(def: #export (pop stack)
  (All [a] (-> (Stack a) (Maybe (Stack a))))
  (case stack
    #.Nil
    #.None
    
    (#.Cons _ stack')
    (#.Some stack')))

(def: #export (push value stack)
  (All [a] (-> a (Stack a) (Stack a)))
  (#.Cons value stack))