PreludeDicts.jl

APISummary
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) -> yModify 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!Function
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(_).

f takes two types of argument:

  • nothing: indicates that the slot associated with key is unoccupied.
  • key′ => value (or a similar pair-like value): indicates that the slot associated with key stores key′ => value.

f can return the following values:

  • nothing or Delete(x): indicates that the value associated with key should be deleted. Delete is useful for returning a value computed in f.

  • Some(value): sets the new value for the slot associated with key.

  • Keep(x): indicates that the slot related to key should not be modified. It is useful for returning a value computed in f.

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
source
PreludeDicts.DeleteType
Delete(data)

A value that can be returned from the function passed to modify! to indicate that the slot has to be deleted while.

source
PreludeDicts.KeepType
Keep(data)

A value that can be returned from the function passed to modify! to indicate that the slot should not be modified.

source
PreludeDicts.tryset!Function
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.

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
source
PreludeDicts.trysetwith!Function
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.

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
source
PreludeDicts.trygetFunction
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.

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
source
PreludeDicts.tryinsert!Function
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.

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
source