3

I have a simple method:

def process(element: SomeData): ZIO[Any, Nothing, Unit]

I would like to use it on every element from list:

def processElements(list: List[SomeData]): ZIO[Any, Nothing, Unit] = {
  list.foreach(element => 
    process(element)
  )
}

And this method returns Unit, so I added ZIO.succeed():

def processElements(list: List[SomeData]): ZIO[Any, Nothing, Unit] = {
      ZIO.succeed(list.foreach(element => 
        process(element)
      ))
    }

I am not sure if it good approach to do it. Is it possible that this code will not be done or return errors? Is there better way to convert Unit into ZIO[Any, Nothing, Unit]? Also I am not sure if this foreach method is ok to use here, but I have no idea how I should process all elements from list in other way.

Developus
  • 1,400
  • 2
  • 14
  • 50
  • 6
    You want what is called `traverse_` but since **ZIO** believes that using standard names is bad because you need to study like two hours to understand it, they provide this functionality under a more familiar name `foreach_` but AFAIK they do not provide it as an extension method on collections but rather as a helper method on the `ZIO` object, so your final code would be: `ZIO.foreach_(list)(process)` – Luis Miguel Mejía Suárez May 20 '21 at 19:47

0 Answers0