Skip to content
This repository has been archived by the owner on Oct 1, 2024. It is now read-only.

Commit

Permalink
Merge pull request johnhungerford#3 from getkyo/main
Browse files Browse the repository at this point in the history
Add `.unless` extension
  • Loading branch information
johnhungerford authored Apr 18, 2024
2 parents 7fdd8e5 + b66ae5d commit e4ff157
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
10 changes: 5 additions & 5 deletions zikyo-core/shared/src/main/scala/zikyo/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,8 @@ package object zikyo:
def fromAutoCloseable[A <: AutoCloseable, S](closeable: => A < S): A < (S & Resources) =
acquireRelease(closeable)(c => IOs(c.close()))

def fromEither[E, A](either: Either[E, A])(using Tag[Aborts[E]]): A < (Aborts[E]) =
val aborts = Aborts[E]
println("HI")
aborts.get(either)
end fromEither
def fromEither[E, A](either: Either[E, A])(using Tag[Aborts[E]]): A < Aborts[E] =
Aborts[E].get(either)

def fromFuture[A: Flat, S](future: => Future[A] < S): A < (S & Fibers) =
future.map(f => Fibers.fromFuture(f))
Expand Down Expand Up @@ -341,6 +338,9 @@ package object zikyo:
def tap[S1](f: A => Any < S1): A < (S & S1) =
effect.map(a => f(a).as(a))

def unless[S1](condition: Boolean < S1): A < (S & S1 & Options) =
condition.map(c => if c then Options.empty else effect)

end extension

extension [A, S, E](effect: A < (S & Aborts[E]))
Expand Down
16 changes: 16 additions & 0 deletions zikyo-core/shared/src/test/scala/zikyoTest/effectsTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,22 @@ class effectsTest extends ZiKyoTest:
assert(handledEffectWhen2.pure == Some(false))
}

"unless" in {
val effect = IOs("value").unless(Envs[Boolean].get)

def runEffect(b: Boolean) =
IOs.run {
Envs[Boolean].run(b) {
Options.run {
effect
}
}
}.pure

assert(runEffect(true) == None)
assert(runEffect(false) == Some("value"))
}

"tap" in {
val effect: Int < IOs = IOs(42).tap(v => assert(42 == v))
val handled = IOs.run(effect)
Expand Down

0 comments on commit e4ff157

Please sign in to comment.