Skip to content

Instantly share code, notes, and snippets.

@long-long-float
Last active November 18, 2020 08:03
Show Gist options
  • Save long-long-float/bddd5cbdf8a76c7a49b38870713e7811 to your computer and use it in GitHub Desktop.
Save long-long-float/bddd5cbdf8a76c7a49b38870713e7811 to your computer and use it in GitHub Desktop.
Generator program on Effekt
module generator
import immutable/option
import immutable/list
effect Replace(x: Int): Unit
effect Reject(): Unit
effect Yield(x: Int): Unit / { Replace, Reject }
def iter(lst: List[Int]): List[Int] / { Yield, Console } = lst match {
case Nil() => Nil()
case Cons(x, xs) =>
val xp = try {
do Yield(x)
Some(x)
}
with Replace { xp => Some(xp) }
with Reject { None() }
val xsp = iter(xs)
xp match {
case None() => xsp
case Some(xpp) => Cons(xpp, xsp)
}
}
def main() = {
val lst = [0, 1, 3, 0 - 2, 0 - 8, 9]
val lst2 = try { iter(lst) }
with Yield { x =>
resume { if (x < 0) do Reject() else do Replace(x * 2) }
}
println(lst2)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment