Iterator slicing: Basic syntax

Take the cookiebox example from earlier. If one wanted to retrieve a few cookies from the box, one way to do that would be through iterator slicing. Take the following as an example:

cookiebox = iter(["chocolate chip", "oatmeal raisin", "sugar", 
            "shortbread", "gingerbread"])
subbox = cookiebox$[0:2]
Here, the $ operator is used with the square brackets to indicate that we want to slice the iterator, or cut a section out of it to use. Try printing both subbox and cookiebox out to see how this worked.

Show Solution:

From this we see that the first two elements of cookiebox were transferred over to subbox. However, it is also clear that the original cookiebox iterator was not preserved.

One feature of Coconut's iterator slicing that is not Present in Python is the ability to use negative indices in slicing.

a = iter("abc")
a$[-1] |> print
This would be a lot more complicated to do in Python.


Next
Previous