box¶
- class pythonic_fp.gadgets.box.Box(item: T)¶
- class pythonic_fp.gadgets.box.Box
Container holding at most one item of a given type.
Note
Box(item: T): contains at one item of typeTBox[T](): creates empty container
Where type
Tis some definite type, which could beNoneor evenNever.Tip
Boxobjects can be used in Python match statements.- Parameters:
item – An optional initial contained
itemfor theBox.
- __init__(item: T) None¶
- __init__() None
- Parameters:
item – An optional initial contained
itemfor theBox.
- __repr__() str¶
Return repr(self).
- __eq__(other: object) bool¶
Efficiently compare to another object.
- Parameters:
other – The object to be compared with,
- Returns:
Trueifotheris of type Box and contains an object which compares as equal to the object contained in theBox, otherwiseFalse.
- get() T¶
- get(alt: T) T
Return the contained item, if it exists, otherwise an alternate item, if given.
- Parameters:
alt – An optional item of type
Tto return if theBoxis empty.- Returns:
Contents of
Boxor an alternate item, if given, when theBoxis empty.- Raises:
ValueError – When the
altitem is not provided but needed.
- pop() T¶
Pop the contained item if
Boxis not empty.- Returns:
The item contained in the
Box.- Raises:
ValueError – If Box is empty.
- push(item: T) None¶
Push an item into an empty
Box.- Parameters:
item – Item to push into the empty
Box.- Raises:
ValueError – If
Boxis not empty.
- put(item: T) None¶
Put an item in the Box. Discard any previous contents.
- exchange(new_item: T) T¶
Exchange an item with what is in the Box.
- Parameters:
new_item – New item to exchange for current item.
- Returns:
Original contents of the
Box.- Raises:
ValueError – If Box is empty.