Skip to content
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 cloning for maps #265

Merged
merged 2 commits into from
Aug 30, 2019
Merged
Changes from all 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
18 changes: 17 additions & 1 deletion infra.bs
Original file line number Diff line number Diff line change
Expand Up @@ -982,7 +982,8 @@ subsequent prose.
<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|.

Note: This is a "shallow clone", as the <a for=list>items</a> themselves are not cloned in any way.
<p class=note>This is a "shallow clone", as the <a for=list>items</a> themselves are not cloned in
any way.

<p class=example id=example-list-clone>Let |original| be the <a>ordered set</a> «
"<code>a</code>", "<code>b</code>", "<code>c</code>" ». <a for=set>Cloning</a> |original| creates
Expand Down Expand Up @@ -1164,6 +1165,21 @@ a set of steps on each <a for=map>entry</a> in order, use phrasing of the form
"<a for=map>For each</a> |key| → |value| of |map|", and then operate on |key| and |value| in the
subsequent prose.

<p>To <dfn export for=map>clone</dfn> an <a>ordered map</a> |map| is to create a new
<a>ordered map</a> |clone|, and, <a for=map>for each</a> |key| → |value| of |map|,
<a for=map>set</a> |clone|[|key|] to |value|.

<p class=note>This is a "shallow clone", as the <a for=map>keys</a> and <a for=map>values</a>
themselves are not cloned in any way.

<p class=example id=example-map-clone>Let |original| be the <a>ordered map</a> «[
"<code>a</code>" → «1, 2, 3», "<code>b</code>" → «» ]». <a for=set>Cloning</a> |original| creates a
new <a>ordered map</a> |clone|, so that <a for=map>setting</a> |clone|["<code>a</code>"] to
«-1, -2, -3» gives «[ "<code>a</code>" → «-1, -2, -3», "<code>b</code>" → «» ]» and leaves
|original| unchanged. However, <a for=list>appending</a> 4 to |clone|["<code>b</code>"] will modify
the corresponding <a for=map>value</a> in both |clone| and |original|, as they both point to the
same <a>list</a>.

<p>To <dfn export for=map lt="sort in ascending order|sorting in ascending order|sort|sorting">sort in ascending order</dfn>
a <a>map</a> |map|, with a less than algorithm |lessThanAlgo|, is to create a new <a>map</a>
|sorted|, containing the same <a for=map>entries</a> as |map| but sorted so that according to
Expand Down