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

@forzaken hw1 #4

Open
wants to merge 5 commits into
base: master
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
12 changes: 11 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import scalariform.formatter.preferences._

name := "fpspeedrun"

version := "0.1"
Expand All @@ -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")
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)
1 change: 1 addition & 0 deletions project/build.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sbt.version=1.1.2
1 change: 1 addition & 0 deletions project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
addSbtPlugin("org.scalariform" % "sbt-scalariform" % "1.8.2")
11 changes: 11 additions & 0 deletions src/main/scala/fpspeedrun/Eq.scala
Original file line number Diff line number Diff line change
@@ -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
}
}
}
32 changes: 26 additions & 6 deletions src/main/scala/fpspeedrun/Ord.scala
Original file line number Diff line number Diff line change
@@ -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
}
}
}
}
18 changes: 17 additions & 1 deletion src/main/scala/fpspeedrun/Ratio.scala
Original file line number Diff line number Diff line change
@@ -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
}
}
}
12 changes: 9 additions & 3 deletions src/main/scala/fpspeedrun/syntax.scala
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
27 changes: 27 additions & 0 deletions src/main/scala/main/Day1.scala
Original file line number Diff line number Diff line change
@@ -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)
}