From 723425154620a9aa8c8dd5297b6107d0ec96a347 Mon Sep 17 00:00:00 2001 From: Adam Fraser Date: Tue, 25 Apr 2023 12:23:13 +0100 Subject: [PATCH] Implement NonEmptyList#mapZIO (#1121) * implement nonemptylist mapzio * format --- .../main/scala/zio/prelude/NonEmptyList.scala | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/core/shared/src/main/scala/zio/prelude/NonEmptyList.scala b/core/shared/src/main/scala/zio/prelude/NonEmptyList.scala index 4cc149e2a..25f2914f6 100644 --- a/core/shared/src/main/scala/zio/prelude/NonEmptyList.scala +++ b/core/shared/src/main/scala/zio/prelude/NonEmptyList.scala @@ -16,9 +16,9 @@ package zio.prelude -import zio.NonEmptyChunk import zio.prelude.NonEmptyList._ import zio.prelude.newtypes.{Max, Min, Prod, Sum} +import zio.{NonEmptyChunk, ZIO} import scala.annotation.tailrec import scala.language.implicitConversions @@ -245,6 +245,22 @@ sealed trait NonEmptyList[+A] { self => final def map[B](f: A => B): NonEmptyList[B] = reduceMapRight(a => single(f(a)))((a, bs) => cons(f(a), bs)) + /** + * Effectfully maps the elements of this `NonEmptyList`. + */ + final def mapZIO[R, E, B](f: A => ZIO[R, E, B]): ZIO[R, E, NonEmptyList[B]] = + ZIO + .foreach[R, E, A, B, Iterable](self)(f) + .map(iterable => NonEmptyList.fromIterable(iterable.head, iterable.tail)) + + /** + * Effectfully maps the elements of this `NonEmptyList` in parallel. + */ + final def mapZIOPar[R, E, B](f: A => ZIO[R, E, B]): ZIO[R, E, NonEmptyList[B]] = + ZIO + .foreachPar[R, E, A, B, Iterable](self)(f) + .map(iterable => NonEmptyList.fromIterable(iterable.head, iterable.tail)) + /** * Returns the maximum element in this `NonEmptyList`. */