From c6e22028de6206ac68508c3717ef42226253b046 Mon Sep 17 00:00:00 2001 From: Philipp Schuster Date: Mon, 25 Nov 2024 13:50:59 +0100 Subject: [PATCH] Add example of pull streams --- .../src/test/scala/effekt/StdlibTests.scala | 4 ++- examples/stdlib/stream/fuse_newlines.check | 3 ++ examples/stdlib/stream/fuse_newlines.effekt | 32 +++++++++++++++++++ libraries/common/stream.effekt | 1 + 4 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 examples/stdlib/stream/fuse_newlines.check create mode 100644 examples/stdlib/stream/fuse_newlines.effekt diff --git a/effekt/jvm/src/test/scala/effekt/StdlibTests.scala b/effekt/jvm/src/test/scala/effekt/StdlibTests.scala index 451cf9c5b..f74cb0e21 100644 --- a/effekt/jvm/src/test/scala/effekt/StdlibTests.scala +++ b/effekt/jvm/src/test/scala/effekt/StdlibTests.scala @@ -24,7 +24,8 @@ abstract class StdlibChezTests extends StdlibTests { // Not implemented yet examplesDir / "stdlib" / "bytearray", examplesDir / "stdlib" / "io", - examplesDir / "stdlib" / "stream" / "characters.effekt" + examplesDir / "stdlib" / "stream" / "characters.effekt", + examplesDir / "stdlib" / "stream" / "fuse_newlines.effekt" ) } class StdlibChezSchemeMonadicTests extends StdlibChezTests { @@ -46,6 +47,7 @@ class StdlibLLVMTests extends StdlibTests { // Valgrind leak/failure examplesDir / "stdlib" / "bytearray" / "bytearray.effekt", examplesDir / "stdlib" / "stream" / "characters.effekt", + examplesDir / "stdlib" / "stream" / "fuse_newlines.effekt", examplesDir / "stdlib" / "io" / "filesystem" / "async_file_io.effekt", examplesDir / "stdlib" / "io" / "filesystem" / "files.effekt", examplesDir / "stdlib" / "io" / "filesystem" / "wordcount.effekt", diff --git a/examples/stdlib/stream/fuse_newlines.check b/examples/stdlib/stream/fuse_newlines.check new file mode 100644 index 000000000..252ac0b51 --- /dev/null +++ b/examples/stdlib/stream/fuse_newlines.check @@ -0,0 +1,3 @@ +ab +c +de \ No newline at end of file diff --git a/examples/stdlib/stream/fuse_newlines.effekt b/examples/stdlib/stream/fuse_newlines.effekt new file mode 100644 index 000000000..3c050c347 --- /dev/null +++ b/examples/stdlib/stream/fuse_newlines.effekt @@ -0,0 +1,32 @@ +import stream + +def fuseNewlines(): Nothing / {read[Char], emit[Char], stop} = { + val c = do read[Char]() + if (c == '\n') { + do emit(c) + skipNewlines() + } else { + do emit(c) + fuseNewlines() + } +} + +def skipNewlines(): Nothing / {read[Char], emit[Char], stop} = { + val c = do read[Char]() + if (c == '\n') { + skipNewlines() + } else { + do emit(c) + fuseNewlines() + } +} + +def main() = { + with feed("ab\n\nc\nde") + println(collectString { + with exhaustively + fuseNewlines() + }) +} + + diff --git a/libraries/common/stream.effekt b/libraries/common/stream.effekt index 9fd79dec0..6337c1f1c 100644 --- a/libraries/common/stream.effekt +++ b/libraries/common/stream.effekt @@ -93,6 +93,7 @@ def boundary { program: () => Any / stop }: Unit = { () } +/// Run `program` forever until `stop` is thrown. def exhaustively { program: () => Any / stop }: Unit = try { def go(): Unit = {