Created
May 26, 2020 07:44
-
-
Save fancellu/f13381ea61113238077677431276daae to your computer and use it in GitHub Desktop.
Example usage of Cats Align
This file contains hidden or 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
| import cats._ | |
| import cats.data._ | |
| import cats.syntax._ | |
| import cats.implicits._ | |
| object CatsAlign extends App { | |
| val alOption=Align[Option] | |
| val iorO: Option[Ior[Int, Int]] =alOption.align(Option(1), Option(2)) | |
| println(iorO) | |
| val iorO2: Option[Ior[Int, Nothing]] =alOption.align(Option(1), None) | |
| println(iorO2) | |
| val iorO3: Option[Ior[Nothing, Nothing]] =alOption.align(None, None) | |
| println(iorO3) | |
| val alList=Align[List] | |
| val l1=List(1,2) | |
| val l2=List(10,11,12) | |
| val iorList: List[Ior[Int, Int]] =alList.align(l1, l2) | |
| println(iorList) | |
| // combines 2 F[A] where A has a Semigroup | |
| val listO: List[Option[Int]] =alList.alignCombine(List(Option(1), Option(2)),List(Option(100),None,Option(120), None)) | |
| println(listO) | |
| // combines lists based on key | |
| println(Map("a" -> List(1), "e"-> Nil).alignCombine(Map("a" -> List(2,3), "e"-> List(100), "b" -> List(10)))) | |
| // adds up values based on key | |
| println(Map("a" ->1).alignCombine(Map("a" -> 2, "b" -> 10))) | |
| // merging 2 maps based on key, whilst keeping both values | |
| println(Map("a" -> 1, "c"-> 5).padZip(Map("a" -> 2, "b" -> 10))) | |
| } |
This file contains hidden or 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
| Some(Both(1,2)) | |
| Some(Left(1)) | |
| None | |
| List(Both(1,10), Both(2,11), Right(12)) | |
| List(Some(101), Some(2), Some(120), None) | |
| Map(a -> List(1, 2, 3), e -> List(100), b -> List(10)) | |
| Map(a -> 3, b -> 10) | |
| Map(a -> (Some(1),Some(2)), c -> (Some(5),None), b -> (None,Some(10))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment