Skip to content

Commit

Permalink
Review zipping
Browse files Browse the repository at this point in the history
  • Loading branch information
phischu committed Nov 25, 2024
1 parent 32d06bc commit b541ce7
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 30 deletions.
4 changes: 3 additions & 1 deletion examples/benchmarks/input_output/word_count_ascii.effekt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ def countWords(): Output / read[Byte] = {
var lines = 0
var wasSpace = true

exhaustively { do read[Byte]() } { c =>
exhaustively {

val c = do read[Byte]()

chars = chars + 1

Expand Down
4 changes: 3 additions & 1 deletion examples/benchmarks/input_output/word_count_utf8.effekt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ def countWords(): Output / read[Char] = {
var lines = 0
var wasSpace = true

exhaustively[Char] { do read() } { c =>
exhaustively {

val c = do read[Char]()

chars = chars + 1

Expand Down
7 changes: 2 additions & 5 deletions examples/stdlib/stream/zip.effekt
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import stream
import array

def main() = {
def stream1() = rangeFrom(1)
// Ideally this would be an array to demonstrate the capabilities.
def stream2() = ['H', 'e', 'l', 'l', 'o'].each

with val tup = for[(Int, Char)] {
zip[Int, Char] { stream1 } { stream2 }
zip[Int, Char]{stream1}{stream2} { (a, b) =>
println(show(a) ++ ", " ++ show(b))
}
val (a, b) = tup
println(show(a) ++ ", " ++ show(b))
}
34 changes: 11 additions & 23 deletions libraries/common/stream.effekt
Original file line number Diff line number Diff line change
Expand Up @@ -93,19 +93,7 @@ def boundary { program: () => Any / stop }: Unit = {
()
}

def exhaustively[A] { program: () => A / stop } { action: A => Unit }: Unit =
try {
def go(): Unit = {
action(program())
go()
}
go()
} with stop {
()
}

/// Run `program` forever until `stop` is thrown.
def exhaustively[A] { program: () => A / stop }: Unit =
def exhaustively { program: () => Any / stop }: Unit =
try {
def go(): Unit = {
program()
Expand All @@ -116,8 +104,8 @@ def exhaustively[A] { program: () => A / stop }: Unit =
()
}

// In Effekt lower bounds are inclusive and upper bounds are exclusive

/// In Effekt lower bounds are inclusive and upper bounds are exclusive
record Indexed[A](index: Int, value: A)

def range(lower: Int, upper: Int): Unit / emit[Int] =
Expand Down Expand Up @@ -279,7 +267,7 @@ def feed[R](bytes: ByteArray) { reader: () => R / read[Byte] }: R = {
}
}

def source[A, R] { stream: () => Unit / emit[A] } { reader: () => R / read[A] }: R = {
def source[A, R] { stream: () => Any / emit[A] } { reader: () => R / read[A] }: R = {
var next = box { None() }
next = box {
try {
Expand All @@ -299,21 +287,19 @@ def source[A, R] { stream: () => Unit / emit[A] } { reader: () => R / read[A] }:
}
}

def source[A] { stream: () => Unit / emit[A] } { reader: () => Any / read[A] }: Unit = {
def source[A] { stream: () => Any / emit[A] } { reader: () => Any / read[A] }: Unit = {
source[A, Any]{stream}{reader}
()
}

/// Combines two streams together producing a stream of pairs in lockstep.
/// Given two streams of length `n` and `m`, it produces a stream of length `min(n, m)`.
def zip[A, B] { stream1: () => Unit / emit[A] } { stream2: () => Unit / emit[B] }: Unit / emit[(A, B)] = {
with source[A, Unit] { stream1 }
with source[B, Unit] { stream2 }
def zip[A, B] { stream1: () => Any / emit[A] } { stream2: () => Any / emit[B] } { action: (A, B) => Any }: Unit = {
with source[A] { stream1 }
with source[B] { stream2 }

exhaustively {
val a = do read[A]()
val b = do read[B]()
do emit((a, b))
action(do read[A](), do read[B]())
}
}

Expand Down Expand Up @@ -441,7 +427,9 @@ def feed(string: String) { reader: () => Unit / read[Char] } =

def each(string: String): Unit / emit[Char] =
feed(string) {
exhaustively[Char] { do read() } { char => do emit(char) }
exhaustively {
do emit[Char](do read[Char]())
}
}

def collectString[R] { stream: () => R / emit[Char] }: (R, String) = {
Expand Down

0 comments on commit b541ce7

Please sign in to comment.