Skip to content

Commit 50a4dec

Browse files
committed
Optimize AlgebraBoilerplate by using instance constructors
1 parent 5f4a965 commit 50a4dec

File tree

5 files changed

+61
-31
lines changed

5 files changed

+61
-31
lines changed

algebra-core/src/main/scala/algebra/ring/Rig.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,12 @@ trait Rig[@sp(Int, Long, Float, Double) A] extends Any with Semiring[A] with Mul
1919

2020
object Rig extends AdditiveMonoidFunctions[Rig] with MultiplicativeMonoidFunctions[Rig] {
2121
@inline final def apply[A](implicit ev: Rig[A]): Rig[A] = ev
22+
23+
private[algebra] def instance[A](z: A, o: A, add: (A, A) => A, mul: (A, A) => A): Rig[A] =
24+
new Rig[A] {
25+
val zero: A = z
26+
val one: A = o
27+
def plus(x: A, y: A): A = add(x, y)
28+
def times(x: A, y: A): A = mul(x, y)
29+
}
2230
}

algebra-core/src/main/scala/algebra/ring/Ring.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,4 +115,13 @@ trait RingFunctions[R[T] <: Ring[T]] extends AdditiveGroupFunctions[R] with Mult
115115

116116
object Ring extends RingFunctions[Ring] {
117117
@inline final def apply[A](implicit ev: Ring[A]): Ring[A] = ev
118+
119+
private[algebra] def instance[A](z: A, o: A, neg: A => A, add: (A, A) => A, mul: (A, A) => A): Ring[A] =
120+
new Ring[A] {
121+
val zero: A = z
122+
val one: A = o
123+
def negate(x: A): A = neg(x)
124+
def plus(x: A, y: A): A = add(x, y)
125+
def times(x: A, y: A): A = mul(x, y)
126+
}
118127
}

algebra-core/src/main/scala/algebra/ring/Rng.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,12 @@ trait Rng[@sp(Int, Long, Float, Double) A] extends Any with Semiring[A] with Add
1919

2020
object Rng extends AdditiveGroupFunctions[Rng] with MultiplicativeSemigroupFunctions[Rng] {
2121
@inline final def apply[A](implicit ev: Rng[A]): Rng[A] = ev
22+
23+
private[algebra] def instance[A](z: A, neg: A => A, add: (A, A) => A, mul: (A, A) => A): Rng[A] =
24+
new Rng[A] {
25+
val zero: A = z
26+
def negate(x: A): A = neg(x)
27+
def plus(x: A, y: A): A = add(x, y)
28+
def times(x: A, y: A): A = mul(x, y)
29+
}
2230
}

algebra-core/src/main/scala/algebra/ring/Semiring.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,11 @@ trait Semiring[@sp(Int, Long, Float, Double) A]
2323

2424
object Semiring extends AdditiveMonoidFunctions[Semiring] with MultiplicativeSemigroupFunctions[Semiring] {
2525
@inline final def apply[A](implicit ev: Semiring[A]): Semiring[A] = ev
26+
27+
private[algebra] def instance[A](z: A, add: (A, A) => A, mul: (A, A) => A): Semiring[A] =
28+
new Semiring[A] {
29+
val zero: A = z
30+
def plus(x: A, y: A): A = add(x, y)
31+
def times(x: A, y: A): A = mul(x, y)
32+
}
2633
}

project/AlgebraBoilerplate.scala

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,7 @@ object AlgebraBoilerplate {
4444
val synVals = (0 until arity).map(n => s"a$n")
4545
val `A..N` = synTypes.mkString(", ")
4646
val `a..n` = synVals.mkString(", ")
47-
val `_.._` = Seq.fill(arity)("_").mkString(", ")
4847
val `(A..N)` = if (arity == 1) "Tuple1[A0]" else synTypes.mkString("(", ", ", ")")
49-
val `(_.._)` = if (arity == 1) "Tuple1[_]" else Seq.fill(arity)("_").mkString("(", ", ", ")")
5048
val `(a..n)` = if (arity == 1) "Tuple1(a)" else synVals.mkString("(", ", ", ")")
5149
}
5250

