-
-
Notifications
You must be signed in to change notification settings - Fork 205
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
5 changed files
with
89 additions
and
29 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
--- | ||
layout: default | ||
title: "Iso" | ||
section: "optics" | ||
scaladoc: "http://julien-truffaut.github.io/Monocle/api/#monocle.PLens" | ||
source: "https://github.com/julien-truffaut/Monocle/blob/master/example/src/main/scala/monocle/example/LensExample.scala" | ||
pageSource: "https://raw.githubusercontent.com/julien-truffaut/Monocle/master/docs/src/main/tut/iso.md" | ||
--- | ||
|
||
# Iso | ||
|
||
An `Iso` is an Optic which converts elements of type `S` into elements of type | ||
`A` without loss. | ||
|
||
Consider these two case classes: | ||
|
||
```tut:silent | ||
case class Person(name: String, age: Int) | ||
case class Pers(n: String, a: Int) | ||
``` | ||
|
||
In order to create an `Iso` between `Person` and `Pers` we need to supply two total functions | ||
|
||
* get : Person => Pers | ||
* reverseGet : Pers => Person | ||
|
||
```tut:silent | ||
import monocle.Iso | ||
val personPrism = Iso[Person, Pers]((p: Person) => Pers(p.name, p.age))((p: Pers) => Person(p.n, p.a)) | ||
``` | ||
|
||
and thereby create a lossless conversion between these two types. Other usages include for example the transformation of different types of physical units. | ||
|
||
It is important to understand that the transformation between type `S` and `A` works for any type `S` and `A` and that the functions `get` and `reverseGet` are true inverses to each other. |
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