|
| 1 | +# @brief Get a value from a given dictionary using a key, or nil if it doesn't exist |
| 2 | +# @param _D dictionary |
| 3 | +# @param _key key to get |
| 4 | +# =begin |
| 5 | +# (let data (dict "key" "value")) |
| 6 | +# (print (dict:get data "key")) # value |
| 7 | +# =end |
| 8 | +# @author https://github.com/SuperFola |
| 9 | +(let get (fun (_D _key) (builtin__dict:get _D _key))) |
| 10 | + |
| 11 | +# @brief Adds or replaces an entry to a dictionary, given a (key, value) pair |
| 12 | +# @details The dictionary is modified in place |
| 13 | +# @param _D dictionary |
| 14 | +# @param _key key to add (or replace) |
| 15 | +# @param _value value for the given key |
| 16 | +# =begin |
| 17 | +# (let data (dict "key" "value")) |
| 18 | +# (dict:add data "hello" "world") |
| 19 | +# (dict:add data "key" "hole") # key:value will be replaced by key:hole |
| 20 | +# (print data) # {key: hole, hello: world} |
| 21 | +# =end |
| 22 | +# @author https://github.com/SuperFola |
| 23 | +(let add (fun (_D _key _value) (builtin__dict:add _D _key _value))) |
| 24 | + |
| 25 | +# @brief |
| 26 | +# @param _D dictionary |
| 27 | +# =begin |
| 28 | +# (let data (dict "key" "value")) |
| 29 | +# (print (dict:contains data "key")) # true |
| 30 | +# (print (dict:contains data "test")) # false |
| 31 | +# =end |
| 32 | +# @author https://github.com/SuperFola |
| 33 | +(let contains (fun (_D _key) (builtin__dict:contains _D _key))) |
| 34 | + |
| 35 | +# @brief Deletes an entry from a dictionary, given a key |
| 36 | +# @details The dictionary is modified in place |
| 37 | +# @param _D dictionary |
| 38 | +# @param _key key to delete |
| 39 | +# =begin |
| 40 | +# (let data (dict "key" "value")) |
| 41 | +# (dict:remove data "key") |
| 42 | +# (print (dict:get data "key")) # nil |
| 43 | +# =end |
| 44 | +# @author https://github.com/SuperFola |
| 45 | +(let remove (fun (_D _key) (builtin__dict:remove _D _key))) |
| 46 | + |
| 47 | +# @brief Returns a list of the keys of a dictionary |
| 48 | +# @param _D dictionary |
| 49 | +# =begin |
| 50 | +# (let data (dict "key" "value" 5 12)) |
| 51 | +# (print (dict:keys data)) # [key, 5] |
| 52 | +# =end |
| 53 | +# @author https://github.com/SuperFola |
| 54 | +(let keys (fun (_D) (builtin__dict:keys _D))) |
| 55 | + |
| 56 | +# @brief Computes the number of (key, value) pairs in a given dictionary |
| 57 | +# @param _D dictionary |
| 58 | +# =begin |
| 59 | +# (let data (dict "key" "value")) |
| 60 | +# (print (dict:size data)) # 1 |
| 61 | +# =end |
| 62 | +# @author https://github.com/SuperFola |
| 63 | +(let size (fun (_D) (builtin__dict:size _D))) |
| 64 | + |
| 65 | +# @brief Returns a list of the values of a dictionary |
| 66 | +# @param _D dictionary |
| 67 | +# =begin |
| 68 | +# (let data (dict "key" "value" 5 12)) |
| 69 | +# (print (dict:values data)) # [value, 12] |
| 70 | +# =end |
| 71 | +# @author https://github.com/SuperFola |
| 72 | +(let values (fun (_D) { |
| 73 | + (mut _output []) |
| 74 | + (let _keys (keys _D)) |
| 75 | + (mut _i 0) |
| 76 | + (while (< _i (len _keys)) { |
| 77 | + (append! _output (get _D (@ _keys _i))) |
| 78 | + (set _i (+ 1 _i)) }) |
| 79 | + _output })) |
| 80 | + |
| 81 | +# @brief Returns a list of the entries of a dictionary |
| 82 | +# @param _D dictionary |
| 83 | +# =begin |
| 84 | +# (let data (dict "key" "value" 5 12)) |
| 85 | +# (print (dict:entries data)) # [[key, value], [5, 12]] |
| 86 | +# =end |
| 87 | +# @author https://github.com/SuperFola |
| 88 | +(let entries (fun (_D) { |
| 89 | + (mut _output []) |
| 90 | + (let _keys (keys _D)) |
| 91 | + (mut _i 0) |
| 92 | + (while (< _i (len _keys)) { |
| 93 | + (let key (@ _keys _i)) |
| 94 | + (let val (get _D key)) |
| 95 | + (append! _output [key val]) |
| 96 | + (set _i (+ 1 _i)) }) |
| 97 | + _output })) |
| 98 | + |
| 99 | +# @brief Map each value in a dictionary with a given function |
| 100 | +# @details The original dictionary is not modified |
| 101 | +# @param _D dictionary |
| 102 | +# @param _f function to apply to each value, taking both key and value as arguments |
| 103 | +# =begin |
| 104 | +# (let data (dict "key" "value")) |
| 105 | +# (dict:map data (fun (key value) (format "{}-{}" key value))) |
| 106 | +# (print data) # {key: key-value} |
| 107 | +# =end |
| 108 | +# @author https://github.com/SuperFola |
| 109 | +(let map (fun (_D _f) { |
| 110 | + (let _new (dict)) |
| 111 | + (mut _i 0) |
| 112 | + (let _keys (keys _D)) |
| 113 | + (while (< _i (len _keys)) { |
| 114 | + (let _key (@ _keys _i)) |
| 115 | + (add _new _key (_f _key (get _D _key))) |
| 116 | + (set _i (+ 1 _i)) }) |
| 117 | + _new })) |
| 118 | + |
| 119 | +# @brief Map each value in a dictionary with a given function |
| 120 | +# @details The original dictionary is updated in place |
| 121 | +# @param _D dictionary |
| 122 | +# @param _f function to apply to each value, taking both key and value as arguments |
| 123 | +# =begin |
| 124 | +# (let data (dict "key" "value")) |
| 125 | +# (dict:map data (fun (key value) (format "{}-{}" key value))) |
| 126 | +# (print data) # {key: key-value} |
| 127 | +# =end |
| 128 | +# @author https://github.com/SuperFola |
| 129 | +(let map! (fun (_D _f) { |
| 130 | + (mut _i 0) |
| 131 | + (let _keys (keys _D)) |
| 132 | + (while (< _i (len _keys)) { |
| 133 | + (let _key (@ _keys _i)) |
| 134 | + (add _D _key (_f _key (get _D _key))) |
| 135 | + (set _i (+ 1 _i)) })})) |
| 136 | + |
| 137 | +# @brief Iterate over the pairs of a dictionary with a given function |
| 138 | +# @param _D dictionary |
| 139 | +# @param _f function to call on each pair |
| 140 | +# =begin |
| 141 | +# (let data (dict "key" "value" 5 12)) |
| 142 | +# (dict:forEach data (fun (key value) (print (format "{}-{}" key value)))) |
| 143 | +# # key-value |
| 144 | +# # 5-12 |
| 145 | +# =end |
| 146 | +# @author https://github.com/SuperFola |
| 147 | +(let forEach (fun (_D _f) { |
| 148 | + (mut _i 0) |
| 149 | + (let _keys (keys _D)) |
| 150 | + (while (< _i (len _keys)) { |
| 151 | + (let _key (@ _keys _i)) |
| 152 | + (_f _key (get _D _key)) |
| 153 | + (set _i (+ 1 _i)) })})) |
| 154 | + |
| 155 | +# @brief Filter a dictionary with a predicate |
| 156 | +# @details The original dictionary is not modified |
| 157 | +# @param _D dictionary |
| 158 | +# @param _f predicate, taking both key and value as arguments |
| 159 | +# =begin |
| 160 | +# (let data (dict "key" "value" "hello" "world")) |
| 161 | +# (let new (dict:filter data (fun (key value) (> (len key) 3)))) |
| 162 | +# (print data) # {key: value, hello: world} |
| 163 | +# (print new) # {hello: world} |
| 164 | +# =end |
| 165 | +# @author https://github.com/SuperFola |
| 166 | +(let filter (fun (_D _f) { |
| 167 | + (let _new (dict)) |
| 168 | + (mut _i 0) |
| 169 | + (let _keys (keys _D)) |
| 170 | + (while (< _i (len _keys)) { |
| 171 | + (let _key (@ _keys _i)) |
| 172 | + (let _val (get _D _key)) |
| 173 | + (if (_f _key _val) |
| 174 | + (add _new _key _val)) |
| 175 | + (set _i (+ 1 _i)) }) |
| 176 | + _new })) |
| 177 | + |
| 178 | +# @brief Filter a dictionary with a predicate |
| 179 | +# @details The original dictionary is updated in place |
| 180 | +# @param _D dictionary |
| 181 | +# @param _f predicate, taking both key and value as arguments |
| 182 | +# =begin |
| 183 | +# (let data (dict "key" "value" "hello" "world")) |
| 184 | +# (let new (dict:filter data (fun (key value) (> (len key) 3)))) |
| 185 | +# (print data) # {key: value, hello: world} |
| 186 | +# (print new) # {hello: world} |
| 187 | +# =end |
| 188 | +# @author https://github.com/SuperFola |
| 189 | +(let filter! (fun (_D _f) { |
| 190 | + (mut _i 0) |
| 191 | + (let _keys (keys _D)) |
| 192 | + (while (< _i (len _keys)) { |
| 193 | + (let _key (@ _keys _i)) |
| 194 | + (let _val (get _D _key)) |
| 195 | + (if (not (_f _key _val)) |
| 196 | + (remove _D _key)) |
| 197 | + (set _i (+ 1 _i)) }) })) |
| 198 | + |
| 199 | +# @brief Copy a dictionary |
| 200 | +# @details The original dictionary is not modified |
| 201 | +# @param _D dictionary to copy |
| 202 | +# =begin |
| 203 | +# (let data (dict "key" "value" "hello" "world")) |
| 204 | +# (let new (dict:copy data)) |
| 205 | +# (dict:add data "test" 12) |
| 206 | +# (print data) # {key: value, hello: world, test: 12} |
| 207 | +# (print new) # {key: value, hello: world} |
| 208 | +# =end |
| 209 | +# @author https://github.com/SuperFola |
| 210 | +(let copy (fun (_D) { |
| 211 | + (let _new (dict)) |
| 212 | + (let _keys (keys _D)) |
| 213 | + (mut _i 0) |
| 214 | + (while (< _i (len _keys)) { |
| 215 | + (let _key (@ _keys _i)) |
| 216 | + (add _new _key (get _D _key)) |
| 217 | + (set _i (+ 1 _i)) }) |
| 218 | + _new })) |
| 219 | + |
| 220 | +# @brief Update a dictionary with (key, value) pairs from a second dictionary |
| 221 | +# @details The original dictionary is updated in place |
| 222 | +# @param _D dictionary to update |
| 223 | +# @param _D2 second dictionary |
| 224 | +# =begin |
| 225 | +# (let data (dict "key" "value" "hello" "world")) |
| 226 | +# (let new (dict "key" "new value" 5 12)) |
| 227 | +# (dict:update data new) |
| 228 | +# (print data) # {key: new value, hello: world, 5: 12} |
| 229 | +# =end |
| 230 | +# @author https://github.com/SuperFola |
| 231 | +(let update! (fun (_D _D2) { |
| 232 | + (mut _i 0) |
| 233 | + (let _keys (keys _D2)) |
| 234 | + (while (< _i (len _keys)) { |
| 235 | + (let _key (@ _keys _i)) |
| 236 | + (add _D _key (get _D2 _key)) |
| 237 | + (set _i (+ 1 _i)) })})) |
0 commit comments