erg/crates/erg_compiler/lib/core.d/Dict!.d.er
Shunsuke Shibayama 365d661c37 feat!: add dict type methods
delete `remove!` method (add `pop!` instead)
2024-12-27 17:18:55 +09:00

69 lines
2.3 KiB
Python

# TODO: transition specifications
dict = pyimport "Dict"
.Dict!: ClassType
.Dict! <: dict.Dict
.Dict!.
'''erg
dic = !{"a": 1}
dic.insert!("b", 2)
assert dic == {"a": 1, "b": 2}
'''
insert!: |K, V|(self: RefMut(.Dict!(K, V)), key: K, value: V) => NoneType
'''erg
dic = !{"a": 1}
x = dic.pop!("a")
assert dic == {}
assert x == 1
'''
pop!: |K, V|(self: RefMut(.Dict!(K, V)), key: K, default := V) => V or NoneType
'''
Update the dictionary with the key-value pairs in `other`.
If `conflict_resolver` is specified, it will be called with the two values and store as a value when there is a conflict.
Otherwise, the value in `other` will be used.
'''
'''erg
dic = !{"a": 1}
dic.update!({"b": 2})
dic.update!([("c", 3)])
assert dic == {"a": 1, "b": 2, "c": 3}
dic.update!({"b": 3}, confilct_resolver := (x, y) -> x + y)
assert dic == {"a": 1, "b": 5, "c": 3}
'''
update!: |K, V|(self: RefMut(.Dict!(K, V)), other: Iterable([K, V]), confilct_resolver := (V, V) -> V) => NoneType
'''
Merge two dictionaries.
If `conflict_resolver` is specified, it will be called with the two values and store as a value when there is a conflict.
Otherwise, the value in `other` will be used.
'''
'''erg
dic = !{"a": 1}
dic.merge!({"b": 2})
assert dic == {"a": 1, "b": 2}
dic.merge!({"b": 3}, confilct_resolver := (x, y) -> x + y)
assert dic == {"a": 1, "b": 5}
'''
merge!: |K, V|(self: RefMut(.Dict!(K, V)), other: .Dict!(K, V), confilct_resolver := (V, V) -> V) => NoneType
'''
Remove and return a (key, value) pair as a 2-tuple.
Pairs are returned in LIFO (last-in, first-out) order.
Raises KeyError if the dict is empty.
'''
'''erg
dic = !{"a": 1}
assert dic.popitem!() == ("a", 1)
'''
popitem!: |K, V|(self: RefMut(.Dict!(K, V))) => (K, V)
'''
Insert key with a value of default if key is not in the dictionary.
Return the value for key if key is in the dictionary, else default.
'''
'''erg
dic = !{"a": 1}
assert dic.setdefault!("b", 2) == 2
assert dic.setdefault!("a", 3) == 1
assert dic == {"a": 1, "b": 2}
'''
setdefault!: |K, V|(self: RefMut(.Dict!(K, V)), key: K, default: V) => V