box¶
- class pythonic_fp.gadgets.box.Box¶
Bases:
GenericBox
Container holding at most one item of a given type.
Tip
Objects of the
BoxtypeAre truthy if not empty.
Can be combined with other iterators before being filled.
Can be used like a promise.
Can be used in Python match statements.
Threadsafe
- __init__(item: T) None¶
- __init__() None
Initializer
Initialize
Boxwith 0 or 1 items.- Parameters:
item – An optional initial
itemfor theBox.
- __bool__() bool¶
Bool
Truthy if not empty.
- __iter__() Iterator¶
Iterability
Iterates boxed item.
- __len__() int¶
Length
1 if
Boxcontains an item0 if
Boxis empty
- Returns:
The number of items currently in the
Box.
- __eq__(other: object) bool¶
Equality comparison
Efficiently compare
Boxto another object.- Parameters:
other – The object to be compared.
- Returns:
Trueifotheris anotherBoxand contains an object which compares as equal to the object contained in theBox, otherwiseFalse.
- __repr__() str¶
Representation string
Construct string ‘Box()’ if empty, otherwise ‘Box(item_repr)’ where
item_repr = repr(item)for the currently contained item.- Returns:
A string to reproduce the current state of the
Box.
- get() T¶
- get(alt: T) T
Get
Return the boxed 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
Pop item from
Boxif not empty.- Returns:
The item contained in the
Box.- Raises:
ValueError – If Box is empty.
- push(item: T) None¶
Push
Push an item into
Boxif empty.- Parameters:
item – Item to push into the empty
Box.- Raises:
ValueError – If
Boxis not empty.
- put(item: T) None¶
Put
Put an item in the Box. Discard any previous contents.
- exchange(new_item: T) T¶
Exchange
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.