From 4175cece7129941a369e78e37e062daeeec4bc2a Mon Sep 17 00:00:00 2001 From: Adam Hearn <22334119+hearnadam@users.noreply.github.com> Date: Wed, 17 Apr 2024 08:32:16 -0700 Subject: [PATCH] add: `.unless` extension - remove debug println from `fromEither` --- .../shared/src/main/scala/zikyo/package.scala | 10 +++++----- .../src/test/scala/zikyoTest/effectsTest.scala | 16 ++++++++++++++++ 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/zikyo-core/shared/src/main/scala/zikyo/package.scala b/zikyo-core/shared/src/main/scala/zikyo/package.scala index bc3de06..bc0b916 100644 --- a/zikyo-core/shared/src/main/scala/zikyo/package.scala +++ b/zikyo-core/shared/src/main/scala/zikyo/package.scala @@ -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)) @@ -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])) diff --git a/zikyo-core/shared/src/test/scala/zikyoTest/effectsTest.scala b/zikyo-core/shared/src/test/scala/zikyoTest/effectsTest.scala index a582269..ac14734 100644 --- a/zikyo-core/shared/src/test/scala/zikyoTest/effectsTest.scala +++ b/zikyo-core/shared/src/test/scala/zikyoTest/effectsTest.scala @@ -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)