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 (
Optional[Int]
): The step increment value of the slice.
Implemented traits
AnyType
,
CollectionElementNew
,
Copyable
,
EqualityComparable
,
ExplicitlyCopyable
,
Movable
,
Representable
,
Stringable
,
Writable
Methods
__init__
__init__(out 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__(out 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__(out self, *, other: Self)
Creates a deep copy of the Slice.
Args:
- other (
Self
): The slice to copy.
__eq__
__eq__(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, 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) -> String
Gets the string representation of the span.
Returns:
The string representation of the span.
__repr__
__repr__(self) -> String
Gets the string representation of the span.
Returns:
The string representation of the span.
write_to
write_to[W: Writer](self, inout writer: W)
Write Slice string representation to a Writer
.
Parameters:
- W (
Writer
): A type conforming to the Writable trait.
Args:
- writer (
W
): The object to write to.
indices
indices(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)
i = s.indices(5) # returns (0, 4, 1)
s = slice(0, -1, 1)
i = 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)
i = s.indices(5) # returns (0, 5, 1)
s = slice(None, None, 1)
i = s.indices(5) # returns (0, 5, 1)
Out of bounds indices are clamped using the size of the container.
s = slice(20)
i = s.indices(5) # returns (0, 5, 1)
s = slice(20)
i = 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!