Skip to content

Commit

Permalink
Implement NonEmptyList#mapZIO (#1121)
Browse files Browse the repository at this point in the history
* implement nonemptylist mapzio

* format
  • Loading branch information
adamgfraser authored Apr 25, 2023
1 parent dd16556 commit 7234251
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion core/shared/src/main/scala/zio/prelude/NonEmptyList.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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`.
*/
Expand Down

0 comments on commit 7234251

Please sign in to comment.