about summary refs log tree commit diff stats
path: root/archive/2.vm/continuation5.mu
diff options
context:
space:
mode:
authorKartik Agaram <vc@akkartik.com>2019-07-27 16:01:55 -0700
committerKartik Agaram <vc@akkartik.com>2019-07-27 17:47:59 -0700
commit6e1eeeebfb453fa7c871869c19375ce60fbd7413 (patch)
tree539c4a3fdf1756ae79770d5c4aaf6366f1d1525e /archive/2.vm/continuation5.mu
parent8846a7f85cc04b77b2fe8a67b6d317723437b00c (diff)
downloadmu-6e1eeeebfb453fa7c871869c19375ce60fbd7413.tar.gz
5485 - promote SubX to top-level
Diffstat (limited to 'archive/2.vm/continuation5.mu')
-rw-r--r--archive/2.vm/continuation5.mu49
1 files changed, 49 insertions, 0 deletions
diff --git a/archive/2.vm/continuation5.mu b/archive/2.vm/continuation5.mu
new file mode 100644
index 00000000..295cb9c9
--- /dev/null
+++ b/archive/2.vm/continuation5.mu
@@ -0,0 +1,49 @@
+# Example program showing that a 'paused' continuation can be 'resumed' with
+# inputs.
+#
+# Print out a list of numbers, first adding 0 to the first, 1 to the second, 2
+# to the third, and so on.
+#
+# To run:
+#   $ git clone https://github.com/akkartik/mu
+#   $ cd mu
+#   $ ./mu continuation5.mu
+#
+# Expected output:
+#   1
+#   3
+#   5
+
+def main [
+  local-scope
+  l:&:list:num <- copy null
+  l <- push 3, l
+  l <- push 2, l
+  l <- push 1, l
+  k:continuation, x:num, done?:bool <- call-with-continuation-mark 100/mark, create-yielder, l
+  a:num <- copy 1
+  {
+    break-if done?
+    $print x 10/newline
+    k, x:num, done?:bool <- call k, a  # resume; x = a + next l value
+    a <- add a, 1
+    loop
+  }
+]
+
+def create-yielder l:&:list:num -> n:num, done?:bool [
+  local-scope
+  load-inputs
+  a:num <- copy 0
+  {
+    done? <- equal l, null
+    break-if done?
+    n <- first l
+    l <- rest l
+    n <- add n, a
+    a <- return-continuation-until-mark 100/mark, n, done?  # pause/resume
+    loop
+  }
+  return-continuation-until-mark 100/mark, -1, done?
+  assert false, [called too many times, ran out of continuations to return]
+]