Skip to content

Commit

Permalink
lists: more consistent names
Browse files Browse the repository at this point in the history
  • Loading branch information
fwbrasil committed Dec 8, 2023
1 parent b555eb6 commit 3893b8d
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 20 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -592,16 +592,16 @@ The `Lists` effect is designed to aid in handling and exploring multiple options
import kyo.lists._

// Evaluate each of the provided lists.
// Note how 'foreach' takes a 'List[T]'
// Note how 'get' takes a 'List[T]'
// and returns a 'T > Lists'
val a: Int > Lists =
Lists.foreach(List(1, 2, 3, 4))
Lists.get(List(1, 2, 3, 4))

// 'dropIf' discards the current choice if
// a condition is not met. Produces a 'List(1, 2)'
// since values greater than 2 are dropped
val b: Int > Lists =
a.map(v => Lists.dropIf(v > 2).map(_ => v))
a.map(v => Lists.filter(v > 2).map(_ => v))

// 'drop' unconditionally discards the
// current choice. Produces a 'List(42)'
Expand Down
8 changes: 4 additions & 4 deletions kyo-core/shared/src/main/scala/kyo/lists.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ object lists {
def loop(l: List[T], acc: List[List[U]]): U > (Lists with S) =
l match {
case Nil =>
Lists.foreach(acc.reverse.flatten: List[U])
Lists.get(acc.reverse.flatten: List[U])
case t :: ts =>
import Flat.unsafe._
Lists.run[U, S](f(t)).map(l => loop(ts, l :: acc))
Expand All @@ -29,15 +29,15 @@ object lists {
handle[T, S, Any](v)

def repeat(n: Int): Unit > Lists =
foreach(List.fill(n)(()))
get(List.fill(n)(()))

def foreach[T, S](v: List[T] > S): T > (Lists with S) =
def get[T, S](v: List[T] > S): T > (Lists with S) =
v.map {
case head :: Nil => head
case _ => suspend(v)
}

def dropIf[S](v: Boolean > S): Unit > (Lists with S) =
def filter[S](v: Boolean > S): Unit > (Lists with S) =
v.map {
case true =>
()
Expand Down
16 changes: 8 additions & 8 deletions kyo-core/shared/src/test/scala/kyoTest/listsTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,37 +9,37 @@ class listsTest extends KyoTest {

"one" in {
assert(
Lists.run(Lists.foreach(List(1)).map(_ + 1)) ==
Lists.run(Lists.get(List(1)).map(_ + 1)) ==
List(2)
)
}
"multiple" in {
assert(
Lists.run(Lists.foreach(List(1, 2, 3)).map(_ + 1)) ==
Lists.run(Lists.get(List(1, 2, 3)).map(_ + 1)) ==
List(2, 3, 4)
)
}
"nested" in {
assert(
Lists.run(Lists.foreach(List(1, 2, 3)).map(i =>
Lists.foreach(List(i * 10, i * 100))
Lists.run(Lists.get(List(1, 2, 3)).map(i =>
Lists.get(List(i * 10, i * 100))
)) ===
List(10, 100, 20, 200, 30, 300)
)
}
"drop" in {
assert(
Lists.run(Lists.foreach(List(1, 2, 3)).map(i =>
Lists.run(Lists.get(List(1, 2, 3)).map(i =>
if (i < 2) Lists.drop
else Lists.foreach(List(i * 10, i * 100))
else Lists.get(List(i * 10, i * 100))
)) ==
List(20, 200, 30, 300)
)
}
"filter" in {
assert(
Lists.run(Lists.foreach(List(1, 2, 3)).map(i =>
Lists.dropIf(i >= 2).map(_ => Lists.foreach(List(i * 10, i * 100)))
Lists.run(Lists.get(List(1, 2, 3)).map(i =>
Lists.filter(i >= 2).map(_ => Lists.get(List(i * 10, i * 100)))
)) ==
List(20, 200, 30, 300)
)
Expand Down
10 changes: 5 additions & 5 deletions kyo-direct/src/test/scala/kyoTest/directTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@ class directTest extends KyoTest {
"lists" in {
import kyo.lists._

val x = Lists.foreach(List(1, -2, -3))
val y = Lists.foreach(List("ab", "cde"))
val x = Lists.get(List(1, -2, -3))
val y = Lists.get(List("ab", "cde"))

val v: Int > Lists =
defer {
Expand All @@ -172,8 +172,8 @@ class directTest extends KyoTest {
"lists + filter" in {
import kyo.lists._

val x = Lists.foreach(List(1, -2, -3))
val y = Lists.foreach(List("ab", "cde"))
val x = Lists.get(List(1, -2, -3))
val y = Lists.get(List("ab", "cde"))

val v: Int > Lists =
defer {
Expand All @@ -183,7 +183,7 @@ class directTest extends KyoTest {
if (xx > 0) await(y).length * await(x)
else await(y).length
)
await(Lists.dropIf(r > 0))
await(Lists.filter(r > 0))
r
}

Expand Down

0 comments on commit 3893b8d

Please sign in to comment.