Skip to content

Commit f1b72e8

Browse files
authored
Add semigroup instance for Tuple2 (#1321)
* Add monad defer laws ported from cats * Remove stack safety laws for now as I don't think they apply * Add arbitrary sleep to catch any async coroutine executions * change method name: bind => flatMap * Add delay constant func and make async and invoke start lazy by default * Remove delay override in async instances Monad defer already provides a default for it. And if async override differs in any way this would produce unexpected results * Add test * Add stack safety laws back * Missing shouldBe true check * Skip stack safety laws for reactor and rx2 integrations * Add some documentation for rx2 and reactor not being stack safe * Slight changes, remove attempt * Fix links and names * Add semigroup for Tuple2 * Fix instances * Formatting
1 parent ea3fb44 commit f1b72e8

File tree

1 file changed

+19
-6
lines changed
  • modules/core/arrow-core-extensions/src/main/kotlin/arrow/core/extensions

1 file changed

+19
-6
lines changed

modules/core/arrow-core-extensions/src/main/kotlin/arrow/core/extensions/tuple.kt

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
@file:Suppress("UnusedImports")
2+
23
package arrow.core.extensions
34

45
import arrow.Kind
@@ -96,21 +97,33 @@ interface Tuple2Traverse<F> : Traverse<Tuple2PartialOf<F>>, Tuple2Foldable<F> {
9697
}
9798

9899
@extension
99-
interface Tuple2Monoid<A, B> : Monoid<Tuple2<A, B>> {
100-
101-
fun MA(): Monoid<A>
100+
interface Tuple2Semigroup<A, B> : Semigroup<Tuple2<A, B>> {
102101

103-
fun MB(): Monoid<B>
102+
fun SA(): Semigroup<A>
104103

105-
override fun empty(): Tuple2<A, B> = Tuple2(MA().empty(), MB().empty())
104+
fun SB(): Semigroup<B>
106105

107106
override fun Tuple2<A, B>.combine(b: Tuple2<A, B>): Tuple2<A, B> {
108107
val (xa, xb) = this
109108
val (ya, yb) = b
110-
return Tuple2(MA().run { xa.combine(ya) }, MB().run { xb.combine(yb) })
109+
return Tuple2(SA().run { xa.combine(ya) }, SB().run { xb.combine(yb) })
111110
}
112111
}
113112

113+
@extension
114+
interface Tuple2Monoid<A, B> : Tuple2Semigroup<A, B>, Monoid<Tuple2<A, B>> {
115+
116+
fun MA(): Monoid<A>
117+
118+
fun MB(): Monoid<B>
119+
120+
override fun SA(): Semigroup<A> = MA()
121+
122+
override fun SB(): Semigroup<B> = MB()
123+
124+
override fun empty(): Tuple2<A, B> = Tuple2(MA().empty(), MB().empty())
125+
}
126+
114127
@extension
115128
interface Tuple2Eq<A, B> : Eq<Tuple2<A, B>> {
116129

0 commit comments

Comments
 (0)