Skip to content

Commit 5168a73

Browse files
committed
fix compilation and align types for wartremover ⚡
1 parent 4fab4d3 commit 5168a73

14 files changed

Lines changed: 89 additions & 47 deletions

File tree

benchmark/src/main/scala/com/htmlism/spawningpool/Benchmark.scala

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,25 @@ object Benchmark {
66
println()
77
println(s"Running $n...")
88

9-
val start = compat.Platform.currentTime
9+
val start = System.currentTimeMillis()
1010

1111
f()
1212

13-
val duration = compat.Platform.currentTime - start
13+
val duration = System.currentTimeMillis() - start
1414
val duratinInSeconds = duration / 1000
1515

1616
duratinInSeconds
1717
}
1818

1919
def apply[A](fs: Map[A, () => Unit], times: Int): Unit = {
2020
val durations = fs
21-
.mapValues(toDurations(times))
22-
.map(identity)
21+
.map { case (k, v) => (k, toDurations(times)(v)) }
2322

2423
println("Durations:")
2524
durations.foreach(println)
2625

2726
val averages = durations
28-
.mapValues(_.sum / times)
27+
.map { case (k, v) => (k, v.sum / times) }
2928

3029
println("Average duration:")
3130
averages.foreach(println)

benchmark/src/main/scala/com/htmlism/spawningpool/VectorVsArray.scala

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ object VectorVsArray extends App {
2222
{ _ =>
2323
util.Random.nextInt()
2424
},
25-
islandCount = PositiveCount(15),
2625
populationSize = PositiveCount(1000),
26+
islandCount = PositiveCount(15),
27+
mutationRate = Solver.DEFAULT_MUTATION_RATE,
2728
generations = PositiveCount(40)
2829
)
2930

@@ -38,8 +39,9 @@ object VectorVsArray extends App {
3839
{ _ =>
3940
util.Random.nextInt()
4041
},
41-
islandCount = PositiveCount(15),
4242
populationSize = PositiveCount(1000),
43+
islandCount = PositiveCount(15),
44+
mutationRate = Solver.DEFAULT_MUTATION_RATE,
4345
generations = PositiveCount(40)
4446
)
4547

@@ -48,29 +50,31 @@ object VectorVsArray extends App {
4850

4951
def withVector(): Unit = {
5052
implicit val combinator =
51-
new DiscreteFixedLengthCombinator((1 to 100).map(WrappedInt), 100)
53+
new DiscreteFixedLengthCombinator((1 to 100).map(WrappedInt.apply), 100)
5254

5355
val solver = new Solver[Seq[WrappedInt], Int](
5456
{ _ =>
5557
util.Random.nextInt()
5658
},
57-
islandCount = PositiveCount(15),
5859
populationSize = PositiveCount(1000),
60+
islandCount = PositiveCount(15),
61+
mutationRate = Solver.DEFAULT_MUTATION_RATE,
5962
generations = PositiveCount(40)
6063
)
6164

6265
val _ = solver.solveNow
6366
}
6467

6568
def withArray(): Unit = {
66-
implicit val combinator = new specialized.DiscreteFixedLengthCombinator((1 to 100).map(WrappedInt), 100)
69+
implicit val combinator = new specialized.DiscreteFixedLengthCombinator((1 to 100).map(WrappedInt.apply), 100)
6770

6871
val solver = new Solver[Seq[WrappedInt], Int](
6972
{ _ =>
7073
util.Random.nextInt()
7174
},
72-
islandCount = PositiveCount(15),
7375
populationSize = PositiveCount(1000),
76+
islandCount = PositiveCount(15),
77+
mutationRate = Solver.DEFAULT_MUTATION_RATE,
7478
generations = PositiveCount(40)
7579
)
7680

project/DependenciesPlugin.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,13 @@ object DependenciesPlugin extends AutoPlugin {
1313
def withEffectMonad: Project =
1414
p.settings(libraryDependencies += "org.typelevel" %% "cats-effect" % Versions.catsEffect)
1515

16-
def withTesting: Project = {
16+
def withTesting: Project =
1717
p.settings(
1818
libraryDependencies ++= Seq(
1919
"org.typelevel" %% "weaver-cats" % Versions.weaver % Test,
2020
"org.typelevel" %% "weaver-scalacheck" % Versions.weaver % Test
2121
)
2222
)
23-
}
2423

2524
def withYaml: Project =
2625
p.settings(
Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,42 @@
11
package com.htmlism.spawningpool.fitness
22

3+
import scala.annotation.nowarn
4+
35
import org.specs2.mutable.Specification
46

7+
@nowarn("msg=unused value")
8+
@SuppressWarnings(Array("org.wartremover.warts.NonUnitStatements", "org.wartremover.warts.Unused"))
59
class FitnessSpec extends Specification {
610
"fitness by ordering" should {
11+
val fitness = new OrdinalFitness[(String, Int)]
712
"summon the implicit ordering" in {
8-
val fitness = new OrdinalFitness[(String, Int)]
9-
10-
fitness.compare("a" -> 1, "a" -> 2) === -1
11-
fitness.compare("a" -> 1, "a" -> 1) === 0
12-
fitness.compare("b" -> 1, "a" -> 2) === 1
13+
fitness.compare("a" -> 1, "a" -> 2) must_== -1
14+
fitness.compare("a" -> 1, "a" -> 1) must_== 0
15+
fitness.compare("b" -> 1, "a" -> 2) must_== 1
1316
}
1417
}
1518

1619
"ratio fitness" should {
20+
val fitness = new RatioFitness((s: String) => s.length)
1721
"summon the implicit numeric" in {
18-
val fitness = new RatioFitness((s: String) => s.length)
19-
20-
fitness.compare("short", "longest") === -1
21-
fitness.compare("equal", "apple") === 0
22-
fitness.compare("longest", "short") === 1
22+
fitness.compare("short", "longest") must_== -1
23+
fitness.compare("equal", "apple") must_== 0
24+
fitness.compare("longest", "short") must_== 1
2325
}
2426
}
2527

2628
"fitness" should {
29+
val fitness = new OrdinalFitness[Int]
2730
"support minimization" in {
28-
val fitness = new OrdinalFitness[Int]
29-
30-
fitness.compare(123, 45) === 1
31-
fitness.minimize.compare(123, 45) === -1
31+
fitness.compare(123, 45) must_== 1
32+
fitness.minimize.compare(123, 45) must_== -1
3233
}
3334

3435
"support chaining" in {
3536
val fitness1 = new OrdinalFitness[Int]
3637
val fitness2 = new OrdinalFitness[Double]
3738
val totalFitness = fitness1 andThen fitness2
38-
39-
totalFitness.compare((1, 1d), (1, 2d)) === -1
39+
totalFitness.compare((1, 1d), (1, 2d)) must_== -1
4040
}
4141
}
4242
}

spawning-pool-core/src/main/scala/com/htmlism/spawningpool/Crossover.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ trait Crossover[A] {
2323
}
2424

2525
object IntUniformCrossover extends Crossover[Int] {
26-
def crossover(xs: List[Int]): Int = ???
26+
def crossover(xs: List[Int]): Int = sys.error("crossover not implemented")
2727
}
2828

2929
object DoubleUniformCrossover extends Crossover[Double] {
30-
def crossover(xs: List[Double]): Double = ???
30+
def crossover(xs: List[Double]): Double = sys.error("crossover not implemented")
3131
}

spawning-pool-core/src/main/scala/com/htmlism/spawningpool/Mutation.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ object IntMutation extends Mutation[Int] {
2929
}
3030

3131
object DoubleMutation extends Mutation[Double] {
32-
def mutate(x: Double): Double = ???
32+
def mutate(x: Double): Double = sys.error("mutate not implemented")
3333
}
3434

3535
object FixedArrayIntMutation extends Mutation[Array[Int]] {
36-
def mutate(x: Array[Int]): Array[Int] = ???
36+
def mutate(x: Array[Int]): Array[Int] = sys.error("mutate not implemented")
3737
}
3838

3939
object FixedDoubleMutation extends Mutation[Array[Double]] {
40-
def mutate(x: Array[Double]): Array[Double] = ???
40+
def mutate(x: Array[Double]): Array[Double] = sys.error("mutate not implemented")
4141
}

spawning-pool-core/src/main/scala/com/htmlism/spawningpool/PositiveCount.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ object PositiveCount {
55
if (count > 0)
66
new PositiveCount(count)
77
else
8-
throw new IllegalArgumentException(s"$count is not positive")
8+
sys.error(s"$count is not positive")
99
}
1010

1111
class PositiveCount private (val count: Int) extends AnyVal {

spawning-pool-core/src/main/scala/com/htmlism/spawningpool/Solver.scala

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ object Solver {
1010
val DEFAULT_GENERATION_COUNT = PositiveCount(20)
1111
val DEFAULT_MUTATION_RATE = .01
1212

13+
def apply[A, B](
14+
fitness: A => B
15+
)(implicit evolver: Evolver[A], ordering: Ordering[B]): Solver[A, B] =
16+
new Solver(fitness, DEFAULT_POPULATION_SIZE, DEFAULT_ISLAND_COUNT, DEFAULT_MUTATION_RATE, DEFAULT_GENERATION_COUNT)
17+
1318
def randomIndividual[A](population: Seq[A])(implicit rig: RandomIndexProvider): A =
1419
population(rig.randomIndex(population.size))
1520

@@ -77,10 +82,10 @@ object Solver {
7782
*/
7883
class Solver[A, B](
7984
fitness: A => B,
80-
populationSize: PositiveCount = Solver.DEFAULT_POPULATION_SIZE,
81-
islandCount: PositiveCount = Solver.DEFAULT_ISLAND_COUNT,
82-
mutationRate: Double = Solver.DEFAULT_MUTATION_RATE,
83-
generations: PositiveCount = Solver.DEFAULT_GENERATION_COUNT
85+
populationSize: PositiveCount,
86+
islandCount: PositiveCount,
87+
mutationRate: Double,
88+
generations: PositiveCount
8489
)(implicit evolver: Evolver[A], ordering: Ordering[B]) {
8590
import com.htmlism.spawningpool.Solver.*
8691

@@ -95,7 +100,7 @@ class Solver[A, B](
95100
def solve(seed: List[A])(implicit ec: ExecutionContext): Future[Solutions] =
96101
Future {
97102
if (seed.isEmpty)
98-
throw new IllegalArgumentException("must provide a non-empty collection as a seed")
103+
sys.error("must provide a non-empty collection as a seed")
99104
else
100105
evolveFrom {
101106
seed.toVector
@@ -122,7 +127,13 @@ class Solver[A, B](
122127
f.map { ctx =>
123128
val byFitness = ctx.population.groupBy(ctx.fitness)
124129

125-
byFitness(byFitness.keys.max)
130+
val maxKey = byFitness
131+
.keys
132+
.headOption
133+
.fold(sys.error("empty population"))(head =>
134+
byFitness.keys.foldLeft(head)((a, b) => if (ctx.ordering.compare(a, b) >= 0) a else b)
135+
)
136+
byFitness(maxKey)
126137
}
127138
}
128139

spawning-pool-core/src/main/scala/com/htmlism/spawningpool/combinatorics/DefaultRandomProvider.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ trait DefaultRandomProvider
1010
with LengthProvider
1111
with VariationProvider
1212
with MutationMethodProvider {
13-
private val rng = new util.Random
13+
private val rng = new util.Random
1414
private val mutations: IndexedSeq[MutationMethod] =
1515
IndexedSeq[MutationMethod](MutateGene, AddGene, RemoveGene)
1616

spawning-pool-core/src/test/scala/com/htmlism/spawningpool/GeneratorSpec.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
package com.htmlism.spawningpool
22

3+
import scala.annotation.nowarn
4+
35
import org.specs2.mutable.Specification
46

7+
@nowarn("msg=unused value")
8+
@SuppressWarnings(Array("org.wartremover.warts.NonUnitStatements", "org.wartremover.warts.Unused"))
59
class GeneratorSpec extends Specification {
610
"Generation" should {
711
"provide default implementations" in {

0 commit comments

Comments
 (0)