mirror of
https://github.com/roc-lang/roc.git
synced 2025-11-02 22:01:20 +00:00
parent
04bceae956
commit
96e12aa790
2 changed files with 30 additions and 0 deletions
|
|
@ -9,6 +9,7 @@ interface Dict
|
||||||
insert,
|
insert,
|
||||||
len,
|
len,
|
||||||
remove,
|
remove,
|
||||||
|
update,
|
||||||
contains,
|
contains,
|
||||||
keys,
|
keys,
|
||||||
values,
|
values,
|
||||||
|
|
@ -122,6 +123,34 @@ remove = \@Dict list, key ->
|
||||||
|> List.dropLast
|
|> List.dropLast
|
||||||
|> @Dict
|
|> @Dict
|
||||||
|
|
||||||
|
## Insert or remove a value in a Dict based on its presence
|
||||||
|
update :
|
||||||
|
Dict k v,
|
||||||
|
k,
|
||||||
|
([Present v, Missing] -> [Present v, Missing])
|
||||||
|
-> Dict k v
|
||||||
|
update = \dict, key, alter ->
|
||||||
|
possibleValue =
|
||||||
|
get dict key
|
||||||
|
|> Result.map Present
|
||||||
|
|> Result.withDefault Missing
|
||||||
|
|
||||||
|
when alter possibleValue is
|
||||||
|
Present value -> insert dict key value
|
||||||
|
Missing -> remove dict key
|
||||||
|
|
||||||
|
## Internal for testing only
|
||||||
|
alterValue : [Present Bool, Missing] -> [Present Bool, Missing]
|
||||||
|
alterValue = \possibleValue ->
|
||||||
|
when possibleValue is
|
||||||
|
Missing -> Present Bool.false
|
||||||
|
Present value if Bool.not value -> Present Bool.true
|
||||||
|
Present _ -> Missing
|
||||||
|
|
||||||
|
expect update empty "a" alterValue == single "a" Bool.false
|
||||||
|
expect update (single "a" Bool.false) "a" alterValue == single "a" Bool.true
|
||||||
|
expect update (single "a" Bool.true) "a" alterValue == empty
|
||||||
|
|
||||||
contains : Dict k v, k -> Bool
|
contains : Dict k v, k -> Bool
|
||||||
contains = \@Dict list, needle ->
|
contains = \@Dict list, needle ->
|
||||||
step = \_, Pair key _val ->
|
step = \_, Pair key _val ->
|
||||||
|
|
|
||||||
|
|
@ -1410,6 +1410,7 @@ define_builtins! {
|
||||||
|
|
||||||
15 DICT_WITH_CAPACITY: "withCapacity"
|
15 DICT_WITH_CAPACITY: "withCapacity"
|
||||||
16 DICT_CAPACITY: "capacity"
|
16 DICT_CAPACITY: "capacity"
|
||||||
|
17 DICT_UPDATE: "update"
|
||||||
}
|
}
|
||||||
9 SET: "Set" => {
|
9 SET: "Set" => {
|
||||||
0 SET_SET: "Set" exposed_type=true // the Set.Set type alias
|
0 SET_SET: "Set" exposed_type=true // the Set.Set type alias
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue