Box type

A box which might (FullBox) or might not (EmptyBox) contain a value. Box is somewhat similar to Arr of zero (EmptyBox) or exactly one (FullBox) element. This analogy helps understanding each() and filter() operation.

Direct parent types

Eachable1
Eachable which each() calls the callback with one argument
Direct subtypes: 16

Direct children types

EmptyBox
Represents absence of a value
FullBox
Represents presence of a value
Direct subtypes: 8

Constructors

Box(x:Any) Source: stdlib.ngs:2786
Convert anything to Box (always FullBox)

Parameters

xvalue to enclose in a box

Returns

FullBox with given value

Example

Box(7).map(X*2).get()  # 14
Box(n:Null) Source: stdlib.ngs:2791
Convert null to Box (always EmptyBox)

Returns

EmptyBox

Example

Box(null).map(X*2).get()  # InvalidArgument exception (in "get()")
Box(a:Arr, idx:Any) Source: stdlib.ngs:2802
Convert array value indexed by the given index

Parameters

idxkey to look in hash

Returns

Box. FullBox if the array has the element indexed by idx, EmptyBox otherwise.

Example

my_array = [10, 20]
my_array.Box(1).map(X*2).each(echo)
# output: 40
my_array.Box(5).map(X*2).each(echo)
# no output
Box(h:Hash, k:Any) Source: stdlib.ngs:2820
Convert hash value indexed by the given key to a Box

Parameters

kkey to look in hash

Returns

Box. FullBox if the hash has the element referenced by k, EmptyBox otherwise.

Example

my_hash = {"a": 10, "b": 300}
my_hash.Box("a").map(X*2).each(echo)
# output: 20
my_hash.Box("nope").map(X*2).each(echo)
# no output
Box(my:MatchY) Source: stdlib.ngs:5274
Convert successful match to FullBox

Returns

FullBox

Example

Box("abcd" ~ /^(..)/).map({"First two letters: ${A[1]}"}).get("(too short)")  # First two letters: ab
Box(mn:MatchN) Source: stdlib.ngs:5279
Convert failed match to EmptyBox

Returns

EmptyBox

Example

Box("a" ~ /^(..)/).map({"First two letters: ${A[1]}"}).get("(too short)")  # (too short)

Methods

==(b1:Box, b2:Box) Source: stdlib.ngs:2856
Compare boxes. Empty boxes and boxes with same content are considered to be equal.

Returns

Bool

Example

EmptyBox() == EmptyBox()
EmptyBox() != FullBox(1)
FullBox(1) == FullBox(1)
FullBox(1) != FullBox(2)
any(b:Box, predicate:Any) Source: stdlib.ngs:2750
Test Box value

Example

Box(5).any(Int)      # true
Box(5).any(Str)      # false
EmptyBox().any(Int)  # false
ExitCode(b:Box) Source: stdlib.ngs:2707
Zero for FullBox and one for EmptyBox
none(b:Box, predicate:Any) Source: stdlib.ngs:2760
Test Box value

Example

Box(5).none(Int)      # false
Box(5).none(Str)      # true
EmptyBox().none(Int)  # true