Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cross building setup for scala 2.13 and 3.4 [WIP] #18

Open
wants to merge 4 commits into
base: GSOC_2024
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .scalafix.conf
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
rules = [
RemoveUnused,
DisableSyntax,
ProcedureSyntax,
NoValInForComprehension
]
4 changes: 2 additions & 2 deletions .scalafmt.conf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
version = 3.8.1
runner.dialect = scala212
runner.dialect = scala213

# Number of maximum characters in a column
maxColumn = 100
Expand All @@ -16,7 +16,7 @@ assumeStandardLibraryStripMargin = true

# Align everything that can be aligned
align.preset = most
align.multiline = false
align.multiline = true
align.tokens."+" = [
{
code = ":=", owner = "Term.ApplyInfix"
Expand Down
70 changes: 43 additions & 27 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
import language.postfixOps

name := "countvotes"
lazy val scala213 = "2.13.14"
lazy val scala304 = "3.4.2"
lazy val supportedScalaVersions = List(scala213, scala304)

organization := "AOSSIE"

version := "1.2"

scalaVersion := "2.12.19"
ThisBuild / name := "countvotes"
ThisBuild / organization := "AOSSIE"
ThisBuild / version := "1.2"
ThisBuild / scalaVersion := scala304

resolvers += Resolver.sonatypeRepo("public")
resolvers += "Sonatype OSS Snapshots" at
"https://oss.sonatype.org/content/repositories/releases"

testFrameworks += new TestFramework("org.scalameter.ScalaMeterFramework")

logBuffered := false

parallelExecution in Test := false

scalacOptions ++= Seq("-unchecked", "-deprecation", "-feature")

scalacOptions in (Compile, doc) ++= Seq("-diagrams","-implicits")

scalacOptions in Test ++= Seq("-Yrangepos")
Expand All @@ -31,49 +28,68 @@ lazy val root = Project("agora", file("."))
.aggregate(
core,
cli
).settings(commonSettings)
).settings(commonSettings, crossScalaVersions := Nil)

lazy val core = (project in file("modules/core"))
.configs(Testing.configs: _*)
.settings(Testing.settings: _*)
.configs(Testing.configs *)
.settings(Testing.settings *)
.settings(
commonSettings,
crossScalaVersions := supportedScalaVersions,
name := "core"
)

lazy val cli = (project in file("modules/cli"))
.configs(Testing.configs: _*)
.settings(Testing.settings: _*)
.configs(Testing.configs *)
.settings(Testing.settings *)
.dependsOn(
core
)
.settings(
commonSettings,
crossScalaVersions := supportedScalaVersions,
name := "cli"
)

lazy val commonSettings = Seq(
scalafmtOnCompile := true,
semanticdbEnabled := true,
semanticdbVersion := scalafixSemanticdb.revision,
scalacOptions ++= Seq("-unchecked", "-deprecation", "-feature"),
// scalacOptions += "-P:semanticdb:synthetics:on",
scalacOptions += {
if (scalaVersion.value.startsWith("2.12")) "-Ywarn-unused-import"
else "-Wunused:imports"
},


libraryDependencies ++= Seq(
"com.fasterxml.jackson.core" % "jackson-databind" % "2.9.0",
"com.github.scopt" %% "scopt" % "4.1.0",
"org.specs2" %% "specs2-core" % "4.20.8" % "test,verification-test,bench",
"com.lihaoyi" %% "ammonite-ops" % "2.4.1",
"ch.qos.logback" % "logback-classic" % "1.2.11",
"com.typesafe.scala-logging" %% "scala-logging" % "3.9.5",
"com.storm-enroute" %% "scalameter" % "0.19",
"com.storm-enroute" %% "scalameter-core" % "0.19",
"com.typesafe.play" %% "play-json" % "2.9.4",
"org.typelevel" %% "spire" % "0.17.0"
),
libraryDependencies ++= {
CrossVersion.partialVersion(scalaVersion.value) match {
case Some((2, 13)) => Seq(
"com.fasterxml.jackson.core" % "jackson-databind" % "2.9.0",
"com.github.scopt" %% "scopt" % "4.1.0",
"org.specs2" %% "specs2-core" % "4.20.6" % "test,verification-test,bench",
"com.lihaoyi" %% "ammonite-ops" % "2.4.1",
"ch.qos.logback" % "logback-classic" % "1.2.11",
"com.typesafe.scala-logging" %% "scala-logging" % "3.9.5",
"org.scala-lang.modules" %% "scala-parser-combinators" % "2.4.0",
"com.typesafe.play" %% "play-json" % "2.9.4",
"org.typelevel" %% "spire" % "0.18.0"
)
case Some((3, 4)) => Seq(
"com.fasterxml.jackson.core" % "jackson-databind" % "2.9.0",
"com.github.scopt" %% "scopt" % "4.1.0",
"org.specs2" %% "specs2-core" % "4.20.6" % "test,verification-test,bench",
("com.lihaoyi" %% "ammonite-ops" % "2.4.1").cross(CrossVersion.for3Use2_13),
"ch.qos.logback" % "logback-classic" % "1.2.11",
"com.typesafe.scala-logging" %% "scala-logging" % "3.9.5",
"org.scala-lang.modules" %% "scala-parser-combinators" % "2.4.0",
"com.typesafe.play" %% "play-json" % "2.10.6",
"org.typelevel" %% "spire" % "0.18.0"
)
case _ => Nil
}
},

dependencyOverrides += "com.fasterxml.jackson.core" % "jackson-databind" % "2.9.0"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package org.aossie.agora.votecounter
import org.aossie.agora.model.Candidate
import org.aossie.agora.parser.CandidatesParser
import org.aossie.agora.parser.PreferencesParser
import org.aossie.agora.votecounter.Bucklin
import org.specs2.mutable.Specification

class BucklinTest extends Specification {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import org.aossie.agora.parser.PreferencesParser
import org.aossie.agora.model.Candidate
import org.specs2.mutable.Specification

import scala.collection.mutable.ListBuffer

class CoombTest extends Specification {

val expectedCoombWinnerList = List(new Candidate("Nashville"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import org.aossie.agora.parser.PreferencesParser
import org.aossie.agora.model.Candidate
import org.specs2.mutable.Specification

import spire.math.Rational

class MeekSTVTest extends Specification {

val expectedMeekSTVWinnerList = List(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package org.aossie.agora.votecounter

import ammonite.ops._
import org.specs2.mutable.Specification

import org.aossie.agora.Main;
import scala.collection.mutable

/** This class executes all vote counting methods. Its purpose is merely to check that Agora
* compiles and does not throw any obvious run-time exceptions.
Expand All @@ -25,7 +23,7 @@ class RunAllMethods extends Specification {
def run(method: String): Boolean = {
(1 to 13)
.map(i => str(i))
.map { i: String =>
.map { i =>
test(method, s"$i-example.txt", s"$i-candidates.txt")
}
.reduce((b1, b2) => b1 && b2)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import org.aossie.agora.parser.PreferencesParser
import org.aossie.agora.model.Candidate
import org.specs2.mutable.Specification

import spire.math.Rational

class SatisfactionApprovalVotingTest extends Specification {

val expectedSatisfactionApprovalWinnerList = List(new Candidate("D"), new Candidate("C"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package org.aossie.agora.votecounter
import org.aossie.agora.model.Candidate
import org.aossie.agora.parser.CandidatesParser
import org.aossie.agora.parser.PreferencesParser
import org.aossie.agora.votecounter.SimpleSTV
import org.specs2.mutable.Specification

class SimpleSTVRuleTest extends Specification {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package org.aossie.agora.votecounter

import org.aossie.agora.model.Candidate
import org.aossie.agora.model.Parameters
import org.aossie.agora.parser.CandidatesParser
import org.aossie.agora.parser.ParameterParser
import org.aossie.agora.parser.PreferencesParser
import org.aossie.agora.votecounter.SuperMajority
import org.specs2.mutable.Specification
import spire.math.Rational

class SuperMajorityTest extends Specification {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package org.aossie.agora.votecounter
import org.aossie.agora.model.Candidate
import org.aossie.agora.parser.CandidatesParser
import org.aossie.agora.parser.PreferencesParser
import org.aossie.agora.votecounter.Veto
import org.specs2.mutable.Specification

class VetoTest extends Specification {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ object SinglePeakAnalyser extends PreferenceAnalysisMethod[Candidate, Ballot] {
getSinglePeakAxis(election, candidates) match {
case Some(axis) =>
if (isCompatibleAxis(axis, election)) {
println("Single Peaked with respect to ", axis.mkString(" > "))
println("Single Peaked with respect to " + axis.mkString(" > "))
true
} else {
println("\n\nNot Single Peaked!\n\n")
Expand Down
51 changes: 29 additions & 22 deletions modules/core/src/main/scala/org/aossie/agora/model/Election.scala
Original file line number Diff line number Diff line change
@@ -1,27 +1,32 @@
package org.aossie.agora.model

import scala.collection._
import scala.collection.generic._
import scala.collection.mutable.Builder
import scala.collection.mutable.{HashMap => MMap}
import spire.math.Rational

import scala.language.higherKinds
import scala.collection.compat.IterableOnce

class Election[C <: Candidate, B[CC >: C <: Candidate] <: Ballot[CC]](val ballots: Seq[B[C]])
extends Seq[B[C]]
with SeqLike[B[C], Election[C, B]] {
with SeqOps[B[C], Seq, Election[C, B]] {

override def companion = ballots.companion
// Mandatory overrides of `fromSpecific`, `newSpecificBuilder`,
// and `empty`, from `IterableOps`

override protected def fromSpecific(coll: IterableOnce[B[C]]): Election[C, B] =
Election.fromSpecific(coll)

override protected def newSpecificBuilder: mutable.Builder[B[C], Election[C, B]] =
Election.newBuilder
override def empty: Election[C, B] = Election(Nil)

def iterator = ballots.iterator

def apply(i: Int) = ballots(i)

def length = ballots.length

override protected[this] def newBuilder = Election.newBuilder

override def toString = ballots.map(_.toString).mkString("\n")

def firstVotes(candidates: List[C]): Map[C, Rational] = {
Expand All @@ -35,29 +40,31 @@ class Election[C <: Candidate, B[CC >: C <: Candidate] <: Ballot[CC]](val ballot
m
}

lazy val weight = ((ballots.map(_.weight)) :\ Rational(0, 1))(_ + _)
lazy val weight = ballots.map(_.weight).foldRight(Rational(0, 1))(_ + _)

}

object Election {
// Mandatory factory methods: `empty`, `newBuilder`
// and `fromSpecific`

def newBuilder[C <: Candidate, B[CC >: C <: Candidate] <: Ballot[CC]] =
new mutable.Builder[B[C], Election[C, B]] {
private[this] val base = Seq().genericBuilder[B[C]]
override def +=(e: B[C]) = { base += e; this }
override def clear() = base.clear()
override def result() = new Election[C, B](base.result())
}

implicit def canBuildFrom[C <: Candidate, B[CC >: C <: Candidate] <: Ballot[CC]]
: CanBuildFrom[Election[C, B], B[C], Election[C, B]] =
new CanBuildFrom[Election[C, B], B[C], Election[C, B]] {
def apply(from: Election[C, B]): Builder[B[C], Election[C, B]] = newBuilder[C, B]
def apply(): Builder[B[C], Election[C, B]] = newBuilder[C, B]
}
def empty[C <: Candidate, B[CC >: C <: Candidate] <: Ballot[CC]]: Election[C, B] = apply(Nil)

def newBuilder[C <: Candidate, B[CC >: C <: Candidate] <: Ballot[CC]]
: mutable.Builder[B[C], Election[C, B]] =
mutable.Seq.newBuilder[B[C]].mapResult(apply)

def fromSpecific[C <: Candidate, B[CC >: C <: Candidate] <: Ballot[CC]](
it: IterableOnce[B[C]]
): Election[C, B] = it match {
case seq: collection.Seq[B[C]] => apply(seq)
case _ => apply(mutable.Seq.from(it))
}

// def apply[B <: Ballot](ballots: B*) = new Election(ballots)
def apply[C <: Candidate, B[CC >: C <: Candidate] <: Ballot[CC]](ballots: Seq[B[C]]) =
def apply[C <: Candidate, B[CC >: C <: Candidate] <: Ballot[CC]](
ballots: Seq[B[C]]
): Election[C, B] =
new Election(ballots)

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ case class MajorityBonus(jackpot: Double, bonus: Double)
object MajorityBonus {

implicit val majorityBonusReader: Reads[MajorityBonus] =
(__ \ "jackpot").read[Double].and((__ \ "bonus").read[Double])(MajorityBonus.apply _)
(__ \ "jackpot").read[Double].and((__ \ "bonus").read[Double])(MajorityBonus.apply)

implicit val majorityBonusWriter: Writes[MajorityBonus] =
(__ \ "jackpot").write[Double].and((__ \ "bonus").write[Double])(unlift(MajorityBonus.unapply))
(__ \ "jackpot").write[Double].and((__ \ "bonus").write[Double])(m => (m.jackpot, m.bonus))

}

Expand All @@ -20,12 +20,11 @@ case class ComparisonSets(set1: Array[String], set2: Array[String])
object ComparisonSets {

implicit val comparisonSetsReader: Reads[ComparisonSets] =
(__ \ "set1").read[Array[String]].and((__ \ "set2").read[Array[String]])(ComparisonSets.apply _)
(__ \ "set1").read[Array[String]].and((__ \ "set2").read[Array[String]])(ComparisonSets.apply)

implicit val comparisonSetsWriter: Writes[ComparisonSets] =
(__ \ "set1")
.write[Array[String]]
.and((__ \ "set2").write[Array[String]])(unlift(ComparisonSets.unapply))
implicit val comparisonSetsWriter: Writes[ComparisonSets] = (__ \ "set1")
.write[Array[String]]
.and((__ \ "set2").write[Array[String]])(s => (s.set1, s.set2))

}

Expand Down Expand Up @@ -61,6 +60,6 @@ object Parameters {
.and((__ \ "majority_bonus").readNullable[MajorityBonus])
.and((__ \ "probability_distribution").readNullable[Array[Map[String, Double]]])
.and((__ \ "comparison_sets").readNullable[ComparisonSets])
.and((__ \ "majorityPercentage").readNullable[Double])(Parameters.apply _)
.and((__ \ "majorityPercentage").readNullable[Double])(Parameters.apply)

}
Loading