Skip to content

Commit 2da8eee

Browse files
committed
add BireducibleLaws
1 parent 4629d3e commit 2da8eee

File tree

1 file changed

+95
-0
lines changed

1 file changed

+95
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
* Copyright (c) 2015 Typelevel
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy of
5+
* this software and associated documentation files (the "Software"), to deal in
6+
* the Software without restriction, including without limitation the rights to
7+
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
8+
* the Software, and to permit persons to whom the Software is furnished to do so,
9+
* subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in all
12+
* copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
16+
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
17+
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
18+
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20+
*/
21+
22+
package cats
23+
package laws
24+
25+
import cats.data.Ior
26+
import cats.kernel.laws.IsEq
27+
28+
trait BireducibleLaws[F[_, _]] extends BifoldableLaws[F] {
29+
implicit def F: Bireducible[F]
30+
31+
def bireduceLeftConsistentWithDefaultImplementation[A, B](
32+
fab: F[A, B],
33+
ma: (A, A) => A,
34+
mb: (B, B) => B
35+
): IsEq[A Ior B] = {
36+
val obtained = F.bireduceLeft(fab)(ma, mb)
37+
val expected = Bireducible.bireduceLeft(fab)(ma, mb)
38+
39+
obtained <-> expected
40+
}
41+
42+
def bireduceRightConsistentWithDefaultImplementation[A, B](
43+
fab: F[A, B],
44+
ma: (A, Eval[A]) => Eval[A],
45+
mb: (B, Eval[B]) => Eval[B]
46+
): IsEq[A Ior B] = {
47+
val obtained = F.bireduceRight(fab)(ma, mb).value
48+
val expected = Bireducible.bireduceRight(fab)(ma, mb).value
49+
50+
obtained <-> expected
51+
}
52+
53+
def bireduceMapConsistentWithDefaultImplementation[A, B, C](
54+
fab: F[A, B],
55+
ma: A => C,
56+
mb: B => C
57+
)(implicit
58+
C: Semigroup[C]
59+
): IsEq[C] = {
60+
val obtained = F.bireduceMap(fab)(ma, mb)
61+
val expected = Bireducible.bireduceMap(fab)(ma, mb)
62+
63+
obtained <-> expected
64+
}
65+
66+
def bireduceConsistentWithDefaultImplementation[A, B](
67+
fab: F[A, B]
68+
)(implicit A: Semigroup[A], B: Semigroup[B]): IsEq[A Ior B] = {
69+
70+
val obtained = F.bireduce(fab)
71+
val expected = Bireducible.bireduce(fab)
72+
73+
obtained <-> expected
74+
}
75+
76+
def bireduceLeftToConsistentWithBireduceRightTo[A, B, C](fab: F[A, B])(
77+
ma: A => C,
78+
mb: B => C
79+
)(
80+
mca: (C, A) => C,
81+
mcb: (C, B) => C
82+
): IsEq[C] = {
83+
val left = F.bireduceLeftTo(fab)(ma, mb)(mca, mcb)
84+
val right =
85+
F.bireduceRightTo(fab)(
86+
a => Eval.now(ma(a)),
87+
b => Eval.now(mb(b))
88+
)(
89+
(a, c) => c.map(mca(_, a)),
90+
(b, c) => c.map(mcb(_, b))
91+
).value
92+
93+
left <-> right
94+
}
95+
}

0 commit comments

Comments
 (0)