Skip to content

Commit 2806a33

Browse files
authored
SCALA-3484: Introduction to Spire (#685)
* chore(wip): introduce spire * chore(spire): init lib * chore(spire): add number types, macros & monoids samples * lint: format code * chore: high level type classes * lint: fmt tests * chore: curate groups examples - SemiGroups - Monoids - Groups * chore: add rings examples - Rings - EuclideanRings * refactor: move to scala-libraries-5 see https://jira.baeldung.com/browse/SCALA-384?focusedCommentId=258512&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-258512
1 parent cf1cda4 commit 2806a33

23 files changed

+461
-1
lines changed

build.sbt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ val fs2Version = "3.7.0"
228228
val AkkaVersion = "2.8.0"
229229
val AkkaHttpVersion = "10.5.0"
230230
val reactiveMongo = "1.0.10"
231+
val spireVersion = "0.18.0"
231232

232233
lazy val scala_libraries = (project in file("scala-libraries"))
233234
.settings(
@@ -390,7 +391,8 @@ lazy val scala_libraries_5 = (project in file("scala-libraries-5"))
390391
libraryDependencies ++= scalaTestDeps,
391392
libraryDependencies ++= Seq(
392393
sparkSqlDep,
393-
sparkCoreDep
394+
sparkCoreDep,
395+
"org.typelevel" %% "spire" % spireVersion
394396
)
395397
)
396398

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.baeldung.scala.spire
2+
3+
import spire.implicits._
4+
import spire.math.Fractional
5+
6+
object Fractional {
7+
def avg[T](values: List[T])(implicit fractional: Fractional[T]): T = {
8+
val sum = values.foldLeft(fractional.zero)(fractional.plus)
9+
fractional.div(sum, fractional.fromInt(values.length))
10+
}
11+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.baeldung.scala.spire
2+
3+
import spire.implicits._
4+
import spire.math._
5+
6+
object Integral {
7+
def factorial[@specialized(Int) T](
8+
n: T
9+
)(implicit integral: Integral[T]): T = {
10+
if (integral.lt(n, integral.one)) integral.one
11+
else integral.times(n, factorial(integral.minus(n, integral.one)))
12+
}
13+
14+
def gcd[T](a: T, b: T)(implicit ev: Integral[T]): T = ev.gcd(a, b)
15+
16+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.baeldung.scala.spire
2+
3+
import spire.implicits._
4+
import spire.math._
5+
object Numeric {
6+
def sum[T](values: List[T])(implicit numeric: Numeric[T]): T = {
7+
values.foldLeft(numeric.zero)(numeric.plus)
8+
}
9+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.baeldung.scala.spire.groups
2+
3+
import spire.algebra._
4+
import spire.implicits._
5+
6+
object Semigroups extends App {
7+
val a: Int = 5
8+
val b: Int = 7
9+
val result: Int = Semigroup[Int].combine(a, b)
10+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.baeldung.scala.spire.groups
2+
3+
import spire.algebra._
4+
import spire.implicits._
5+
6+
case class Transaction(amount: Int)
7+
8+
object TransactionGroup extends Group[Transaction] {
9+
def combine(x: Transaction, y: Transaction): Transaction = Transaction(
10+
x.amount + y.amount
11+
)
12+
def empty: Transaction = Transaction(0)
13+
def inverse(a: Transaction): Transaction = Transaction(-a.amount)
14+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.baeldung.scala.spire.macros
2+
3+
import spire.implicits.cfor
4+
5+
object CFor extends App {
6+
7+
cfor(9)(_ >= 0, _ - 1) { i =>
8+
println(i)
9+
}
10+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.baeldung.scala.spire.macros
2+
3+
import spire.implicits._
4+
5+
object LiteralNumberSyntax extends App {
6+
7+
val poly1 = poly"3x^2 - 5x + 1"
8+
val poly2 = poly"5/4x^6 - 7x - 2"
9+
val poly3 = poly1.-(poly2)
10+
println(poly3)
11+
12+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.baeldung.scala.spire.monoids
2+
3+
import spire.algebra._
4+
import spire.implicits._
5+
6+
object AdditiveMonoid extends App {
7+
def sum[T](values: List[T])(implicit ev: AdditiveMonoid[T]): T =
8+
values.foldLeft(ev.zero)(ev.plus)
9+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.baeldung.scala.spire.numbers
2+
3+
import spire.implicits._
4+
import spire.math._
5+
6+
object Complexes extends App {
7+
8+
private def quadraticRoots(
9+
x: Complex[Double],
10+
y: Complex[Double],
11+
z: Complex[Double]
12+
): (Complex[Double], Complex[Double]) = {
13+
val discriminant = y * y - Complex(4.0) * x.*(z)
14+
val sqrtDiscriminant = discriminant.sqrt
15+
val r1 = (-y + sqrtDiscriminant) / (Complex(2.0) * x)
16+
val r2 = (-y - sqrtDiscriminant) / (Complex(2.0) * x)
17+
(r1, r2)
18+
}
19+
20+
val x = Complex(2.0, 0.0)
21+
val y = Complex(-3.0, 4.0)
22+
private val z = Complex(5.0, -6.0)
23+
24+
private val (r1, r2) = quadraticRoots(x, y, z)
25+
println(s"Root 1: $r1")
26+
println(s"Root 2: $r2")
27+
28+
}

0 commit comments

Comments
 (0)