Skip to content

Commit

Permalink
mapN
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefanos Stefanou committed Nov 1, 2023
1 parent 837834c commit 4e03dd7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/main/scala/mycats/examples/ValidatedExample.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import mycats.examples.common.Utils.NonEmptyList
import mycats.instances.ValidatedInstances.validatedApplicativeErrorInstance
import mycats.lib.obj.Semigroup
import mycats.lib.syntax.SemigroupalSyntax
import mycats.lib.syntax.TupleCombineOps.Tuple2SemigroupalOps
object ValidatedExample {
type ValidatedNel[A] = Validated[NonEmptyList[String],A]
val validatedNelInstance = validatedApplicativeErrorInstance[NonEmptyList[String],Long]
Expand All @@ -28,8 +29,12 @@ object ValidatedExample {
validatedNelInstance.map(product)((Expense.apply _).tupled)

}
private def mkExpenseMapN(id:Long,amount:Double):ValidatedNel[Expense] =
(expenseIdValidation(id), expenseAmountValidation(amount)).mapN((Expense.apply _).tupled)


def main(args: Array[String]): Unit = {
val valid = mkExpense(-1,-12.0)
val valid = mkExpenseMapN(-1,-12.0)
print(valid)

}
Expand Down
11 changes: 11 additions & 0 deletions src/main/scala/mycats/lib/syntax/TupleCombineOps.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package mycats.lib.syntax
import mycats.lib.morphisms.{Functor, Semigroupal}

object TupleCombineOps {

implicit class Tuple2SemigroupalOps[F[_],A,B](ab:Tuple2[F[A],F[B]]){
def mapN[C](f:Tuple2[A,B]=>C)(implicit semigroupalWithFunctor:Semigroupal[F] with Functor[F]): F[C] =
semigroupalWithFunctor.map[(A,B),C](semigroupalWithFunctor.product[A,B](ab._1,ab._2))(f)
}

}

0 comments on commit 4e03dd7

Please sign in to comment.