Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor 'stream', add a few examples #705

Merged
merged 10 commits into from
Nov 25, 2024
2 changes: 2 additions & 0 deletions examples/stdlib/stream/fibonacci.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
The first 10 Fibonacci numbers:
Cons(0, Cons(1, Cons(1, Cons(2, Cons(3, Cons(5, Cons(8, Cons(13, Cons(21, Cons(34, Nil()))))))))))
20 changes: 20 additions & 0 deletions examples/stdlib/stream/fibonacci.effekt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import stream

def main() = {
val max = 10

val fibs = collectList[Int] {
var a = 0
var b = 1

replicate(max) {
val current = a
val next = a + b
a = b
b = next
current
}
}
println("The first " ++ show(max) ++ " Fibonacci numbers:")
println(fibs)
}
35 changes: 35 additions & 0 deletions examples/stdlib/stream/neighbours.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
The immediate neighbours of [4, 3] are:
[3, 2]
[3, 3]
[3, 4]
[4, 2]
[4, 4]
[5, 2]
[5, 3]
[5, 4]

The neighbours and their neighbours of [4, 3] are:
[2, 1]
[2, 2]
[2, 3]
[2, 4]
[2, 5]
[3, 1]
[3, 2]
[3, 3]
[3, 4]
[3, 5]
[4, 1]
[4, 2]
[4, 4]
[4, 5]
[5, 1]
[5, 2]
[5, 3]
[5, 4]
[5, 5]
[6, 1]
[6, 2]
[6, 3]
[6, 4]
[6, 5]
38 changes: 38 additions & 0 deletions examples/stdlib/stream/neighbours.effekt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import stream

record Pos(x: Int, y: Int)

def equals(left: Pos, right: Pos) = (left, right) match {
case (Pos(lx, ly), Pos(rx, ry)) => lx == rx && ly == ry
}

def show(p: Pos) = "[" ++ show(p.x) ++ ", " ++ show(p.y) ++ "]"

/// Gets the neighbours of a given position.
/// with radius=1, those are immediate neighbours (including the diagonal)
/// with radius=2, these are neighbours&their neighbours
/// ...
def neighboursOf(pos: Pos, radius: Int) = {
with val dx = for[Int] { range(neg(radius), radius + 1) }
with val dy = for[Int] { range(neg(radius), radius + 1) }
val newPosition = Pos(pos.x + dx, pos.y + dy)
do emit(newPosition)
}
jiribenes marked this conversation as resolved.
Show resolved Hide resolved

def main() = {
val start = Pos(4, 3)

println("The immediate neighbours of " ++ show(start) ++ " are:")
for[Pos] { start.neighboursOf(1) } {
case p and not(p.equals(start)) => println(show(p))
case _ => ()
}

println("")

println("The neighbours and their neighbours of " ++ show(start) ++ " are:")
for[Pos] { start.neighboursOf(2) } {
case p and not(p.equals(start)) => println(show(p))
case _ => ()
}
}
2 changes: 2 additions & 0 deletions examples/stdlib/stream/sum_of_squares.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
The sum of squares from 1 to 10 is:
385
12 changes: 12 additions & 0 deletions examples/stdlib/stream/sum_of_squares.effekt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import stream

def squares(max: Int): Unit / emit[Int] = {
with val n = for[Int] { range(1, max) }
do emit(n * n)
}

def main() = {
val max = 10
println("The sum of squares from 1 to " ++ show(max) ++ " is:")
println(sum { squares(max + 1) })
}
jiribenes marked this conversation as resolved.
Show resolved Hide resolved