Skip to content

Commit

Permalink
Make slice step optional
Browse files Browse the repository at this point in the history
Signed-off-by: Brian Grenier <[email protected]>
  • Loading branch information
bgreni committed Aug 25, 2024
1 parent 6477f0f commit f9fcaeb
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
10 changes: 5 additions & 5 deletions stdlib/src/builtin/builtin_slice.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ struct Slice(
"""The starting index of the slice."""
var end: Optional[Int]
"""The end index of the slice."""
var step: Int
var step: Optional[Int]
"""The step increment value of the slice."""

@always_inline("nodebug")
Expand All @@ -57,7 +57,7 @@ struct Slice(
"""
self.start = start
self.end = end
self.step = 1
self.step = None

@always_inline("nodebug")
fn __init__(
Expand All @@ -75,7 +75,7 @@ struct Slice(
"""
self.start = start
self.end = end
self.step = step.or_else(1)
self.step = step

fn __init__(inout self, *, other: Self):
"""Creates a deep copy of the Slice.
Expand Down Expand Up @@ -126,7 +126,7 @@ struct Slice(
writer.write(", ")
write_optional(self.end)
writer.write(", ")
writer.write(repr(self.step))
write_optional(self.step)
writer.write(")")

@always_inline("nodebug")
Expand Down Expand Up @@ -191,7 +191,7 @@ struct Slice(
Returns:
A tuple containing three integers for start, end, and step.
"""
var step = self.step
var step = self.step.or_else(1)

var start = self.start
var end = self.end
Expand Down
8 changes: 4 additions & 4 deletions stdlib/test/builtin/test_slice.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def test_none_end_folds():
var all_def_slice = slice(0, None, 1)
assert_equal(all_def_slice.start.value(), 0)
assert_true(all_def_slice.end is None)
assert_equal(all_def_slice.step, 1)
assert_equal(all_def_slice.step.value(), 1)


# This requires parameter inference of StartT.
Expand Down Expand Up @@ -74,11 +74,11 @@ def test_slice_stringable():
var s = SliceStringable()
assert_equal(s[2::-1], "slice(2, None, -1)")
assert_equal(s[1:-1:2], "slice(1, -1, 2)")
assert_equal(s[:-1], "slice(None, -1, 1)")
assert_equal(s[::], "slice(None, None, 1)")
assert_equal(s[:-1], "slice(None, -1, None)")
assert_equal(s[::], "slice(None, None, None)")
assert_equal(s[::4], "slice(None, None, 4)")
assert_equal(repr(slice(None, 2, 3)), "slice(None, 2, 3)")
assert_equal(repr(slice(10)), "slice(None, 10, 1)")
assert_equal(repr(slice(10)), "slice(None, 10, None)")


def test_slice_eq():
Expand Down

0 comments on commit f9fcaeb

Please sign in to comment.