Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Iso involuton #869

Merged
merged 2 commits into from
Jun 23, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions core/shared/src/main/scala/monocle/Iso.scala
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,13 @@ object Iso {
/** alias for [[PIso]] id when S = T and A = B */
def id[S]: Iso[S, S] =
PIso.id[S, S]

/**
* create a [[PIso]] where S = T and A = B from
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need the first line of comment.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As in, is create a [[PIso]] where S = T and A = B redundant? I guess we could change the whole comment block to

/** create a [[Iso]] from a function that is its own inverse */

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep perfect. Maybe an [[ISO]]

* a function that is its own inverse
*/
def involuted[A](update: A => A): Iso[A, A] =
Iso(update)(update)
}

sealed abstract class IsoInstances {
Expand Down
20 changes: 20 additions & 0 deletions test/shared/src/test/scala/monocle/IsoSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ class IsoSpec extends MonocleSuite {
implicit def emptyCaseTypeEq[A] = Eq.fromUniversalEquals[EmptyCaseType[A]]

val iso = Iso[IntWrapper, Int](_.i)(IntWrapper.apply)
val involutedListReverse =
Iso.involuted[List[Int]](_.reverse) // ∀ {T} -> List(ts: T*).reverse.reverse == List(ts: T*)
val involutedTwoMinusN = Iso.involuted[Int](2 - _) // ∀ {n : Int} -> n == 2 - (2 - n)

checkAll("apply Iso", IsoTests(iso))
checkAll("GenIso", IsoTests(GenIso[IntWrapper, Int]))
Expand All @@ -49,6 +52,9 @@ class IsoSpec extends MonocleSuite {

checkAll("Iso id", IsoTests(Iso.id[Int]))

checkAll("Iso involutedListReverse", IsoTests(involutedListReverse))
checkAll("Iso involutedTwoMinusN", IsoTests(involutedTwoMinusN))

checkAll("Iso.asLens", LensTests(iso.asLens))
checkAll("Iso.asPrism", PrismTests(iso.asPrism))
checkAll("Iso.asOptional", OptionalTests(iso.asOptional))
Expand Down Expand Up @@ -122,6 +128,20 @@ class IsoSpec extends MonocleSuite {
iso.modify(_ + 1)(IntWrapper(0)) shouldEqual IntWrapper(1)
}

test("involuted") {
involutedListReverse.get(List(1, 2, 3)) shouldEqual List(3, 2, 1)
involutedListReverse.reverseGet(List(1, 2, 3)) shouldEqual List(3, 2, 1)

involutedListReverse.reverse.get(List(1, 2, 3)) shouldEqual involutedListReverse.get(List(1, 2, 3))
involutedListReverse.reverse.reverseGet(List(1, 2, 3)) shouldEqual involutedListReverse.reverseGet(List(1, 2, 3))

involutedTwoMinusN.get(5) shouldEqual -3
involutedTwoMinusN.reverseGet(5) shouldEqual -3

involutedTwoMinusN.reverse.get(5) shouldEqual involutedTwoMinusN.get(5)
involutedTwoMinusN.reverse.reverseGet(5) shouldEqual involutedTwoMinusN.reverseGet(5)
}

test("GenIso nullary equality") {
GenIso.unit[Nullary] shouldEqual _nullary
}
Expand Down