diff options
author | Kartik K. Agaram <vc@akkartik.com> | 2017-05-27 00:52:28 -0700 |
---|---|---|
committer | Kartik K. Agaram <vc@akkartik.com> | 2017-05-27 00:52:28 -0700 |
commit | 898f03cc3b8a7261104730518a6cf1b6eae9f6f5 (patch) | |
tree | 2cde7e1d29aaaf8bc8465a7339a70ad55f6216d4 /065duplex_list.mu | |
parent | aac76bca12c9fc60af15219d4d93d92699743ac1 (diff) | |
download | mu-898f03cc3b8a7261104730518a6cf1b6eae9f6f5.tar.gz |
3881 - allow students to turn sandboxes into recipes
Thanks Juan Crispin Hernandez for the suggestion.
Diffstat (limited to '065duplex_list.mu')
-rw-r--r-- | 065duplex_list.mu | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/065duplex_list.mu b/065duplex_list.mu index dea42fbc..4b16a870 100644 --- a/065duplex_list.mu +++ b/065duplex_list.mu @@ -573,3 +573,79 @@ def dump-from x:&:duplex-list:_elem [ } $print 10/newline, [---], 10/newline ] + +scenario stash-duplex-list [ + local-scope + list:&:duplex-list:num <- push 1, 0 + list <- push 2, list + list <- push 3, list + run [ + stash [list:], list + ] + trace-should-contain [ + app: list: 3 <-> 2 <-> 1 + ] +] + +def to-text in:&:duplex-list:_elem -> result:text [ + local-scope + load-ingredients + buf:&:buffer:char <- new-buffer 80 + buf <- to-buffer in, buf + result <- buffer-to-array buf +] + +# variant of 'to-text' which stops printing after a few elements (and so is robust to cycles) +def to-text-line in:&:duplex-list:_elem -> result:text [ + local-scope + load-ingredients + buf:&:buffer:char <- new-buffer 80 + buf <- to-buffer in, buf, 6 # max elements to display + result <- buffer-to-array buf +] + +def to-buffer in:&:duplex-list:_elem, buf:&:buffer:char -> buf:&:buffer:char [ + local-scope + load-ingredients + { + break-if in + buf <- append buf, [[]] + return + } + # append in.value to buf + val:_elem <- get *in, value:offset + buf <- append buf, val + # now prepare next + next:&:duplex-list:_elem <- next in + nextn:num <- copy next + return-unless next + buf <- append buf, [ <-> ] + # and recurse + remaining:num, optional-ingredient-found?:bool <- next-ingredient + { + break-if optional-ingredient-found? + # unlimited recursion + buf <- to-buffer next, buf + return + } + { + break-unless remaining + # limited recursion + remaining <- subtract remaining, 1 + buf <- to-buffer next, buf, remaining + return + } + # past recursion depth; insert ellipses and stop + append buf, [...] +] + +scenario stash-empty-duplex-list [ + local-scope + x:&:duplex-list:num <- copy 0 + run [ + stash x + ] + trace-should-contain [ + app: [] + ] +] |