aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2025-03-08 16:26:25 +0000
committerMichael Williamson <mike@zwobble.org>2025-03-08 16:26:25 +0000
commitdbe19ddd1fbb769f6f778dc1e9909bef70ece48f (patch)
tree9dcfc0dfb90fbe9ee03bba9870036d740a5ce951 /examples
parente1d05f02ab7c865d752d38d0611b95583876bd8b (diff)
Support non-divergent list building
Diffstat (limited to 'examples')
-rw-r--r--examples/44-list-builder/shed-examples.json518
-rw-r--r--examples/44-list-builder/src/Lists.shed78
2 files changed, 89 insertions, 7 deletions
diff --git a/examples/44-list-builder/shed-examples.json5 b/examples/44-list-builder/shed-examples.json5
index b669a2e6..e6cc5e44 100644
--- a/examples/44-list-builder/shed-examples.json5
+++ b/examples/44-list-builder/shed-examples.json5
@@ -3,7 +3,23 @@
{
project: ".",
bin: "lists",
- expectedOutput: "buildList()\nhello world\n\nbuildSameListTwice()\nhello world\nhello world\n",
+ expectedOutput: "\
+buildList()\n\
+hello world\n\
+\n\
+buildSameListTwice()\n\
+hello world\n\
+hello world\n\
+\n\
+buildListThenExtend()\n\
+hellohello world\n\
+\n\
+extendThenBuildPrefixAndList()\n\
+hellohello world\n\
+\n\
+buildListThenPrefix()\n\
+hello world\n\
+hello",
},
],
}
diff --git a/examples/44-list-builder/src/Lists.shed b/examples/44-list-builder/src/Lists.shed
index 7744e8d8..c6d01df5 100644
--- a/examples/44-list-builder/src/Lists.shed
+++ b/examples/44-list-builder/src/Lists.shed
@@ -3,14 +3,17 @@ import Std/Lists;
pub fun main() ! Io -> Unit {
buildList!();
buildSameListTwice!();
+ buildListThenExtend!();
+ extendThenBuildPrefixAndList!();
+ buildListThenPrefix!();
}
fun buildList() ! Io -> Unit {
print!("buildList()\n");
- val builder = Lists.listBuilder[String]();
- builder.add("hello");
- builder.add(" world\n");
+ var builder = Lists.listBuilder[String]();
+ assign builder = builder.add("hello");
+ assign builder = builder.add(" world\n");
val list = builder.build();
@@ -24,9 +27,9 @@ fun buildList() ! Io -> Unit {
fun buildSameListTwice() ! Io -> Unit {
print!("buildSameListTwice()\n");
- val builder = Lists.listBuilder[String]();
- builder.add("hello");
- builder.add(" world\n");
+ var builder = Lists.listBuilder[String]();
+ assign builder = builder.add("hello");
+ assign builder = builder.add(" world\n");
val list1 = builder.build();
val list2 = builder.build();
@@ -41,3 +44,66 @@ fun buildSameListTwice() ! Io -> Unit {
print!("\n");
}
+
+fun buildListThenExtend() ! Io -> Unit {
+ print!("buildListThenExtend()\n");
+
+ var builder1 = Lists.listBuilder[String]();
+ assign builder1 = builder1.add("hello");
+ val list1 = builder1.build();
+
+ val builder2 = builder1.add(" world\n");
+ val list2 = builder2.build();
+
+ list1.forEach!(fn (element) ! Io {
+ print!(element);
+ });
+
+ list2.forEach!(fn (element) ! Io {
+ print!(element);
+ });
+
+ print!("\n");
+}
+
+fun extendThenBuildPrefixAndList() ! Io -> Unit {
+ print!("extendThenBuildPrefixAndList()\n");
+
+ var builder1 = Lists.listBuilder[String]();
+ assign builder1 = builder1.add("hello");
+ val builder2 = builder1.add(" world\n");
+
+ val list1 = builder1.build();
+ val list2 = builder2.build();
+
+ list1.forEach!(fn (element) ! Io {
+ print!(element);
+ });
+
+ list2.forEach!(fn (element) ! Io {
+ print!(element);
+ });
+
+ print!("\n");
+}
+
+fun buildListThenPrefix() ! Io -> Unit {
+ print!("buildListThenPrefix()\n");
+
+ var builder1 = Lists.listBuilder[String]();
+ assign builder1 = builder1.add("hello");
+ val builder2 = builder1.add(" world\n");
+
+ val list1 = builder2.build();
+ val list2 = builder1.build();
+
+ list1.forEach!(fn (element) ! Io {
+ print!(element);
+ });
+
+ list2.forEach!(fn (element) ! Io {
+ print!(element);
+ });
+
+ print!("\n");
+}