aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/data/collection/dictionary/plist.lux
blob: 7b11ee20847e5f612179db70506d8302abf9eb5c (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
(.module:
  [lux #*
   [data
    ["." product]
    [text ("text/." equivalence)]
    [collection
     [list ("list/." functor)]]]])

(type: #export (PList a)
  (List [Text a]))

(def: #export (get key properties)
  (All [a] (-> Text (PList a) (Maybe a)))
  (case properties
    #.Nil
    #.None

    (#.Cons [k' v'] properties')
    (if (text/= key k')
      (#.Some v')
      (get key properties'))))

(do-template [<name> <type> <access>]
  [(def: #export <name>
     (All [a] (-> (PList a) (List <type>)))
     (list/map <access>))]

  [keys   Text product.left]
  [values a    product.right]
  )

(def: #export (contains? key properties)
  (All [a] (-> Text (PList a) Bit))
  (case (get key properties)
    (#.Some _)
    #1

    #.None
    #0))

(def: #export (put key val properties)
  (All [a] (-> Text a (PList a) (PList a)))
  (case properties
    #.Nil
    (list [key val])

    (#.Cons [k' v'] properties')
    (if (text/= key k')
      (#.Cons [key val]
              properties')
      (#.Cons [k' v']
              (put key val properties')))))

(def: #export (update key f properties)
  (All [a] (-> Text (-> a a) (PList a) (PList a)))
  (case properties
    #.Nil
    #.Nil

    (#.Cons [k' v'] properties')
    (if (text/= key k')
      (#.Cons [k' (f v')] properties')
      (#.Cons [k' v'] (update key f properties')))))

(def: #export (remove key properties)
  (All [a] (-> Text (PList a) (PList a)))
  (case properties
    #.Nil
    properties

    (#.Cons [k' v'] properties')
    (if (text/= key k')
      properties'
      (#.Cons [k' v']
              (remove key properties')))))