Mojo struct
Slice
Represents a slice expression.
Objects of this type are generated when slice syntax is used within square brackets, e.g.:
var msg: String = "Hello Mojo"
# Both are equivalent and print "Mojo".
print(msg[6:])
print(msg.__getitem__(Slice(6, len(msg))))
var msg: String = "Hello Mojo"
# Both are equivalent and print "Mojo".
print(msg[6:])
print(msg.__getitem__(Slice(6, len(msg))))
Fieldsβ
- βstart (
Optional[Int]
): The starting index of the slice. - βend (
Optional[Int]
): The end index of the slice. - βstep (
Int
): The step increment value of the slice.
Implemented traitsβ
AnyType
,
CollectionElementNew
,
Copyable
,
EqualityComparable
,
ExplicitlyCopyable
,
Formattable
,
Movable
,
Representable
,
Stringable
Methodsβ
__init__
β
__init__(inout self: Self, start: Int, end: Int)
Construct slice given the start and end values.
Args:
- βstart (
Int
): The start value. - βend (
Int
): The end value.
__init__(inout self: Self, start: Optional[Int], end: Optional[Int], step: Optional[Int])
Construct slice given the start, end and step values.
Args:
- βstart (
Optional[Int]
): The start value. - βend (
Optional[Int]
): The end value. - βstep (
Optional[Int]
): The step value.
__init__(inout self: Self, *, other: Self)
Creates a deep copy of the Slice.
Args:
- βother (
Self
): The slice to copy.
__eq__
β
__eq__(self: Self, other: Self) -> Bool
Compare this slice to the other.
Args:
- βother (
Self
): The slice to compare to.
Returns:
True if start, end, and step values of this slice match the corresponding values of the other slice and False otherwise.
__ne__
β
__ne__(self: Self, other: Self) -> Bool
Compare this slice to the other.
Args:
- βother (
Self
): The slice to compare to.
Returns:
False if start, end, and step values of this slice match the corresponding values of the other slice and True otherwise.
__str__
β
__str__(self: Self) -> String
Gets the string representation of the span.
Returns:
The string representation of the span.
__repr__
β
__repr__(self: Self) -> String
Gets the string representation of the span.
Returns:
The string representation of the span.
format_to
β
format_to(self: Self, inout writer: Formatter)
Write Slice string representation to a Formatter
.
Args:
- βwriter (
Formatter
): The formatter to write to.
indices
β
indices(self: Self, length: Int) -> Tuple[Int, Int, Int]
Returns a tuple of 3 integers representing the start, end, and step of the slice if applied to a container of the given length.
Uses the target container length to normalize negative, out of bounds, or None indices.
Negative indices are wrapped using the length of the container.
s = slice(0, -1, 1)
s.indices(5) # returns (0, 4, 1)
s = slice(0, -1, 1)
s.indices(5) # returns (0, 4, 1)
None indices are defaulted to the start or the end of the container
based on whether step
is positive or negative.
s = slice(None, None, 1)
s.indices(5) # returns (0, 5, 1)
s = slice(None, None, 1)
s.indices(5) # returns (0, 5, 1)
Out of bounds indices are clamped using the size of the container.
s = slice(20)
s.indices(5) # returns (0, 5, 1)
s = slice(20)
s.indices(5) # returns (0, 5, 1)
Args:
- βlength (
Int
): The length of the target container.
Returns:
A tuple containing three integers for start, end, and step.
Was this page helpful?
Thank you! We'll create more content like this.
Thank you for helping us improve!
π What went wrong?