diff --git a/build.sbt b/build.sbt index 0346fab..7a2318a 100644 --- a/build.sbt +++ b/build.sbt @@ -1,3 +1,5 @@ +import scalariform.formatter.preferences._ + name := "fpspeedrun" version := "0.1" @@ -9,4 +11,12 @@ scalacOptions += "-Ypartial-unification" libraryDependencies += "org.typelevel" %% "cats-core" % "1.1.0" libraryDependencies += "org.typelevel" %% "cats-effect" % "1.0.0-RC2" -addCompilerPlugin("org.spire-math" %% "kind-projector" % "0.9.7") \ No newline at end of file +addCompilerPlugin("org.spire-math" %% "kind-projector" % "0.9.7") + +scalariformPreferences := scalariformPreferences.value + .setPreference(DanglingCloseParenthesis, Force) + .setPreference(AlignArguments, true) + .setPreference(AlignParameters, true) + .setPreference(AlignSingleLineCaseStatements, true) + .setPreference(DanglingCloseParenthesis, Force) + .setPreference(FirstParameterOnNewline, Force) diff --git a/project/build.properties b/project/build.properties new file mode 100644 index 0000000..0531343 --- /dev/null +++ b/project/build.properties @@ -0,0 +1 @@ +sbt.version=1.1.2 diff --git a/project/plugins.sbt b/project/plugins.sbt new file mode 100644 index 0000000..3a8b7aa --- /dev/null +++ b/project/plugins.sbt @@ -0,0 +1 @@ +addSbtPlugin("org.scalariform" % "sbt-scalariform" % "1.8.2") diff --git a/src/main/scala/fpspeedrun/Eq.scala b/src/main/scala/fpspeedrun/Eq.scala index 7343eaa..bfbf9c1 100644 --- a/src/main/scala/fpspeedrun/Eq.scala +++ b/src/main/scala/fpspeedrun/Eq.scala @@ -1,5 +1,16 @@ package fpspeedrun +import fpspeedrun.syntax.eq._ + trait Eq[T] { def ===(x: T, y: T): Boolean } + +object Eq { + implicit def eqSeq[T: Eq]: Eq[Seq[T]] = (x: Seq[T], y: Seq[T]) => { + x.size == y.size && x.zip(y).forall { + case (a, b) => + a === b + } + } +} diff --git a/src/main/scala/fpspeedrun/Ord.scala b/src/main/scala/fpspeedrun/Ord.scala index 231eec3..e0cdeb4 100644 --- a/src/main/scala/fpspeedrun/Ord.scala +++ b/src/main/scala/fpspeedrun/Ord.scala @@ -1,15 +1,35 @@ package fpspeedrun import fpspeedrun.Ord.Compare +import fpspeedrun.syntax.ord._ -trait Ord[T] extends Eq[T]{ +trait Ord[T] extends Eq[T] { def compare(x: T, y: T): Compare + + def ===(x: T, y: T): Boolean = { + compare(x, y) match { + case Compare.EQ => true + case _ => false + } + } } -object Ord{ +object Ord { sealed trait Compare - object Compare{ - case object LT //less than - case object EQ //equals to - case object GT //greater than + object Compare { + case object LT extends Compare //less than + case object EQ extends Compare //equals to + case object GT extends Compare //greater than + } + + implicit def ordSeq[T: Ord]: Ord[Seq[T]] = (x: Seq[T], y: Seq[T]) => { + x.zip(y) + .collectFirst { case (a, b) if (a <> b) != Compare.EQ => a <> b } + .getOrElse { + (x.size, y.size) match { + case (a, b) if a > b => Compare.GT + case (a, b) if a < b => Compare.LT + case (a, b) if a == b => Compare.EQ + } + } } } diff --git a/src/main/scala/fpspeedrun/Ratio.scala b/src/main/scala/fpspeedrun/Ratio.scala index bc3fe1b..ea537c7 100644 --- a/src/main/scala/fpspeedrun/Ratio.scala +++ b/src/main/scala/fpspeedrun/Ratio.scala @@ -1,4 +1,20 @@ package fpspeedrun -final case class Ratio() +import fpspeedrun.Ord.Compare +final case class Ratio(numerator: Int, denominator: Int) + +object Ratio { + implicit val eqRatio: Eq[Ratio] = (x: Ratio, y: Ratio) => { + x.numerator.toLong * y.denominator.toLong == + x.denominator.toLong * y.numerator.toLong + } + + implicit val ordRatio: Ord[Ratio] = (x: Ratio, y: Ratio) => { + (x.numerator.toLong * y.denominator.toLong, x.denominator.toLong * y.numerator.toLong) match { + case (a, b) if a > b => Compare.GT + case (a, b) if a == b => Compare.EQ + case (a, b) if a < b => Compare.LT + } + } +} diff --git a/src/main/scala/fpspeedrun/syntax.scala b/src/main/scala/fpspeedrun/syntax.scala index d4f9a19..b2780d9 100644 --- a/src/main/scala/fpspeedrun/syntax.scala +++ b/src/main/scala/fpspeedrun/syntax.scala @@ -1,9 +1,15 @@ package fpspeedrun object syntax { - object eq{ - implicit class EqOps[T](val x: T) extends AnyVal{ - def ===(y: T)(implicit eq: Eq[T]): Boolean = eq.===(x, y) + object eq { + implicit class EqOps[T](val x: T) extends AnyVal { + def ===(y: T)(implicit ev: Eq[T]): Boolean = implicitly[Eq[T]].===(x, y) + } + } + + object ord { + implicit class OrdOps[T](val x: T) extends AnyVal { + def <>(y: T)(implicit ev: Ord[T]): Ord.Compare = implicitly[Ord[T]].compare(x, y) } } } diff --git a/src/main/scala/main/Day1.scala b/src/main/scala/main/Day1.scala new file mode 100644 index 0000000..34c7237 --- /dev/null +++ b/src/main/scala/main/Day1.scala @@ -0,0 +1,27 @@ +package main + +import fpspeedrun.Ord.Compare +import fpspeedrun.Ratio +import fpspeedrun.syntax.eq._ +import fpspeedrun.syntax.ord._ +import fpspeedrun.Ratio._ + +object Day1 extends App { + assert(Ratio(1, 2) === Ratio(1, 2)) + assert(!(Ratio(1, 2) === Ratio(1, 3))) + assert(Seq(Ratio(1, 2), Ratio(1, 3)) === Seq(Ratio(1, 2), Ratio(1, 3))) + assert(!(Seq(Ratio(1, 2), Ratio(1, 3)) === Seq(Ratio(1, 2), Ratio(1, 4)))) +} + +object Day1Homework extends App { + assert((Ratio(1, 2) <> Ratio(2, 3)) == Compare.LT) + assert((Ratio(5, 4) <> Ratio(2, 3)) == Compare.GT) + assert((Ratio(5, 4) <> Ratio(5, 4)) == Compare.EQ) + + assert((Seq(Ratio(1, 2), Ratio(2, 3)) <> Seq(Ratio(1, 2), Ratio(2, 4))) == Compare.GT) + assert((Seq(Ratio(1, 2), Ratio(2, 4)) <> Seq(Ratio(1, 2), Ratio(2, 3))) == Compare.LT) + + assert((Seq(Ratio(1, 2), Ratio(2, 3), Ratio(2, 3)) <> Seq(Ratio(1, 2), Ratio(2, 3))) == Compare.GT) + assert((Seq(Ratio(1, 2), Ratio(2, 3)) <> Seq(Ratio(1, 2), Ratio(2, 3), Ratio(2, 3))) == Compare.LT) + assert((Seq(Ratio(1, 2), Ratio(2, 3)) <> Seq(Ratio(1, 2), Ratio(2, 3))) == Compare.EQ) +}