0

First I have a case class defined as (this is part of a library):

final case class Abstraction[A](
    result: Either[AbError, A],
    status: Int,
    info: Map[String, String]
)

I want to use it as a primary component for fold in order to go from List[IO[Abstraction[List[A]]]] to IO[Abstraction[List[A]]].

As I read I thought of making it an instance of Monoid to define the behavior to merge the final result.

My proposition is as follows but I know it has mistakes as I'm not grasping it very well.

implicit val instanceabstaction : Monoid[Abstraction[A]] = new Monoid[Abstraction[A]] = {

    def empty: Abstraction[A] = Abstraction(Either.empty[AbError, A] , 0 , Map.empty[String, String])
    def combine(x: Abstraction[A], y: Abstraction[A]): Int = x ++ y
  }

If there is any other way to achieve this, I'm open to propositions

Mar Es
  • 33
  • 4
  • As I already answered in your previous question ([deleted](https://stackoverflow.com/questions/64729804/how-to-traverse-listiofliststring-to-iofliststring-scala), copy: [Q](https://snipboard.io/e2tXkG.jpg) [A](https://snipboard.io/gnTx6O.jpg)) only you can know how to combine instances of your class. This depends on your business logic. Think how you can combine `Abstraction(either1, status1, map1)` and `Abstraction(either2, status2, map2)`. – Dmytro Mitin Nov 08 '20 at 11:11
  • Well that would be by merging the right values in Either, because stats and map are the same for them all. – Mar Es Nov 08 '20 at 11:24
  • is using a monoid instance a good approach ? – Mar Es Nov 08 '20 at 11:30
  • It's possible that `status` and `info` are the same now. But code can change. What will you return for different `status` and `info`? Will you throw exception, will you return default value etc.? – Dmytro Mitin Nov 08 '20 at 11:33
  • Monoid is a good approach if your data type **is** a Monoid. – Dmytro Mitin Nov 08 '20 at 11:33
  • It does not matter, it can be returning a default value of throwing an exception – Mar Es Nov 08 '20 at 11:36
  • If you know how to combine your instances then probably you answered your question, don't you? – Dmytro Mitin Nov 08 '20 at 11:36
  • YEs but I would need an empty instance of that case class to start foorlding, am I right? – Mar Es Nov 08 '20 at 11:39
  • 2
    I would, use **NonEmptyList** on the outside. Then, I would use `nonEmprySequence` to transform the **NonEmptyList[IO[A]]** into a **IO[NonEmptyList[A]]** then you can use `combineAll` to fold that non empty list using only a **Semigroup** – Luis Miguel Mejía Suárez Nov 08 '20 at 13:53
  • 1
    If Abstraction is a Monoid and I would use something like `input.sequence.map(_.reduce)` which also uses Traverse and Applicative (existing for List and IO). – Mateusz Kubuszok Nov 08 '20 at 15:54

0 Answers0