From dfe957149e632a5eef44478ce8e6fbc6fdab0f1b Mon Sep 17 00:00:00 2001 From: Jake Archibald Date: Thu, 9 Apr 2026 11:01:24 +0100 Subject: [PATCH 1/5] Add slice --- infra.bs | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/infra.bs b/infra.bs index 800f0c2..c1f46c0 100644 --- a/infra.bs +++ b/infra.bs @@ -1661,13 +1661,15 @@ set of steps on each item in order, use phrasing of the form "For each |item| of list", and then operate on |item| in the subsequent prose. -

To clone a list |list| is to create a new -list |clone|, of the same designation, and, for each |item| of |list|, -append |item| to |clone|, so that |clone| contains the same -items, in the same order as |list|. - -

This is a "shallow clone", as the items themselves are not cloned in -any way. +

To slice a list |list|, with an optional index +from (default 0) and an optional index +to (default |list|'s size), is to create a new +list |slice|, of the same designation, and append each +item of |list|, where the index is greater than or equal to |from| and less than +|to|, to |slice|, maintaining order. + +

To clone a list |list| is to return a +slice of |list|.

Let |original| be the ordered set « "a", "b", "c" ». Cloning |original| creates From ce2df871f35b6b7cf3157c9c5ea77a973e66fec1 Mon Sep 17 00:00:00 2001 From: Jake Archibald Date: Thu, 9 Apr 2026 12:58:36 +0100 Subject: [PATCH 2/5] Restore note --- infra.bs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/infra.bs b/infra.bs index c1f46c0..6188213 100644 --- a/infra.bs +++ b/infra.bs @@ -1671,6 +1671,9 @@ subsequent prose.

To clone a list |list| is to return a slice of |list|. +

This is a "shallow clone", as the items themselves are not cloned in +any way. +

Let |original| be the ordered set « "a", "b", "c" ». Cloning |original| creates a new ordered set |clone|, so that replacing "a" with From 816b8b4d157ce086e95c3f8700bf45d8a920a773 Mon Sep 17 00:00:00 2001 From: Jake Archibald Date: Fri, 10 Apr 2026 11:26:57 +0100 Subject: [PATCH 3/5] Expand slice algorithm --- infra.bs | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/infra.bs b/infra.bs index 6188213..f27a749 100644 --- a/infra.bs +++ b/infra.bs @@ -1661,12 +1661,23 @@ set of steps on each item in order, use phrasing of the form "For each |item| of list", and then operate on |item| in the subsequent prose. +

To slice a list |list|, with an optional index from (default 0) and an optional index -to (default |list|'s size), is to create a new -list |slice|, of the same designation, and append each -item of |list|, where the index is greater than or equal to |from| and less than -|to|, to |slice|, maintaining order. +to (default |list|'s size), perform the +following steps. They return a list of the same designation as |list|. + +

    +
  1. Assert: |to| is greater than or equal to |from|. + +

  2. Let |slice| be a new list of the same designation as |list|. + +

  3. For each |i| of the range |from| to |to|, + exclusive: append |list|[|i|] to |slice|. + +

  4. Return |slice|. +

+

To clone a list |list| is to return a slice of |list|. From 5dfaeb9aa98939f3740c278a4d4a8a2494824fe2 Mon Sep 17 00:00:00 2001 From: Jake Archibald Date: Fri, 10 Apr 2026 14:51:04 +0100 Subject: [PATCH 4/5] Handle out of bound values --- infra.bs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/infra.bs b/infra.bs index f27a749..bdce510 100644 --- a/infra.bs +++ b/infra.bs @@ -1662,14 +1662,18 @@ set of steps on each item in order, use phrasing of the form subsequent prose.

-

To slice a list |list|, with an optional index -from (default 0) and an optional index +

To slice a list |list|, with an optional +integer from (default 0) and an optional integer to (default |list|'s size), perform the following steps. They return a list of the same designation as |list|.

  1. Assert: |to| is greater than or equal to |from|. +

  2. If |from| is less than 0, then set |from| to 0. + +

  3. If |to| is greater than |list|'s size, then set |to| to |list|'s size. +

  4. Let |slice| be a new list of the same designation as |list|.

  5. For each |i| of the range |from| to |to|, From f6c8e610825fe94f4e62577c23a60b0611cad939 Mon Sep 17 00:00:00 2001 From: Jake Archibald Date: Tue, 14 Apr 2026 09:22:59 +0100 Subject: [PATCH 5/5] Asserts rather than clamping --- infra.bs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/infra.bs b/infra.bs index bdce510..54c6bda 100644 --- a/infra.bs +++ b/infra.bs @@ -1668,11 +1668,11 @@ integer from (default 0) and an opti following steps. They return a list of the same designation as |list|.

      -
    1. Assert: |to| is greater than or equal to |from|. +

    2. Assert: |from| is greater than or equal to 0. -

    3. If |from| is less than 0, then set |from| to 0. +

    4. Assert: |to| is less than or equal to |list|'s size. -

    5. If |to| is greater than |list|'s size, then set |to| to |list|'s size. +

    6. Assert: |to| is greater than or equal to |from|.

    7. Let |slice| be a new list of the same designation as |list|.