PreludeDicts.jl
API | Summary |
---|---|
Delete(data) | A value that can be returned from the function passed to modify! to indicate that the slot has to be deleted while. |
Keep(data) | A value that can be returned from the function passed to modify! to indicate that the slot should not be modified. |
modify!(f, dict, key) -> y | Modify the slot of dict for key using f that maps nothing or key′ => value to nothing , Some(value) , Keep(_) , or Delete(_) . |
tryget(dict, key) -> Ok(value) or Err(TypedKeyError(key)) | Look up the key and return the value wrapped in an Ok if found. Return Err(TypedKeyError(key)) otherwise. |
tryinsert!(set, x) -> Ok(x′) or Err(x′′) | Insert x if it does not exist in set and return Ok(x′) where x′ is the value just inserted to set . Otherwise, return Err(x′′) where x′′ is the value exists in set that is equivalent to x . |
tryset!(dict, key, value) -> Ok(key′ => value′) or Err(key′ => dict[key′]) | Set dict[key] = value if dict[key] does not exist and return Ok(key′ => value′) where the key′ => value′ is the key-value pair just inserted at dict[key] . Return Err(key′ => dict[key]) if dict[key] exists. |
trysetwith!(factory, dict, key) -> Ok(key′ => value′) or Err(key′ => dict[key′]) | Set dict[key] = factory() if dict[key] does not exist and return Ok(key′ => value′) where the key′ => value′ is the key-value pair just inserted at dict[key] . Return Err(key′ => dict[key]) if dict[key] exists. |
PreludeDicts.modify!
— Functionmodify!(f, dict, key) -> y
Modify the slot of dict
for key
using f
that maps nothing
or key′ => value
to nothing
, Some(value)
, Keep(_)
, or Delete(_)
.
f
takes two types of argument:
nothing
: indicates that the slot associated withkey
is unoccupied.key′ => value
(or a similar pair-like value): indicates that the slot associated withkey
storeskey′ => value
.
f
can return the following values:
nothing
orDelete(x)
: indicates that the value associated withkey
should be deleted.Delete
is useful for returning a value computed inf
.Some(value)
: sets the newvalue
for the slot associated withkey
.Keep(x)
: indicates that the slot related tokey
should not be modified. It is useful for returning a value computed inf
.
Extended help
Examples
julia> using PreludeDicts
julia> inc!(dict, key) = modify!(dict, key) do slot
if slot === nothing
Some(1)
else
Some(last(slot) + 1)
end
end;
julia> dict = Dict(:a => 111);
julia> inc!(dict, :a)
Some(112)
julia> inc!(dict, :b)
Some(1)
julia> dict == Dict(:a => 112, :b => 1)
true
PreludeDicts.Delete
— TypeDelete(data)
A value that can be returned from the function passed to modify!
to indicate that the slot has to be deleted while.
PreludeDicts.Keep
— TypeKeep(data)
A value that can be returned from the function passed to modify!
to indicate that the slot should not be modified.
PreludeDicts.tryset!
— Functiontryset!(dict, key, value) -> Ok(key′ => value′) or Err(key′ => dict[key′])
Set dict[key] = value
if dict[key]
does not exist and return Ok(key′ => value′)
where the key′ => value′
is the key-value pair just inserted at dict[key]
. Return Err(key′ => dict[key])
if dict[key]
exists.
value === value′
and/or key === key′
may not hold if value isa valtype(dict)
does not hold.
Extended help
Examples
julia> using PreludeDicts
julia> dict = Dict(:a => 111);
julia> tryset!(dict, :a, 222)
Try.Err: :a => 111
julia> tryset!(dict, :b, 222)
Try.Ok: :b => 222
julia> dict == Dict(:a => 111, :b => 222)
true
PreludeDicts.trysetwith!
— Functiontrysetwith!(factory, dict, key) -> Ok(key′ => value′) or Err(key′ => dict[key′])
Set dict[key] = factory()
if dict[key]
does not exist and return Ok(key′ => value′)
where the key′ => value′
is the key-value pair just inserted at dict[key]
. Return Err(key′ => dict[key])
if dict[key]
exists.
Extended help
Examples
julia> using PreludeDicts
julia> dict = Dict(:a => 111);
julia> trysetwith!(Returns(222), dict, :a)
Try.Err: :a => 111
julia> trysetwith!(Returns(222), dict, :b)
Try.Ok: :b => 222
julia> dict == Dict(:a => 111, :b => 222)
true
PreludeDicts.tryget
— Functiontryget(dict, key) -> Ok(value) or Err(TypedKeyError(key))
Look up the key
and return the value
wrapped in an Ok
if found. Return Err(TypedKeyError(key))
otherwise.
Extended help
Examples
julia> using PreludeDicts
julia> dict = Dict(:a => 111);
julia> tryget(dict, :a)
Try.Ok: 111
julia> tryget(dict, :b)
Try.Err: TypedKeyError: key :b not found
PreludeDicts.tryinsert!
— Functiontryinsert!(set, x) -> Ok(x′) or Err(x′′)
Insert x
if it does not exist in set
and return Ok(x′)
where x′
is the value just inserted to set
. Otherwise, return Err(x′′)
where x′′
is the value exists in set
that is equivalent to x
.
x === x′
or x === x′′
may not hold if x isa eltype(set)
does not hold.
Extended help
Examples
julia> using PreludeDicts
julia> set = Set([111]);
julia> tryinsert!(set, 111)
Try.Err: 111
julia> tryinsert!(set, 222)
Try.Ok: 222
julia> set == Set([111, 222])
true