Mouse

Continuous Integration Maven Central

Mouse is a small companion to the Cats functional programming library and the Scala standard library.

The library arose from this Cats issue and is a Typelevel member.

Mouse is published for Scala 2.12, 2.13 and 3.0. For Scala.jvm:

"org.typelevel" %% "mouse" % "1.3.2"

For scala.js 1.x:

"org.typelevel" %%% "mouse" % "1.3.2"

Scope of Library

Provide enrichments to classes from the Scala standard library that convert to Cats datatypes, or otherwise improve the functional programming experience.

Content

Mouse includes enrichments for:

Example:

import mouse.all._

true.option("Its true!")
// res0: Option[String] = Some(value = "Its true!")

def makeEven(i: Int) = (i % 2 == 1).applyIf(i)(_ - 1)

val e1: Either[String, Int] = Left("failed")
// e1: Either[String, Int] = Left(value = "failed")

true.whenA(e1)
// res1: Either[String, Unit] = Left(value = "failed")

false.whenA(e1)
// res2: Either[String, Unit] = Right(value = ())

res0.cata(msg => s"Message received: ${msg}", "No message")
// res3: String = "Message received: Its true!"

"1.0".parseFloat
// res4: Either[NumberFormatException, Float] = Right(value = 1.0F)

"foo".parseIntValidated
// res5: cats.data.Validated[NumberFormatException, Int] = Invalid(
//   e = java.lang.NumberFormatException: For input string: "foo"
// )

val t1 = scala.util.Try(new java.net.URL("https://www.github.com"))
// t1: util.Try[java.net.URL] = Success(value = https://www.github.com)

t1.cata(_ => s"URL is valid!", error => s"URL is invalid: ${error.getMessage}")
// res6: String = "URL is valid!"

t1.toEither
// res7: Either[Throwable, java.net.URL] = Right(
//   value = https://www.github.com
// )

val t2 = scala.util.Try(new java.net.URL("https//www.github.com"))
// t2: util.Try[java.net.URL] = Failure(
//   exception = java.net.MalformedURLException: no protocol: https//www.github.com
// )

t2.cata(_ => s"URL is valid!", error => s"URL is invalid: ${error.getMessage}")
// res8: String = "URL is invalid: no protocol: https//www.github.com"

t2.toEither
// res9: Either[Throwable, java.net.URL] = Left(
//   value = java.net.MalformedURLException: no protocol: https//www.github.com
// )

val intToBytes = 123456789.toByteArray
// intToBytes: Array[Byte] = Array(7, 91, -51, 21)

val longToBase64 = 123456789L.toBase64
// longToBase64: String = "AAAAAAdbzRU"

5.squared
// res10: Int = 25

1.5 |> (_.toInt) |> (_.toString)
// res11: String = "1"

//lift a partial function into a total function to an Either, when you want to treat unhandled input cases as an error
liftEither[Option[Int]]({case Some(n) => n}, a => s"Unexpected: $a")(Some(6))
// res12: Either[String, Int] = Right(value = 6)

val mapped = Map(1 -> 2, 3 -> 4).mapKeys(_ * 2)
// mapped: Map[Int, Int] = Map(2 -> 2, 6 -> 4)

val foption = List(Option(1), Option(2), Option(4)).mapIn(_ * 2)
// foption: List[Option[Int]] = List(
//   Some(value = 2),
//   Some(value = 4),
//   Some(value = 8)
// )

val feither = List(Either.cond(true, 1, "0")).mapIn(_ * 2)
// feither: List[Either[String, Int]] = List(Right(value = 2))

val listOption = List(Option(1), Option(2)).mapNested2(_ * 2)
// listOption: List[Option[Int]] = List(Some(value = 2), Some(value = 4))

val listOptionList = List(Option(List(1)), Option(List(2))).mapNested3(_ * 2)
// listOptionList: List[Option[List[Int]]] = List(
//   Some(value = List(2)),
//   Some(value = List(4))
// )

val tupleHead = (1, 2, 4, 8).head
// tupleHead: Int = 1

val tupleLast = (1, 2, 4, 8).last
// tupleLast: Int = 8

Contributing

Mouse is maintained by @benhutchison and @danicheg.

Issues and pull requests are welcome. Code contributions should be aligned with the above scope to be included, and include unit tests. See contributing guide for more details.

This project supports the Scala code of conduct and aims that its channels (mailing list, Gitter, Github, etc.) to be welcoming environments for everyone.