-
-
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.
Add withDefault and atOrElse to replace None by a default value (#886)
* Add withDefault and atOrElse to replace None by a default value * format * add doc and tests for atOrElse * fix doc
- Loading branch information
1 parent
14b7902
commit 3a2ef4e
Showing
5 changed files
with
99 additions
and
2 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
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,17 +1,38 @@ | ||
package monocle.function | ||
|
||
import cats.kernel.Eq | ||
import monocle.MonocleSuite | ||
import monocle.{Lens, MonocleSuite} | ||
import monocle.law.discipline.function.AtTests | ||
|
||
import scala.collection.immutable.ListMap | ||
|
||
class AtSpec extends MonocleSuite { | ||
implicit val eqListMap: Eq[ListMap[Int, String]] = Eq.fromUniversalEquals | ||
|
||
implicit def mmapAt[K, V]: At[MMap[K, V], K, Option[V]] = At.fromIso(MMap.toMap) | ||
implicit def mmapAt[K, V]: At[MMap[K, V], K, Option[V]] = | ||
At.fromIso(MMap.toMap) | ||
|
||
checkAll("fromIso", AtTests[MMap[Int, String], Int, Option[String]]) | ||
|
||
checkAll("ListMap", AtTests[ListMap[Int, String], Int, Option[String]]) | ||
|
||
def mapDefaultTo0(index: String): Lens[Map[String, Int], Int] = | ||
atOrElse(index)(0) | ||
|
||
test("atOrElse") { | ||
val counters = Map("id1" -> 4, "id2" -> 2) | ||
|
||
assert(mapDefaultTo0("id1").get(counters) == 4) | ||
assert(mapDefaultTo0("id3").get(counters) == 0) | ||
|
||
assert(mapDefaultTo0("id1").modify(_ + 1)(counters) == Map("id1" -> 5, "id2" -> 2)) | ||
assert( | ||
mapDefaultTo0("id3") | ||
.modify(_ + 1)(counters) == Map("id1" -> 4, "id2" -> 2, "id3" -> 1) | ||
) | ||
} | ||
|
||
test("atOrElse can break get-set property") { | ||
assert(mapDefaultTo0("id").set(0)(Map("id" -> 0)) == Map.empty) | ||
} | ||
} |
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