about summary refs log tree commit diff stats
path: root/html/064list.mu.html
blob: 236bf533ca40f1a975e3a950a311449662034764 (plain) (blame)
1
2
3
4
5
6
pre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .ges { font-weight: bold; font-style: italic } /* Generic.EmphStrong */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */
.highlight .no { color: #003366; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555555 } /* Name.Decorator */
.highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */
.highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */
.highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #336699 } /* Name.Variable */
.highlight .ow { color: #008800 } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */
.highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */
.highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */
.highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */
.highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */
.highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */
.highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */
.highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */
.highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */
.highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */
.highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */
.highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */
.highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */
.highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */
.highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */
.highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */
.highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */
.highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */
.highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */
.highlight .vc { color: #336699 } /* Name.Variable.Class */
.highlight .vg { color: #dd7700 } /* Name.Variable.Global */
.highlight .vi { color: #3333bb } /* Name.Variable.Instance */
.highlight .vm { color: #336699 } /* Name.Variable.Magic */
.highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
Console

   (X) #0   09/12/06  console commands
   (X) #1   09/12/06  quick find
   (X) #2   09/12/06  open with
   ( ) #3   09/12/06  MVC for widgets
   (X) #4   09/12/06  history for console
   ( ) #13  09/12/27  display docstring of a command


General

   (X) #5   09/12/06  move code from fm into objects
   (X) #6   09/12/06  move main to __init__
   (X) #7   09/12/06  cooler titlebar
   (X) #8   09/12/17  Add operations to modify files/directories
   (X) #9   09/12/24  add a widget for managing running operations
   (X) #10  09/12/24  sorting
   (X) #11  09/12/27  filter
   ( ) #12  09/12/27  jump through the list in a specific order
2' href='#n202'>202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Mu - 064list.mu</title>
<meta name="Generator" content="Vim/7.4">
<meta name="plugin-version" content="vim7.4_v2">
<meta name="syntax" content="none">
<meta name="settings" content="use_css,pre_wrap,no_foldcolumn,expand_tabs,prevent_copy=">
<meta name="colorscheme" content="minimal">
<style type="text/css">
<!--
pre { white-space: pre-wrap; font-family: monospace; color: #eeeeee; background-color: #080808; }
body { font-size: 12pt; font-family: monospace; color: #eeeeee; background-color: #080808; }
* { font-size: 12pt; font-size: 1em; }
.muRecipe { color: #ff8700; }
.muData { color: #ffff00; }
.muScenario { color: #00af00; }
.Delimiter { color: #800080; }
.Comment { color: #9090ff; }
.Constant { color: #00a0a0; }
.Special { color: #c00000; }
.muControl { color: #c0a020; }
-->
</style>

<script type='text/javascript'>
<!--

-->
</script>
</head>
<body>
<pre id='vimCodeElement'>
<span class="Comment"># A list links up multiple objects together to make them easier to manage.</span>
<span class="Comment">#</span>
<span class="Comment"># The objects must be of the same type. If you want to store multiple types in</span>
<span class="Comment"># a single list, use an exclusive-container.</span>

<span class="muData">container</span> list:_elem [
  value:_elem
  next:&amp;:list:_elem
]

<span class="muRecipe">def</span> push x:_elem, in:&amp;:list:_elem<span class="muRecipe"> -&gt; </span>result:&amp;:list:_elem [
  <span class="Constant">local-scope</span>
  <span class="Constant">load-ingredients</span>
  result<span class="Special"> &lt;- </span>new <span class="Delimiter">{</span>(list _elem): type<span class="Delimiter">}</span>
  *result<span class="Special"> &lt;- </span>merge x, in
]

<span class="muRecipe">def</span> first in:&amp;:list:_elem<span class="muRecipe"> -&gt; </span>result:_elem [
  <span class="Constant">local-scope</span>
  <span class="Constant">load-ingredients</span>
  result<span class="Special"> &lt;- </span>get *in, <span class="Constant">value:offset</span>
]

<span class="muRecipe">def</span> rest in:&amp;:list:_elem<span class="muRecipe"> -&gt; </span>result:&amp;:list:_elem/contained-in:in [
  <span class="Constant">local-scope</span>
  <span class="Constant">load-ingredients</span>
  result<span class="Special"> &lt;- </span>get *in, <span class="Constant">next:offset</span>
]

<span class="muScenario">scenario</span> list-handling [
  run [
    <span class="Constant">local-scope</span>
    x:&amp;:list:num<span class="Special"> &lt;- </span>push <span class="Constant">3</span>, <span class="Constant">0</span>
    x<span class="Special"> &lt;- </span>push <span class="Constant">4</span>, x
    x<span class="Special"> &lt;- </span>push <span class="Constant">5</span>, x
    <span class="Constant">10</span>:num/<span class="Special">raw &lt;- </span>first x
    x<span class="Special"> &lt;- </span>rest x
    <span class="Constant">11</span>:num/<span class="Special">raw &lt;- </span>first x
    x<span class="Special"> &lt;- </span>rest x
    <span class="Constant">12</span>:num/<span class="Special">raw &lt;- </span>first x
    <span class="Constant">20</span>:&amp;:list:num/<span class="Special">raw &lt;- </span>rest x
  ]
  memory-should-contain [
    <span class="Constant">10</span><span class="Special"> &lt;- </span><span class="Constant">5</span>
    <span class="Constant">11</span><span class="Special"> &lt;- </span><span class="Constant">4</span>
    <span class="Constant">12</span><span class="Special"> &lt;- </span><span class="Constant">3</span>
    <span class="Constant">20</span><span class="Special"> &lt;- </span><span class="Constant">0</span>  <span class="Comment"># nothing left</span>
  ]
]

<span class="muRecipe">def</span> length l:&amp;:list:_elem<span class="muRecipe"> -&gt; </span>result:num [
  <span class="Constant">local-scope</span>
  <span class="Constant">load-ingredients</span>
  <span class="muControl">return-unless</span> l, <span class="Constant">0</span>
  rest:&amp;:list:_elem<span class="Special"> &lt;- </span>rest l
  length-of-rest:num<span class="Special"> &lt;- </span>length rest
  result<span class="Special"> &lt;- </span>add length-of-rest, <span class="Constant">1</span>
]

<span class="Comment"># insert 'x' after 'in'</span>
<span class="muRecipe">def</span> insert x:_elem, in:&amp;:list:_elem<span class="muRecipe"> -&gt; </span>in:&amp;:list:_elem [
  <span class="Constant">local-scope</span>
  <span class="Constant">load-ingredients</span>
  new-node:&amp;:list:_elem<span class="Special"> &lt;- </span>new <span class="Delimiter">{</span>(list _elem): type<span class="Delimiter">}</span>
  *new-node<span class="Special"> &lt;- </span>put *new-node, <span class="Constant">value:offset</span>, x
  next-node:&amp;:list:_elem<span class="Special"> &lt;- </span>get *in, <span class="Constant">next:offset</span>
  *in<span class="Special"> &lt;- </span>put *in, <span class="Constant">next:offset</span>, new-node
  *new-node<span class="Special"> &lt;- </span>put *new-node, <span class="Constant">next:offset</span>, next-node
]

<span class="muScenario">scenario</span> inserting-into-list [
  run [
    <span class="Constant">local-scope</span>
    list:&amp;:list:char<span class="Special"> &lt;- </span>push <span class="Constant">3</span>, <span class="Constant">0</span>
    list<span class="Special"> &lt;- </span>push <span class="Constant">4</span>, list
    list<span class="Special"> &lt;- </span>push <span class="Constant">5</span>, list
    list2:&amp;:list:char<span class="Special"> &lt;- </span>rest list  <span class="Comment"># inside list</span>
    list2<span class="Special"> &lt;- </span>insert <span class="Constant">6</span>, list2
    <span class="Comment"># check structure</span>
    list2<span class="Special"> &lt;- </span>copy list
    <span class="Constant">10</span>:char/<span class="Special">raw &lt;- </span>first list2
    list2<span class="Special"> &lt;- </span>rest list2
    <span class="Constant">11</span>:char/<span class="Special">raw &lt;- </span>first list2
    list2<span class="Special"> &lt;- </span>rest list2
    <span class="Constant">12</span>:char/<span class="Special">raw &lt;- </span>first list2
    list2<span class="Special"> &lt;- </span>rest list2
    <span class="Constant">13</span>:char/<span class="Special">raw &lt;- </span>first list2
  ]
  memory-should-contain [
    <span class="Constant">10</span><span class="Special"> &lt;- </span><span class="Constant">5</span>  <span class="Comment"># scanning next</span>
    <span class="Constant">11</span><span class="Special"> &lt;- </span><span class="Constant">4</span>
    <span class="Constant">12</span><span class="Special"> &lt;- </span><span class="Constant">6</span>  <span class="Comment"># inserted element</span>
    <span class="Constant">13</span><span class="Special"> &lt;- </span><span class="Constant">3</span>
  ]
]

<span class="muScenario">scenario</span> inserting-at-end-of-list [
  run [
    <span class="Constant">local-scope</span>
    list:&amp;:list:char<span class="Special"> &lt;- </span>push <span class="Constant">3</span>, <span class="Constant">0</span>
    list<span class="Special"> &lt;- </span>push <span class="Constant">4</span>, list
    list<span class="Special"> &lt;- </span>push <span class="Constant">5</span>, list
    list2:&amp;:list:char<span class="Special"> &lt;- </span>rest list  <span class="Comment"># inside list</span>
    list2<span class="Special"> &lt;- </span>rest list2  <span class="Comment"># now at end of list</span>
    list2<span class="Special"> &lt;- </span>insert <span class="Constant">6</span>, list2
    <span class="Comment"># check structure like before</span>
    list2<span class="Special"> &lt;- </span>copy list
    <span class="Constant">10</span>:char/<span class="Special">raw &lt;- </span>first list2
    list2<span class="Special"> &lt;- </span>rest list2
    <span class="Constant">11</span>:char/<span class="Special">raw &lt;- </span>first list2
    list2<span class="Special"> &lt;- </span>rest list2
    <span class="Constant">12</span>:char/<span class="Special">raw &lt;- </span>first list2
    list2<span class="Special"> &lt;- </span>rest list2
    <span class="Constant">13</span>:char/<span class="Special">raw &lt;- </span>first list2
  ]
  memory-should-contain [
    <span class="Constant">10</span><span class="Special"> &lt;- </span><span class="Constant">5</span>  <span class="Comment"># scanning next</span>
    <span class="Constant">11</span><span class="Special"> &lt;- </span><span class="Constant">4</span>
    <span class="Constant">12</span><span class="Special"> &lt;- </span><span class="Constant">3</span>
    <span class="Constant">13</span><span class="Special"> &lt;- </span><span class="Constant">6</span>  <span class="Comment"># inserted element</span>
  ]
]

<span class="muScenario">scenario</span> inserting-after-start-of-list [
  run [
    <span class="Constant">local-scope</span>
    list:&amp;:list:char<span class="Special"> &lt;- </span>push <span class="Constant">3</span>, <span class="Constant">0</span>
    list<span class="Special"> &lt;- </span>push <span class="Constant">4</span>, list
    list<span class="Special"> &lt;- </span>push <span class="Constant">5</span>, list
    list<span class="Special"> &lt;- </span>insert <span class="Constant">6</span>, list
    <span class="Comment"># check structure like before</span>
    list2:&amp;:list:char<span class="Special"> &lt;- </span>copy list
    <span class="Constant">10</span>:char/<span class="Special">raw &lt;- </span>first list2
    list2<span class="Special"> &lt;- </span>rest list2
    <span class="Constant">11</span>:char/<span class="Special">raw &lt;- </span>first list2
    list2<span class="Special"> &lt;- </span>rest list2
    <span class="Constant">12</span>:char/<span class="Special">raw &lt;- </span>first list2
    list2<span class="Special"> &lt;- </span>rest list2
    <span class="Constant">13</span>:char/<span class="Special">raw &lt;- </span>first list2
  ]
  memory-should-contain [
    <span class="Constant">10</span><span class="Special"> &lt;- </span><span class="Constant">5</span>  <span class="Comment"># scanning next</span>
    <span class="Constant">11</span><span class="Special"> &lt;- </span><span class="Constant">6</span>  <span class="Comment"># inserted element</span>
    <span class="Constant">12</span><span class="Special"> &lt;- </span><span class="Constant">4</span>
    <span class="Constant">13</span><span class="Special"> &lt;- </span><span class="Constant">3</span>
  ]
]

<span class="Comment"># remove 'x' from its surrounding list 'in'</span>
<span class="Comment">#</span>
<span class="Comment"># Returns null if and only if list is empty. Beware: in that case any other</span>
<span class="Comment"># pointers to the head are now invalid.</span>
<span class="muRecipe">def</span> remove x:&amp;:list:_elem/contained-in:in, in:&amp;:list:_elem<span class="muRecipe"> -&gt; </span>in:&amp;:list:_elem [
  <span class="Constant">local-scope</span>
  <span class="Constant">load-ingredients</span>
  <span class="Comment"># if 'x' is null, return</span>
  <span class="muControl">return-unless</span> x
  next-node:&amp;:list:_elem<span class="Special"> &lt;- </span>rest x
  <span class="Comment"># clear next pointer of 'x'</span>
  *x<span class="Special"> &lt;- </span>put *x, <span class="Constant">next:offset</span>, <span class="Constant">0</span>
  <span class="Comment"># if 'x' is at the head of 'in', return the new head</span>
  at-head?:bool<span class="Special"> &lt;- </span>equal x, in
  <span class="muControl">return-if</span> at-head?, next-node
  <span class="Comment"># compute prev-node</span>
  prev-node:&amp;:list:_elem<span class="Special"> &lt;- </span>copy in
  curr:&amp;:list:_elem<span class="Special"> &lt;- </span>rest prev-node
  <span class="Delimiter">{</span>
    <span class="muControl">return-unless</span> curr
    found?:bool<span class="Special"> &lt;- </span>equal curr, x
    <span class="muControl">break-if</span> found?
    prev-node<span class="Special"> &lt;- </span>copy curr
    curr<span class="Special"> &lt;- </span>rest curr
  <span class="Delimiter">}</span>
  <span class="Comment"># set its next pointer to skip 'x'</span>
  *prev-node<span class="Special"> &lt;- </span>put *prev-node, <span class="Constant">next:offset</span>, next-node
]

<span class="muScenario">scenario</span> removing-from-list [
  run [
    <span class="Constant">local-scope</span>
    list:&amp;:list:char<span class="Special"> &lt;- </span>push <span class="Constant">3</span>, <span class="Constant">0</span>
    list<span class="Special"> &lt;- </span>push <span class="Constant">4</span>, list
    list<span class="Special"> &lt;- </span>push <span class="Constant">5</span>, list
    list2:&amp;:list:char<span class="Special"> &lt;- </span>rest list  <span class="Comment"># second element</span>
    list<span class="Special"> &lt;- </span>remove list2, list
    <span class="Constant">10</span>:bool/<span class="Special">raw &lt;- </span>equal list2, <span class="Constant">0</span>
    <span class="Comment"># check structure like before</span>
    list2<span class="Special"> &lt;- </span>copy list
    <span class="Constant">11</span>:char/<span class="Special">raw &lt;- </span>first list2
    list2<span class="Special"> &lt;- </span>rest list2
    <span class="Constant">12</span>:char/<span class="Special">raw &lt;- </span>first list2
    <span class="Constant">20</span>:&amp;:list:char/<span class="Special">raw &lt;- </span>rest list2
  ]
  memory-should-contain [
    <span class="Constant">10</span><span class="Special"> &lt;- </span><span class="Constant">0</span>  <span class="Comment"># remove returned non-null</span>
    <span class="Constant">11</span><span class="Special"> &lt;- </span><span class="Constant">5</span>  <span class="Comment"># scanning next, skipping deleted element</span>
    <span class="Constant">12</span><span class="Special"> &lt;- </span><span class="Constant">3</span>
    <span class="Constant">20</span><span class="Special"> &lt;- </span><span class="Constant">0</span>  <span class="Comment"># no more elements</span>
  ]
]

<span class="muScenario">scenario</span> removing-from-start-of-list [
  run [
    <span class="Constant">local-scope</span>
    list:&amp;:list:char<span class="Special"> &lt;- </span>push <span class="Constant">3</span>, <span class="Constant">0</span>
    list<span class="Special"> &lt;- </span>push <span class="Constant">4</span>, list
    list<span class="Special"> &lt;- </span>push <span class="Constant">5</span>, list
    list<span class="Special"> &lt;- </span>remove list, list
    <span class="Comment"># check structure like before</span>
    list2:&amp;:list:char<span class="Special"> &lt;- </span>copy list
    <span class="Constant">10</span>:char/<span class="Special">raw &lt;- </span>first list2
    list2<span class="Special"> &lt;- </span>rest list2
    <span class="Constant">11</span>:char/<span class="Special">raw &lt;- </span>first list2
    <span class="Constant">20</span>:&amp;:list:char/<span class="Special">raw &lt;- </span>rest list2
  ]
  memory-should-contain [
    <span class="Constant">10</span><span class="Special"> &lt;- </span><span class="Constant">4</span>  <span class="Comment"># scanning next, skipping deleted element</span>
    <span class="Constant">11</span><span class="Special"> &lt;- </span><span class="Constant">3</span>
    <span class="Constant">20</span><span class="Special"> &lt;- </span><span class="Constant">0</span>  <span class="Comment"># no more elements</span>
  ]
]

<span class="muScenario">scenario</span> removing-from-end-of-list [
  run [
    <span class="Constant">local-scope</span>
    list:&amp;:list:char<span class="Special"> &lt;- </span>push <span class="Constant">3</span>, <span class="Constant">0</span>
    list<span class="Special"> &lt;- </span>push <span class="Constant">4</span>, list
    list<span class="Special"> &lt;- </span>push <span class="Constant">5</span>, list
    <span class="Comment"># delete last element</span>
    list2:&amp;:list:char<span class="Special"> &lt;- </span>rest list
    list2<span class="Special"> &lt;- </span>rest list2
    list<span class="Special"> &lt;- </span>remove list2, list
    <span class="Constant">10</span>:bool/<span class="Special">raw &lt;- </span>equal list2, <span class="Constant">0</span>
    <span class="Comment"># check structure like before</span>
    list2<span class="Special"> &lt;- </span>copy list
    <span class="Constant">11</span>:char/<span class="Special">raw &lt;- </span>first list2
    list2<span class="Special"> &lt;- </span>rest list2
    <span class="Constant">12</span>:char/<span class="Special">raw &lt;- </span>first list2
    <span class="Constant">20</span>:&amp;:list:char/<span class="Special">raw &lt;- </span>rest list2
  ]
  memory-should-contain [
    <span class="Constant">10</span><span class="Special"> &lt;- </span><span class="Constant">0</span>  <span class="Comment"># remove returned non-null</span>
    <span class="Constant">11</span><span class="Special"> &lt;- </span><span class="Constant">5</span>  <span class="Comment"># scanning next, skipping deleted element</span>
    <span class="Constant">12</span><span class="Special"> &lt;- </span><span class="Constant">4</span>
    <span class="Constant">20</span><span class="Special"> &lt;- </span><span class="Constant">0</span>  <span class="Comment"># no more elements</span>
  ]
]

<span class="muScenario">scenario</span> removing-from-singleton-list [
  run [
    <span class="Constant">local-scope</span>
    list:&amp;:list:char<span class="Special"> &lt;- </span>push <span class="Constant">3</span>, <span class="Constant">0</span>
    list<span class="Special"> &lt;- </span>remove list, list
    <span class="Constant">1</span>:num/<span class="Special">raw &lt;- </span>copy list
  ]
  memory-should-contain [
    <span class="Constant">1</span><span class="Special"> &lt;- </span><span class="Constant">0</span>  <span class="Comment"># back to an empty list</span>
  ]
]

<span class="Comment"># reverse the elements of a list</span>
<span class="Comment"># (contributed by Caleb Couch)</span>
<span class="muRecipe">def</span> reverse list:&amp;:list:_elem temp:&amp;:list:_elem<span class="muRecipe"> -&gt; </span>result:&amp;:list:_elem [
  <span class="Constant">local-scope</span>
  <span class="Constant">load-ingredients</span>
  <span class="muControl">reply-unless</span> list, temp
  object:_elem<span class="Special"> &lt;- </span>first, list
  list<span class="Special"> &lt;- </span>rest list
  temp<span class="Special"> &lt;- </span>push object, temp
  result<span class="Special"> &lt;- </span>reverse list, temp
]

<span class="muScenario">scenario</span> reverse-list [
  run [
    <span class="Constant">local-scope</span>
    list:&amp;:list:number<span class="Special"> &lt;- </span>push <span class="Constant">1</span>, <span class="Constant">0</span>
    list<span class="Special"> &lt;- </span>push <span class="Constant">2</span>, list
    list<span class="Special"> &lt;- </span>push <span class="Constant">3</span>, list
    stash <span class="Constant">[list:]</span>, list
    list<span class="Special"> &lt;- </span>reverse list, <span class="Constant">0</span>
    stash <span class="Constant">[reversed:]</span>, list
  ]
  trace-should-contain [
    app: list: <span class="Constant">3</span><span class="muRecipe"> -&gt; </span><span class="Constant">2</span><span class="muRecipe"> -&gt; </span><span class="Constant">1</span>
    app: reversed: <span class="Constant">1</span><span class="muRecipe"> -&gt; </span><span class="Constant">2</span><span class="muRecipe"> -&gt; </span><span class="Constant">3</span>
  ]
]

<span class="muRecipe">def</span> to-text in:&amp;:list:_elem<span class="muRecipe"> -&gt; </span>result:text [
  <span class="Constant">local-scope</span>
  <span class="Constant">load-ingredients</span>
  buf:&amp;:buffer<span class="Special"> &lt;- </span>new-buffer <span class="Constant">80</span>
  buf<span class="Special"> &lt;- </span>to-buffer in, buf
  result<span class="Special"> &lt;- </span>buffer-to-array buf
]

<span class="Comment"># variant of 'to-text' which stops printing after a few elements (and so is robust to cycles)</span>
<span class="muRecipe">def</span> to-text-line in:&amp;:list:_elem<span class="muRecipe"> -&gt; </span>result:text [
  <span class="Constant">local-scope</span>
  <span class="Constant">load-ingredients</span>
  buf:&amp;:buffer<span class="Special"> &lt;- </span>new-buffer <span class="Constant">80</span>
  buf<span class="Special"> &lt;- </span>to-buffer in, buf, <span class="Constant">6</span>  <span class="Comment"># max elements to display</span>
  result<span class="Special"> &lt;- </span>buffer-to-array buf
]

<span class="muRecipe">def</span> to-buffer in:&amp;:list:_elem, buf:&amp;:buffer<span class="muRecipe"> -&gt; </span>buf:&amp;:buffer [
  <span class="Constant">local-scope</span>
  <span class="Constant">load-ingredients</span>
  <span class="Delimiter">{</span>
    <span class="muControl">break-if</span> in
    buf<span class="Special"> &lt;- </span>append buf, <span class="Constant">48/0</span>
    <span class="muControl">return</span>
  <span class="Delimiter">}</span>
  <span class="Comment"># append in.value to buf</span>
  val:_elem<span class="Special"> &lt;- </span>get *in, <span class="Constant">value:offset</span>
  buf<span class="Special"> &lt;- </span>append buf, val
  <span class="Comment"># now prepare next</span>
  next:&amp;:list:_elem<span class="Special"> &lt;- </span>rest in
  nextn:num<span class="Special"> &lt;- </span>copy next
  <span class="muControl">return-unless</span> next
  buf<span class="Special"> &lt;- </span>append buf, <span class="Constant">[ -&gt; ]</span>
  <span class="Comment"># and recurse</span>
  remaining:num, optional-ingredient-found?:bool<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  <span class="Delimiter">{</span>
    <span class="muControl">break-if</span> optional-ingredient-found?
    <span class="Comment"># unlimited recursion</span>
    buf<span class="Special"> &lt;- </span>to-buffer next, buf
    <span class="muControl">return</span>
  <span class="Delimiter">}</span>
  <span class="Delimiter">{</span>
    <span class="muControl">break-unless</span> remaining
    <span class="Comment"># limited recursion</span>
    remaining<span class="Special"> &lt;- </span>subtract remaining, <span class="Constant">1</span>
    buf<span class="Special"> &lt;- </span>to-buffer next, buf, remaining
    <span class="muControl">return</span>
  <span class="Delimiter">}</span>
  <span class="Comment"># past recursion depth; insert ellipses and stop</span>
  append buf, <span class="Constant">[...]</span>
]
</pre>
</body>
</html>
<!-- vim: set foldmethod=manual : -->