about summary refs log tree commit diff stats
path: root/065duplex_list.mu
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2017-05-27 00:52:28 -0700
committerKartik K. Agaram <vc@akkartik.com>2017-05-27 00:52:28 -0700
commit898f03cc3b8a7261104730518a6cf1b6eae9f6f5 (patch)
tree2cde7e1d29aaaf8bc8465a7339a70ad55f6216d4 /065duplex_list.mu
parentaac76bca12c9fc60af15219d4d93d92699743ac1 (diff)
downloadmu-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.mu76
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: []
+  ]
+]