Skip to content

Commit

Permalink
Arity and productN
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefanos Stefanou committed Nov 2, 2023
1 parent f5840fc commit ee7feac
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 23 deletions.
7 changes: 1 addition & 6 deletions src/main/scala/mycats/instances/OptionInstances.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,7 @@ object OptionInstances {
}
}
implicit val optionalApplyInstance:Apply[Option] = new Apply[Option] {
override def product[A, B](a: Option[A], b: Option[B]): Option[(A, B)] = {
(a,b) match {
case (Some(a),Some(b))=>Some((a,b))
case _ => None
}
}
override def product[A, B](a: Option[A], b: Option[B]): Option[(A, B)] = optionalSemigroupalInstance.product(a,b)
override def ap[A, B](ff: Option[A => B])(fa: Option[A]): Option[B] = ???
override def map[A, B](fa: Option[A])(f: A => B): Option[B] = fa match {
case Some(value) => Some(f(value))
Expand Down
4 changes: 2 additions & 2 deletions src/main/scala/mycats/lib/syntax/ApplySyntax.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import mycats.lib.morphisms.{Apply, Semigroupal}

object ApplySyntax {
implicit class applyAritySyntaxOps[F[_],A](fa:F[A]){
def product[B](fb:F[B])(implicit semigroupalF:Semigroupal[F]) = semigroupalF.product(fa,fb)
def product[B,C](fb:F[B],fc:F[C])(implicit apply:Apply[F]) = apply.product2(fa,fb,fc)
def productN[B](fb:F[B])(implicit semigroupalF:Semigroupal[F]) = semigroupalF.product(fa,fb)
def productN[B,C](fb:F[B],fc:F[C])(implicit apply:Apply[F]) = apply.product2(fa,fb,fc)
}
}
22 changes: 7 additions & 15 deletions src/test/scala/mycats/lib/ApplyAritySpec.scala
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
package mycats.lib

import mycats.instances.OptionInstances.optionalSemigroupalInstance
import mycats.lib.syntax.SemigroupalSyntax._
import mycats.instances.OptionInstances.optionalApplyInstance
import mycats.lib.morphisms.Apply
import mycats.lib.morphisms.arity.ApplyArityFunctions
import mycats.instances.OptionInstances.optionalApplyInstance
import mycats.lib.syntax.ApplySyntax.applyAritySyntaxOps
//There is some sources

class ApplyAritySpec extends org.scalatest.funsuite.AnyFunSuite {
test("Apply Law : Arity 2 : Product should be associative") {
// implicit val he:Apply[Option] = optionalApplyInstance
// val opt1:Option[Int]=Some(32)
// val opt2:Option[String]=Some("Hello world")
// val opt3:Option[Double]=Some(42.42)
//
// val left = applyAritySyntaxOps(opt1).product(opt2)(he)
// val right = opt2.product(opt2,opt3)(he)
//
// assert(optionalApplyInstance.pro(opt1,opt2,opt3)) ,"Semigroupal is not associative?")
val opt1:Option[Int]=Some(32)
val opt2:Option[String]=Some("Hello world")
val opt3:Option[Double]=Some(42.42)
//assert((opt1.productN(opt2)).productN(opt3) == opt1.productN(opt2.productN(opt3)) ,"Semigroupal is not associative?")
//wait for answer https://medium.com/@PerrottaFrancisco/semigroupal-parallel-applicative-in-cats-6df527b853da
}
}

0 comments on commit ee7feac

Please sign in to comment.