From dbe19ddd1fbb769f6f778dc1e9909bef70ece48f Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 8 Mar 2025 16:26:25 +0000 Subject: Support non-divergent list building --- examples/44-list-builder/shed-examples.json5 | 18 ++++++- examples/44-list-builder/src/Lists.shed | 78 +++++++++++++++++++++++++--- 2 files changed, 89 insertions(+), 7 deletions(-) (limited to 'examples') 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"); +} -- cgit v1.2.3