Skip to content

Commit

Permalink
Add example of pull streams
Browse files Browse the repository at this point in the history
  • Loading branch information
phischu committed Nov 25, 2024
1 parent b69033d commit 0a5a021
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
4 changes: 3 additions & 1 deletion effekt/jvm/src/test/scala/effekt/StdlibTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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",
Expand Down
3 changes: 3 additions & 0 deletions examples/stdlib/stream/fuse_newlines.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ab
c
de
32 changes: 32 additions & 0 deletions examples/stdlib/stream/fuse_newlines.effekt
Original file line number Diff line number Diff line change
@@ -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()
})
}


0 comments on commit 0a5a021

Please sign in to comment.