forked from typelevel/cats
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from adelbertc/freet-either
Freet either
- Loading branch information
Showing
84 changed files
with
1,834 additions
and
477 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package cats.bench | ||
|
||
import cats.instances.list._ | ||
import cats.instances.int._ | ||
import cats.instances.map._ | ||
|
||
import scalaz.std.anyVal._ | ||
import scalaz.std.list._ | ||
import scalaz.std.map._ | ||
|
||
import org.openjdk.jmh.annotations.{ Benchmark, Scope, State } | ||
|
||
@State(Scope.Benchmark) | ||
class MapMonoidBench { | ||
|
||
val words: List[String] = | ||
for { | ||
c <- List("a", "b", "c", "d", "e") | ||
t <- 1 to 100 | ||
} yield c * t | ||
|
||
val maps: List[Map[String, Int]] = (words ++ words).map(s => Map(s -> 1)) | ||
|
||
@Benchmark def combineAllCats: Map[String, Int] = | ||
cats.Monoid[Map[String, Int]].combineAll(maps) | ||
|
||
@Benchmark def combineCats: Map[String, Int] = | ||
maps.foldLeft(Map.empty[String, Int]) { | ||
case (acc, m) => cats.Monoid[Map[String, Int]].combine(acc, m) | ||
} | ||
|
||
@Benchmark def combineScalaz: Map[String, Int] = | ||
maps.foldLeft(Map.empty[String, Int]) { | ||
case (acc, m) => scalaz.Monoid[Map[String, Int]].append(acc, m) | ||
} | ||
|
||
@Benchmark def combineDirect: Map[String, Int] = | ||
maps.foldLeft(Map.empty[String, Int]) { | ||
case (acc, m) => m.foldLeft(acc) { | ||
case (m, (k, v)) => m.updated(k, v + m.getOrElse(k, 0)) | ||
} | ||
} | ||
|
||
@Benchmark def combineGeneric: Map[String, Int] = | ||
combineMapsGeneric[String, Int](maps, 0, _ + _) | ||
|
||
def combineMapsGeneric[K, V](maps: List[Map[K, V]], z: V, f: (V, V) => V): Map[K, V] = | ||
maps.foldLeft(Map.empty[K, V]) { | ||
case (acc, m) => m.foldLeft(acc) { | ||
case (m, (k, v)) => m.updated(k, f(v, m.getOrElse(k, z))) | ||
} | ||
} | ||
|
||
@Benchmark def foldMapCats: Map[String, Int] = | ||
cats.Foldable[List].foldMap(maps)(identity) | ||
|
||
@Benchmark def foldMapScalaz: Map[String, Int] = | ||
scalaz.Foldable[List].foldMap(maps)(identity) | ||
} |
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
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
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,50 +1,45 @@ | ||
package cats | ||
package arrow | ||
|
||
import cats.data.Xor | ||
|
||
import simulacrum.typeclass | ||
|
||
@typeclass trait Choice[F[_, _]] extends Category[F] { | ||
|
||
/** | ||
* Given two `F`s (`f` and `g`) with a common target type, create a new `F` | ||
* with the same target type, but with a source type of either `f`'s source | ||
* type OR `g`'s source type. | ||
* | ||
* Example: | ||
* {{{ | ||
* scala> import cats.data.Xor | ||
* scala> import cats.implicits._ | ||
* scala> val b: Boolean => String = _ + " is a boolean" | ||
* scala> val i: Int => String = _ + " is an integer" | ||
* scala> val f: (Boolean Xor Int) => String = Choice[Function1].choice(b, i) | ||
* scala> val f: (Either[Boolean, Int]) => String = Choice[Function1].choice(b, i) | ||
* | ||
* scala> f(Xor.right(3)) | ||
* scala> f(Right(3)) | ||
* res0: String = 3 is an integer | ||
* | ||
* scala> f(Xor.left(false)) | ||
* scala> f(Left(false)) | ||
* res0: String = false is a boolean | ||
* }}} | ||
*/ | ||
def choice[A, B, C](f: F[A, C], g: F[B, C]): F[Xor[A, B], C] | ||
def choice[A, B, C](f: F[A, C], g: F[B, C]): F[Either[A, B], C] | ||
|
||
/** | ||
* An `F` that, given a source `A` on either the right or left side, will | ||
* return that same `A` object. | ||
* | ||
* Example: | ||
* {{{ | ||
* scala> import cats.data.Xor | ||
* scala> import cats.implicits._ | ||
* scala> val f: (Int Xor Int) => Int = Choice[Function1].codiagonal[Int] | ||
* scala> val f: (Either[Int, Int]) => Int = Choice[Function1].codiagonal[Int] | ||
* | ||
* scala> f(Xor.right(3)) | ||
* scala> f(Right(3)) | ||
* res0: Int = 3 | ||
* | ||
* scala> f(Xor.left(3)) | ||
* scala> f(Left(3)) | ||
* res1: Int = 3 | ||
* }}} | ||
*/ | ||
def codiagonal[A]: F[Xor[A, A], A] = choice(id, id) | ||
def codiagonal[A]: F[Either[A, A], A] = choice(id, id) | ||
} |
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
Oops, something went wrong.