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

Map aliases #1315

Merged
merged 7 commits into from
Aug 7, 2019
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 17 additions & 5 deletions core/shared/src/main/scala/zio/Exit.scala
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,26 @@ sealed trait Exit[+E, +A] extends Product with Serializable { self =>
final def <*>[E1 >: E, B](that: Exit[E1, B]): Exit[E1, (A, B)] = zipWith(that)((_, _), _ ++ _)

/**
* Maps over both the error and value type.
* Replaces the success value with the one provided.
*/
final def bimap[E1, A1](f: E => E1, g: A => A1): Exit[E1, A1] = mapError(f).map(g)
final def as[B](b: B): Exit[E, B] = map(_ => b)

/**
* Replaces the value with the one provided.
* Replaces the error value with the one provided.
*/
final def const[B](b: B): Exit[E, B] = map(_ => b)
final def asError[E1](e1: E1): Exit[E1, A] =
self match {
ioleo marked this conversation as resolved.
Show resolved Hide resolved
case e @ Success(_) => e
case Failure(c) => halt(c.map(_ => e1))
}

/**
* Maps over both the error and value type.
*/
final def bimap[E1, A1](f: E => E1, g: A => A1): Exit[E1, A1] = mapError(f).map(g)

@deprecated("use as", "1.0.0")
final def const[B](b: B): Exit[E, B] = as(b)

/**
* Flat maps over the value type.
Expand Down Expand Up @@ -144,7 +156,7 @@ sealed trait Exit[+E, +A] extends Product with Serializable { self =>
/**
* Discards the value.
*/
final def unit: Exit[E, Unit] = const(())
final def unit: Exit[E, Unit] = as(())

/**
* Named alias for `<*>`.
Expand Down
8 changes: 6 additions & 2 deletions core/shared/src/main/scala/zio/Fiber.scala
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,14 @@ trait Fiber[+E, +A] { self =>
def inheritFiberRefs: UIO[Unit] = self.inheritFiberRefs
}

@deprecated("use as", "1.0.0")
final def const[B](b: => B): Fiber[E, B] =
as(b)

/**
* Maps the output of this fiber to the specified constant.
*/
final def const[B](b: => B): Fiber[E, B] =
final def as[B](b: => B): Fiber[E, B] =
map(_ => b)

/**
Expand All @@ -181,7 +185,7 @@ trait Fiber[+E, +A] { self =>
/**
* Maps the output of this fiber to `()`.
*/
final def unit: Fiber[E, Unit] = const(())
final def unit: Fiber[E, Unit] = as(())

/**
* Converts this fiber into a [[scala.concurrent.Future]].
Expand Down
11 changes: 6 additions & 5 deletions core/shared/src/main/scala/zio/FunctionIO.scala
Original file line number Diff line number Diff line change
Expand Up @@ -182,22 +182,23 @@ sealed trait FunctionIO[+E, -A, +B] extends Serializable { self =>
final def |||[E1 >: E, B1 >: B, C](that: FunctionIO[E1, C, B1]): FunctionIO[E1, Either[A, C], B1] =
FunctionIO.join(self, that)

@deprecated("use as", "1.0.0")
final def const[C](c: => C): FunctionIO[E, A, C] =
as(c)

/**
* Maps the output of this effectful function to the specified constant.
*/
final def const[C](c: => C): FunctionIO[E, A, C] =
final def as[C](c: => C): FunctionIO[E, A, C] =
self >>> FunctionIO.fromFunction[B, C](_ => c)

/**
* Maps the output of this effectful function to `Unit`.
*/
@deprecated("use unit", "1.0.0")
final def void: FunctionIO[E, A, Unit] = unit

/**
* Maps the output of this effectful function to `Unit`.
*/
final def unit: FunctionIO[E, A, Unit] = const(())
final def unit: FunctionIO[E, A, Unit] = as(())

/**
* Returns a new effectful function that merely applies this one for its
Expand Down
5 changes: 4 additions & 1 deletion core/shared/src/main/scala/zio/Runtime.scala
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,10 @@ trait Runtime[+R] {
/**
* Constructs a new `Runtime` with the specified new environment.
*/
final def const[R1](r1: R1): Runtime[R1] = map(_ => r1)
final def as[R1](r1: R1): Runtime[R1] = map(_ => r1)

@deprecated("use as", "1.0.0")
final def const[R1](r1: R1): Runtime[R1] = as(r1)

/**
* Constructs a new `Runtime` with the specified executor.
Expand Down
23 changes: 15 additions & 8 deletions core/shared/src/main/scala/zio/ZIO.scala
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,16 @@ sealed trait ZIO[-R, +E, +A] extends Serializable { self =>
final def andThen[R1 >: A, E1 >: E, B](that: ZIO[R1, E1, B]): ZIO[R, E1, B] =
self >>> that

/**
* Maps the success value of this effect to the specified constant value.
*/
final def as[B](b: B): ZIO[R, E, B] = self.flatMap(new ZIO.ConstFn(() => b))

/**
* Maps the error value of this effect to the specified constant value.
*/
final def asError[E1](e1: E1): ZIO[R, E1, A] = mapError(_ => e1)

/**
* Returns an effect whose failure and success channels have been mapped by
* the specified pair of functions, `f` and `g`.
Expand Down Expand Up @@ -203,11 +213,8 @@ sealed trait ZIO[-R, +E, +A] extends Serializable { self =>

final def compose[R1, E1 >: E](that: ZIO[R1, E1, R]): ZIO[R1, E1, A] = self <<< that

/**
* Maps this effect to the specified constant while preserving the
* effects of this effect.
*/
final def const[B](b: => B): ZIO[R, E, B] = self.flatMap(new ZIO.ConstFn(() => b))
@deprecated("use as", "1.0.0")
final def const[B](b: => B): ZIO[R, E, B] = as(b)

/**
* Returns an effect that is delayed from this effect by the specified
Expand Down Expand Up @@ -1242,7 +1249,7 @@ sealed trait ZIO[-R, +E, +A] extends Serializable { self =>
/**
* Returns the effect resulting from mapping the success of this effect to unit.
*/
final def unit: ZIO[R, E, Unit] = const(())
final def unit: ZIO[R, E, Unit] = as(())

/**
* The inverse operation to `sandbox`. Submerges the full cause of failure.
Expand Down Expand Up @@ -2459,7 +2466,7 @@ object ZIO extends ZIOFunctions {

final class ZipLeftFn[R, E, A, B](override val underlying: () => ZIO[R, E, A]) extends ZIOFn1[B, ZIO[R, E, B]] {
def apply(a: B): ZIO[R, E, B] =
underlying().const(a)
underlying().as(a)
}

final class ZipRightFn[R, E, A, B](override val underlying: () => ZIO[R, E, B]) extends ZIOFn1[A, ZIO[R, E, B]] {
Expand All @@ -2471,7 +2478,7 @@ object ZIO extends ZIOFunctions {

final class TapFn[R, E, A](override val underlying: A => ZIO[R, E, _]) extends ZIOFn1[A, ZIO[R, E, A]] {
def apply(a: A): ZIO[R, E, A] =
underlying(a).const(a)
underlying(a).as(a)
}

final class MapFn[R, E, A, B](override val underlying: A => B) extends ZIOFn1[A, ZIO[R, E, B]] {
Expand Down
30 changes: 17 additions & 13 deletions core/shared/src/main/scala/zio/ZManaged.scala
Original file line number Diff line number Diff line change
Expand Up @@ -168,11 +168,15 @@ final case class ZManaged[-R, +E, +A](reserve: ZIO[R, E, Reservation[R, E, A]])
final def compose[R1, E1 >: E](that: ZManaged[R1, E1, R]): ZManaged[R1, E1, A] =
self <<< that

@deprecated("use as", "1.0.0")
final def const[B](b: => B): ZManaged[R, E, B] =
as(b)

/**
* Maps this effect to the specified constant while preserving the
* effects of this effect.
*/
final def const[B](b: => B): ZManaged[R, E, B] =
final def as[B](b: => B): ZManaged[R, E, B] =
map(_ => b)

/**
Expand Down Expand Up @@ -224,11 +228,11 @@ final case class ZManaged[-R, +E, +A](reserve: ZIO[R, E, Reservation[R, E, A]])
Reservation(
acquire = for {
resR <- reserve
.flatMap(res => finalizers.update(res.release :: _).const(res))
.flatMap(res => finalizers.update(res.release :: _).as(res))
.uninterruptible
r <- resR.acquire
resR1 <- f0(r).reserve
.flatMap(res => finalizers.update(res.release :: _).const(res))
.flatMap(res => finalizers.update(res.release :: _).as(res))
.uninterruptible
r1 <- resR1.acquire
} yield r1,
Expand Down Expand Up @@ -301,19 +305,19 @@ final case class ZManaged[-R, +E, +A](reserve: ZIO[R, E, Reservation[R, E, A]])
val direct =
ZIO.uninterruptibleMask { restore =>
reserve
.flatMap(res => finalizers.update(res.release :: _).const(res))
.flatMap(res => finalizers.update(res.release :: _).as(res))
.flatMap(res => restore(res.acquire))
}
val onFailure = (e: E) =>
ZIO.uninterruptibleMask { restore =>
failure(e).reserve
.flatMap(res => finalizers.update(res.release :: _).const(res))
.flatMap(res => finalizers.update(res.release :: _).as(res))
.flatMap(res => restore(res.acquire))
}
val onSuccess = (a: A) =>
ZIO.uninterruptibleMask { restore =>
success(a).reserve
.flatMap(res => finalizers.update(res.release :: _).const(res))
.flatMap(res => finalizers.update(res.release :: _).as(res))
.flatMap(res => restore(res.acquire))
}
direct.foldM(onFailure, onSuccess)
Expand Down Expand Up @@ -594,7 +598,7 @@ final case class ZManaged[-R, +E, +A](reserve: ZIO[R, E, Reservation[R, E, A]])
)
)
.uninterruptible
cleanup.fork.const(None).uninterruptible
cleanup.fork.as(None).uninterruptible
}
)
}
Expand All @@ -615,7 +619,7 @@ final case class ZManaged[-R, +E, +A](reserve: ZIO[R, E, Reservation[R, E, A]])
* Return unit while running the effect
*/
lazy final val unit: ZManaged[R, E, Unit] =
const(())
as(())

/**
* The inverse operation `ZManaged.sandboxed`
Expand Down Expand Up @@ -709,12 +713,12 @@ final case class ZManaged[-R, +E, +A](reserve: ZIO[R, E, Reservation[R, E, A]])
acquire = {
val left = ZIO.uninterruptibleMask { restore =>
reserve
.flatMap(res => finalizers.update(fs => res.release :: fs).const(res))
.flatMap(res => finalizers.update(fs => res.release :: fs).as(res))
.flatMap(res => restore(res.acquire))
}
val right = ZIO.uninterruptibleMask { restore =>
that.reserve
.flatMap(res => finalizers.update(fs => res.release :: fs).const(res))
.flatMap(res => finalizers.update(fs => res.release :: fs).as(res))
.flatMap(res => restore(res.acquire))
}
left.zipWithPar(right)(f0)
Expand Down Expand Up @@ -1010,7 +1014,7 @@ object ZManaged {
case (a, prom) =>
ZIO.uninterruptibleMask { restore =>
a.reserve
.flatMap(res => finalizers.update(res.release :: _).const(res))
.flatMap(res => finalizers.update(res.release :: _).as(res))
.flatMap(res => restore(res.acquire))
}.foldCauseM(
_.failureOrCause.fold(prom.fail, prom.halt),
Expand Down Expand Up @@ -1088,7 +1092,7 @@ object ZManaged {
case (a, prom) =>
ZIO.uninterruptibleMask { restore =>
a.reserve
.flatMap(res => finalizers.update(res.release :: _).const(res))
.flatMap(res => finalizers.update(res.release :: _).as(res))
.flatMap(res => restore(res.acquire))
}.foldCauseM(
_.failureOrCause.fold(prom.fail, prom.halt),
Expand All @@ -1106,7 +1110,7 @@ object ZManaged {
}
zero = ZIO.uninterruptibleMask { restore =>
a1.reserve
.flatMap(res => finalizers.update(res.release :: _).const(res))
.flatMap(res => finalizers.update(res.release :: _).as(res))
.flatMap(res => restore(res.acquire))
}
result <- proms.foldLeft[ZIO[R, E, A]](zero) { (acc, a) =>
Expand Down
17 changes: 10 additions & 7 deletions core/shared/src/main/scala/zio/ZSchedule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,11 @@ trait ZSchedule[-R, -A, +B] extends Serializable { self =>
}
}

/**
* Returns a new schedule that maps this schedule to a constant output.
*/
final def as[C](c: => C): ZSchedule[R, A, C] = map(_ => c)

/**
* A named alias for `&&`.
*/
Expand Down Expand Up @@ -232,10 +237,8 @@ trait ZSchedule[-R, -A, +B] extends Serializable { self =>
*/
final def compose[R1 <: R, C](that: ZSchedule[R1, C, A]): ZSchedule[R1, C, B] = self <<< that

/**
* Returns a new schedule that maps this schedule to a constant output.
*/
final def const[C](c: => C): ZSchedule[R, A, C] = map(_ => c)
@deprecated("use as", "1.0.0")
final def const[C](c: => C): ZSchedule[R, A, C] = as(c)

/**
* Returns a new schedule that deals with a narrower class of inputs than
Expand Down Expand Up @@ -476,7 +479,7 @@ trait ZSchedule[-R, -A, +B] extends Serializable { self =>
/**
* Returns a new schedule that maps this schedule to a Unit output.
*/
final def unit: ZSchedule[R, A, Unit] = const(())
final def unit: ZSchedule[R, A, Unit] = as(())

/**
* Returns a new schedule that continues the schedule only until the predicate
Expand Down Expand Up @@ -722,12 +725,12 @@ object ZSchedule {
/**
* A schedule that recurs forever, returning the constant for every output.
*/
final def succeed[A](a: A): Schedule[Any, A] = forever.const(a)
final def succeed[A](a: A): Schedule[Any, A] = forever.as(a)

/**
* A schedule that recurs forever, returning the constant for every output (by-name version).
*/
final def succeedLazy[A](a: => A): Schedule[Any, A] = forever.const(a)
final def succeedLazy[A](a: => A): Schedule[Any, A] = forever.as(a)

/**
* A schedule that always recurs without delay, and computes the output
Expand Down
18 changes: 13 additions & 5 deletions core/shared/src/main/scala/zio/stm/STM.scala
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,16 @@ final class STM[+E, +A] private[stm] (
final def >>=[E1 >: E, B](f: A => STM[E1, B]): STM[E1, B] =
self flatMap f

/**
* Maps the success value of this effect to the specified constant value.
*/
final def as[B](b: => B): STM[E, B] = self map (_ => b)

/**
* Maps the error value of this effect to the specified constant value.
*/
final def asError[E1](e: => E1): STM[E1, A] = self mapError (_ => e)

/**
* Simultaneously filters and maps the value produced by this effect.
*/
Expand All @@ -121,10 +131,8 @@ final class STM[+E, +A] private[stm] (
*/
final def commit: IO[E, A] = STM.atomically(self)

/**
* Maps the success value of this effect to the specified constant value.
*/
final def const[B](b: => B): STM[E, B] = self map (_ => b)
@deprecated("use as", "1.0.0")
final def const[B](b: => B): STM[E, B] = as(b)

/**
* Converts the failure channel into an `Either`.
Expand Down Expand Up @@ -261,7 +269,7 @@ final class STM[+E, +A] private[stm] (
/**
* Maps the success value of this effect to unit.
*/
final def unit: STM[E, Unit] = const(())
final def unit: STM[E, Unit] = as(())

/**
* Maps the success value of this effect to unit.
Expand Down
2 changes: 1 addition & 1 deletion docs/datatypes/io.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ object Main extends App {

// run my bracket
def run(args: List[String]) =
mybracket.orDie.const(0)
mybracket.orDie.as(0)

def closeStream(is: FileInputStream) =
UIO(is.close())
Expand Down