-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #101 from sagifogel/addition_of_polymorphic_partsOf
addition of unsafePartsOf
- Loading branch information
Showing
13 changed files
with
242 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,9 @@ | ||
package proptics.instances | ||
|
||
import proptics.internal.{CorepresentableInstances, SellableInstances} | ||
import proptics.internal.{Bazaar, CorepresentableInstances, Sellable, SellableInstances} | ||
import proptics.profunctor.Corepresentable.Aux | ||
|
||
trait PartsOf extends SellableInstances with CorepresentableInstances | ||
trait PartsOf extends SellableInstances with CorepresentableInstances { | ||
implicit def partsOfSellable[A](implicit ev: Aux[* => *, Bazaar[* => *, List[A], List[A], Unit, *]]): Sellable[* => *, Bazaar[* => *, *, *, Unit, *]] = | ||
sellableBazaar[* => *, Bazaar[* => *, List[A], List[A], Unit, *]] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 18 additions & 6 deletions
24
core/shared/src/main/scala/proptics/internal/Corepresentable.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,38 @@ | ||
package proptics.internal | ||
|
||
import cats.arrow.Profunctor | ||
import cats.data.State | ||
import cats.instances.function._ | ||
import cats.{Applicative, Id} | ||
|
||
import proptics.profunctor.{Corepresentable => Corep} | ||
|
||
trait CorepresentableInstances { | ||
implicit def corepresentableBazaar[C, D]: Corep.Aux[* => *, Bazaar[* => *, C, D, Unit, *]] = new Corep[* => *] { | ||
implicit def corepresentableBazaar[C, D]: Corep.Aux[* => *, Bazaar[* => *, List[C], List[D], Unit, *]] = new Corep[* => *] { | ||
override def P: Profunctor[* => *] = Profunctor[* => *] | ||
|
||
override type Corepresentation[x] = Bazaar[* => *, C, D, Unit, x] | ||
override type Corepresentation[x] = Bazaar[* => *, List[C], List[D], Unit, x] | ||
|
||
override def cotabulate[A, B](f: Bazaar[* => *, C, D, Unit, A] => B): A => B = a => { | ||
f(Applicative[Bazaar[* => *, C, D, Unit, *]].pure(a)) | ||
override def cotabulate[A, B](f: Bazaar[* => *, List[C], List[D], Unit, A] => B): A => B = a => { | ||
f(Applicative[Bazaar[* => *, List[C], List[D], Unit, *]].pure(a)) | ||
} | ||
|
||
override def cosieve[A, B](pab: A => B)(fa: Bazaar[* => *, C, D, Unit, A]): B = { | ||
val res = fa.runBazaar.apply[Id](c => pab(c.asInstanceOf[A]).asInstanceOf[D])(()) | ||
override def cosieve[A, B](pab: A => B)(fa: Bazaar[* => *, List[C], List[D], Unit, A]): B = { | ||
val res = fa.runBazaar.apply[Id](list => list.map(c => pab(c.asInstanceOf[A]).asInstanceOf[D]))(()) | ||
pab(res) | ||
} | ||
} | ||
|
||
implicit def corepresentableState[C]: Corep.Aux[* => *, State[List[C], *]] = new Corep[* => *] { | ||
override def P: Profunctor[* => *] = Profunctor[* => *] | ||
|
||
override type Corepresentation[x] = State[List[C], x] | ||
|
||
override def cotabulate[A, B](f: State[List[C], A] => B): A => B = a => f(State.pure[List[C], A](a)) | ||
|
||
override def cosieve[A, B](pab: A => B)(fa: State[List[C], A]): B = | ||
fa.map(pab).runEmptyA.value | ||
} | ||
} | ||
|
||
object Corepresentable extends CorepresentableInstances |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package optics | ||
|
||
import proptics.instances.partsOf._ | ||
import proptics.specs.PropticsSuite | ||
import proptics.std.tuple._1P | ||
import proptics.{ATraversal_, Lens_, Traversal_} | ||
|
||
class UnsafePartsOfExamples extends PropticsSuite { | ||
val traversedTuple1: Traversal_[List[(String, Int)], List[(Boolean, Int)], String, Boolean] = | ||
Traversal_.fromTraverse[List, (String, Int), (Boolean, Int)] compose | ||
_1P[String, Boolean, Int] | ||
val traversedTuple2: ATraversal_[List[(String, Int)], List[(Boolean, Int)], String, Boolean] = | ||
ATraversal_.fromTraverse[List, (String, Int), (Boolean, Int)] compose | ||
_1P[String, Boolean, Int] | ||
val unsafePartsOfFromTraversal: Lens_[List[(String, Int)], List[(Boolean, Int)], List[String], List[Boolean]] = | ||
traversedTuple1.unsafePartsOf | ||
val unsafePartsOfFromATraversal: Lens_[List[(String, Int)], List[(Boolean, Int)], List[String], List[Boolean]] = | ||
traversedTuple2.unsafePartsOf | ||
|
||
test("unsafePartsOf from Traversal can view focuses as List") { | ||
val target = List("A", "B", "C").zipWithIndex | ||
unsafePartsOfFromTraversal.view(target) shouldEqual List("A", "B", "C") | ||
} | ||
|
||
test("unsafePartsOf from ATraversal can view focuses as List") { | ||
val target = List("A", "B", "C").zipWithIndex | ||
unsafePartsOfFromATraversal.view(target) shouldEqual List("A", "B", "C") | ||
} | ||
|
||
test("unsafePartsOf from Traversal can set the lens to a list to replace the corresponding elements") { | ||
val target = List("A", "B", "C").zipWithIndex | ||
unsafePartsOfFromTraversal.set(List(true, false, true))(target) shouldEqual | ||
List((true, 0), (false, 1), (true, 2)) | ||
} | ||
|
||
test("unsafePartsOf from ATraversal can set the lens to a list to replace the corresponding elements") { | ||
val target = List("A", "B", "C").zipWithIndex | ||
unsafePartsOfFromATraversal.set(List(true, false, true))(target) shouldEqual | ||
List((true, 0), (false, 1), (true, 2)) | ||
} | ||
|
||
test("unsafePartsOf from Traversal ignores extra elements") { | ||
val target = List("A", "B", "C").zipWithIndex | ||
val replaceList = List(true, false, true, false, true, false, true) | ||
|
||
unsafePartsOfFromTraversal.set(replaceList)(target) shouldEqual | ||
List((true, 0), (false, 1), (true, 2)) | ||
} | ||
|
||
test("unsafePartsOf from ATraversal ignores extra elements") { | ||
val target = List("A", "B", "C").zipWithIndex | ||
val replaceList = List(true, false, true, false, true, false, true) | ||
|
||
unsafePartsOfFromATraversal.set(replaceList)(target) shouldEqual | ||
List((true, 0), (false, 1), (true, 2)) | ||
} | ||
|
||
test("unsafePartsOf from Traversal crashes, when setting the lens with a wrong number of list elements") { | ||
val target = List("A", "B", "C").zipWithIndex | ||
val thrown = intercept[IllegalArgumentException] { | ||
unsafePartsOfFromTraversal.set(List(true, false))(target) | ||
} | ||
|
||
thrown.getMessage shouldEqual "Not enough elements were supplied" | ||
} | ||
|
||
test("unsafePartsOf from ATraversal crashes, when setting the lens with a wrong number of list elements") { | ||
val target = List("A", "B", "C").zipWithIndex | ||
val thrown = intercept[IllegalArgumentException] { | ||
unsafePartsOfFromATraversal.set(List(true, false))(target) | ||
} | ||
|
||
thrown.getMessage shouldEqual "Not enough elements were supplied" | ||
} | ||
|
||
test("unsafePartsOf from Traversal is safer when using over") { | ||
val target = List("A", "B", "C").zipWithIndex | ||
val replaceList = List(true, false) | ||
val result = unsafePartsOfFromATraversal.over { list => | ||
list.zipWithIndex.map { case (_, i) => replaceList.lift(i).getOrElse(true) } | ||
} | ||
|
||
result(target) shouldEqual List((true, 0), (false, 1), (true, 2)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.