-
Notifications
You must be signed in to change notification settings - Fork 120
Add list slice #706
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add list slice #706
Changes from 4 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1661,10 +1661,30 @@ set of steps on each <a for=list>item</a> in order, use phrasing of the form | |
| "<a for=list>For each</a> |item| of <var ignore>list</var>", and then operate on |item| in the | ||
| subsequent prose. | ||
|
|
||
| <p>To <dfn export for=list,stack,queue,set>clone</dfn> a <a>list</a> |list| is to create a new | ||
| <a>list</a> |clone|, of the same designation, and, <a for=list>for each</a> |item| of |list|, | ||
| <a for=list>append</a> |item| to |clone|, so that |clone| <a for=list>contains</a> the same | ||
| <a for=list>items</a>, in the same order as |list|. | ||
| <div algorithm=slice> | ||
| <p>To <dfn export for=list,stack,queue,set>slice</dfn> a <a>list</a> |list|, with an optional | ||
| integer <dfn export for=list/slice><var>from</var></dfn> (default 0) and an optional integer | ||
| <dfn export for=list/slice><var>to</var></dfn> (default |list|'s <a for=list>size</a>), perform the | ||
| following steps. They return a <a>list</a> of the same designation as |list|. | ||
|
|
||
| <ol> | ||
| <li><p><a>Assert</a>: |to| is greater than or equal to |from|. | ||
|
|
||
| <li><p>If |from| is less than 0, then set |from| to 0. | ||
|
|
||
| <li><p>If |to| is greater than |list|'s <a for=list>size</a>, then set |to| to |list|'s <a for=list>size</a>. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this needed for specifications? I might not be considering all scenarios that well, but I was assuming we'd just want to require both of these to be within bounds of the list.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't mind turning these into asserts. I was just copying how JS
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's start out with a very strict contract then until we hit cases where it would make sense to not be so strict.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done! |
||
|
|
||
| <li><p>Let |slice| be a new <a>list</a> of the same designation as |list|. | ||
|
|
||
| <li><p><a for=set>For each</a> |i| of <a lt="the exclusive range">the range</a> |from| to |to|, | ||
| exclusive: <a for=list>append</a> |list|[|i|] to |slice|. | ||
|
|
||
| <li><p>Return |slice|. | ||
| </ol> | ||
| </div> | ||
|
|
||
| <p>To <dfn export for=list,stack,queue,set>clone</dfn> a <a>list</a> |list| is to return a | ||
| <a for=list>slice</a> of |list|. | ||
|
|
||
| <p class=note>This is a "shallow clone", as the <a for=list>items</a> themselves are not cloned in | ||
| any way. | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I felt this was preferable to asserting. It means, like in JS, you can slice from 0 to 100 to 'crop' a list to maximum of 100 items.