aboutsummaryrefslogtreecommitdiff
path: root/api/tables.yaml
blob: 6ab5f50546805eca140e2534900b3b2504938eee (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
Table.clear:
  description: >
    Removes all key-value pairs from the table.
  return:
    type: 'Void'
    description: >
      Nothing.
  args:
    t:
      type: '&{K=V}'
      description: >
        The reference to the table.
  example: |
    >> t.clear()
    
Table.get:
  description: >
    Retrieves the value associated with a key, or returns `none` if the key is not present.
  note: >
    Default values for the table are ignored.
  return:
    type: 'V?'
    description: >
      The value associated with the key or `none` if the key is not found.
  args:
    t:
      type: '{K=V}'
      description: >
        The table.
    key:
      type: 'K'
      description: >
        The key whose associated value is to be retrieved.
  example: |
    >> t := {"A"=1, "B"=2}
    >> t.get("A")
    = 1?
    
    >> t.get("????")
    = none
    
    >> t.get("A")!
    = 1
    
    >> t.get("????") or 0
    = 0
    
Table.get_or_set:
  description: >
    If the given key is in the table, return the associated value. Otherwise,
    insert the given default value into the table and return it.
  note: >
    If no default value is provided explicitly, but the table has a
    default value associated with it, the table's default value will be used.

    The default value is only evaluated if the key is missing.
  return:
    type: 'V?'
    description: >
      Either the value associated with the key (if present) or the default value. The
      table will be mutated if the key is not already present.
  args:
    t:
      type: "&{K=V}"
      description: >
        The table.
    key:
      type: "K"
      description: >
        The key whose associated value is to be retrieved.
    default:
      type: "V"
      description: >
        The default value to insert and return if the key is not present in the table.
  example: |
    >> t := &{"A"=@[1, 2, 3]; default=@[]}
    >> t.get_or_set("A").insert(4)
    >> t.get_or_set("B").insert(99)
    >> t
    = &{"A"=@[1, 2, 3, 4], "B"=@[99]}

    >> t.get_or_set("C", @[0, 0, 0])
    = @[0, 0, 0]
    >> t
    = &{"A"=@[1, 2, 3, 4], "B"=@[99], "C"=@[0, 0, 0]}
    
Table.has:
  description: >
    Checks if the table contains a specified key.
  return:
    type: 'Bool'
    description: >
      `yes` if the key is present, `no` otherwise.
  args:
    t:
      type: '{K=V}'
      description: >
        The table.
    key:
      type: 'K'
      description: >
        The key to check for presence.
  example: |
    >> {"A"=1, "B"=2}.has("A")
    = yes
    >> {"A"=1, "B"=2}.has("xxx")
    = no
    
Table.remove:
  description: >
    Removes the key-value pair associated with a specified key.
  return:
    type: 'Void'
    description: >
      Nothing.
  args:
    t:
      type: '{K=V}'
      description: >
        The reference to the table.
    key:
      type: 'K'
      description: >
        The key of the key-value pair to remove.
  example: |
    t := {"A"=1, "B"=2}
    t.remove("A")
    >> t
    = {"B"=2}
    
Table.set:
  description: >
    Sets or updates the value associated with a specified key.
  return:
    type: 'Void'
    description: >
      Nothing.
  args:
    t:
      type: '{K=V}'
      description: >
        The reference to the table.
    key:
      type: 'K'
      description: >
        The key to set or update.
    value:
      type: 'V'
      description: >
        The value to associate with the key.
  example: |
    t := {"A"=1, "B"=2}
    t.set("C", 3)
    >> t
    = {"A"=1, "B"=2, "C"=3}