Skip to content
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 24 additions & 4 deletions infra.bs
Original file line number Diff line number Diff line change
Expand Up @@ -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>.
Copy link
Copy Markdown
Contributor Author

@jakearchibald jakearchibald Apr 10, 2026

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.

['foo'].slice(0, 100); // does not throw

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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 array.slice() works.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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.
Expand Down