@@ -86,32 +84,32 @@ object AlgebraBoilerplate {
8684
import tv._
8785

8886
def constraints(constraint: String) =
89-
synTypes.map(tpe => s"${tpe}: ${constraint}[${tpe}]").mkString(", ")
87+
synTypes.map(tpe => s"$tpe: $constraint[$tpe]").mkString(", ")
9088

9189
def tuple(results: TraversableOnce[String]) = {
9290
val resultsVec = results.toVector
9391
val a = synTypes.size
9492
val r = s"${0.until(a).map(i => resultsVec(i)).mkString(", ")}"
9593
if (a == 1) "Tuple1(" ++ r ++ ")"
96-
else s"(${r})"
94+
else s"($r)"
9795
}
9896

9997
def binMethod(name: String) =
10098
synTypes.zipWithIndex.iterator.map { case (tpe, i) =>
10199
val j = i + 1
102-
s"${tpe}.${name}(x._${j}, y._${j})"
100+
s"$tpe.$name(x._$j, y._$j)"
103101
}
104102

105103
def binTuple(name: String) =
106104
tuple(binMethod(name))
107105

108106
def unaryTuple(name: String) = {
109-
val m = synTypes.zipWithIndex.map { case (tpe, i) => s"${tpe}.${name}(x._${i + 1})" }
107+
val m = synTypes.zipWithIndex.map { case (tpe, i) => s"$tpe.$name(x._${i + 1})" }
110108
tuple(m)
111109
}
112110

113111
def nullaryTuple(name: String) = {
114-
val m = synTypes.map(tpe => s"${tpe}.${name}")
112+
val m = synTypes.map(tpe => s"$tpe.$name")
115113
tuple(m)
116114
}
117115

@@ -124,36 +122,36 @@ object AlgebraBoilerplate {
124122
|trait TupleInstances extends cats.kernel.instances.TupleInstances {
125123
-
126124
- implicit def tuple${arity}Rig[${`A..N`}](implicit ${constraints("Rig")}): Rig[${`(A..N)`}] =
127-
- new Rig[${`(A..N)`}] {
128-
- def one: ${`(A..N)`} = ${nullaryTuple("one")}
129-
- def plus(x: ${`(A..N)`}, y: ${`(A..N)`}): ${`(A..N)`} = ${binTuple("plus")}
130-
- def times(x: ${`(A..N)`}, y: ${`(A..N)`}): ${`(A..N)`} = ${binTuple("times")}
131-
- def zero: ${`(A..N)`} = ${nullaryTuple("zero")}
132-
- }
125+
- Rig.instance(
126+
- ${nullaryTuple("zero")},
127+
- ${nullaryTuple("one")},
128+
- (x, y) => ${binTuple("plus")},
129+
- (x, y) => ${binTuple("times")}
130+
- )
133131
-
134132
- implicit def tuple${arity}Ring[${`A..N`}](implicit ${constraints("Ring")}): Ring[${`(A..N)`}] =
135-
- new Ring[${`(A..N)`}] {
136-
- def one: ${`(A..N)`} = ${nullaryTuple("one")}
137-
- def plus(x: ${`(A..N)`}, y: ${`(A..N)`}): ${`(A..N)`} = ${binTuple("plus")}
138-
- def times(x: ${`(A..N)`}, y: ${`(A..N)`}): ${`(A..N)`} = ${binTuple("times")}
139-
- def zero: ${`(A..N)`} = ${nullaryTuple("zero")}
140-
- def negate(x: ${`(A..N)`}): ${`(A..N)`} = ${unaryTuple("negate")}
141-
- }
133+
- Ring.instance(
134+
- ${nullaryTuple("zero")},
135+
- ${nullaryTuple("one")},
136+
- x => ${unaryTuple("negate")},
137+
- (x, y) => ${binTuple("plus")},
138+
- (x, y) => ${binTuple("times")}
139+
- )
142140
-
143141
- implicit def tuple${arity}Rng[${`A..N`}](implicit ${constraints("Rng")}): Rng[${`(A..N)`}] =
144-
- new Rng[${`(A..N)`}] {
145-
- def plus(x: ${`(A..N)`}, y: ${`(A..N)`}): ${`(A..N)`} = ${binTuple("plus")}
146-
- def times(x: ${`(A..N)`}, y: ${`(A..N)`}): ${`(A..N)`} = ${binTuple("times")}
147-
- def zero: ${`(A..N)`} = ${nullaryTuple("zero")}
148-
- def negate(x: ${`(A..N)`}): ${`(A..N)`} = ${unaryTuple("negate")}
149-
- }
142+
- Rng.instance(
143+
- ${nullaryTuple("zero")},
144+
- x => ${unaryTuple("negate")},
145+
- (x, y) => ${binTuple("plus")},
146+
- (x, y) => ${binTuple("times")}
147+
- )
150148
-
151149
- implicit def tuple${arity}Semiring[${`A..N`}](implicit ${constraints("Semiring")}): Semiring[${`(A..N)`}] =
152-
- new Semiring[${`(A..N)`}] {
153-
- def plus(x: ${`(A..N)`}, y: ${`(A..N)`}): ${`(A..N)`} = ${binTuple("plus")}
154-
- def times(x: ${`(A..N)`}, y: ${`(A..N)`}): ${`(A..N)`} = ${binTuple("times")}
155-
- def zero: ${`(A..N)`} = ${nullaryTuple("zero")}
156-
- }
150+
- Semiring.instance(
151+
- ${nullaryTuple("zero")},
152+
- (x, y) => ${binTuple("plus")},
153+
- (x, y) => ${binTuple("times")}
154+
- )
157155
|}
158156
"""
159157
}

0 commit comments

Comments
 (0)