Skip to content
This repository has been archived by the owner on Mar 11, 2020. It is now read-only.

Port to cats #25

Merged
merged 8 commits into from
Dec 19, 2017
Merged
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
18 changes: 3 additions & 15 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,13 @@ matrix:
include:
# scala 2.10
- jdk: oraclejdk8
scala: 2.10.6
env: SCALAZ_VERSION=7.1.11
- jdk: oraclejdk8
scala: 2.10.6
env: SCALAZ_VERSION=7.2.7
scala: 2.10.7
# scala 2.11
- jdk: oraclejdk8
scala: 2.11.8
env: SCALAZ_VERSION=7.1.11
- jdk: oraclejdk8
scala: 2.11.8
env: SCALAZ_VERSION=7.2.7
scala: 2.11.12
# scala 2.12
- jdk: oraclejdk8
scala: 2.12.1
env: SCALAZ_VERSION=7.1.11
- jdk: oraclejdk8
scala: 2.12.1
env: SCALAZ_VERSION=7.2.7
scala: 2.12.4

branches:
only:
Expand Down
11 changes: 0 additions & 11 deletions codecs/build.sbt
Original file line number Diff line number Diff line change
@@ -1,14 +1,3 @@
scalaCheckVersion := {
scalazVersion.value match {
case VersionNumber(Seq(7, 2, _*), _, _) => "1.12.6"
case VersionNumber(Seq(7, 1, _*), _, _) =>
CrossVersion.partialVersion(scalaVersion.value) match {
case Some((2, n)) if n <= 11 => "1.11.4"
case Some((2, n)) => "1.11.6"
}
}
}

resolvers += Resolver.sonatypeRepo("releases")

libraryDependencies += "org.scodec" %% "scodec-core" % "1.10.3"
3 changes: 0 additions & 3 deletions codecs/src/main/scala/GraphCodecs.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@
package quiver

import scodec._
import scodec.bits._
import shapeless._
import scalaz.\/

object GraphCodecs {

Expand Down
1 change: 0 additions & 1 deletion codecs/src/test/scala/GraphCodecsTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import org.scalacheck._
import org.scalacheck.Prop._
import scodec.codecs
import scodec.Attempt
import scalaz.\/

object GraphCodecsTest extends Properties("codecs"){
import GraphGen.{arbitraryNode,arbitraryEdge,arbitraryGraph}
Expand Down
16 changes: 4 additions & 12 deletions core/build.sbt
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
scalaCheckVersion := {
scalazVersion.value match {
case VersionNumber(Seq(7, 2, _*), _, _) => "1.12.6"
case VersionNumber(Seq(7, 1, _*), _, _) =>
CrossVersion.partialVersion(scalaVersion.value) match {
case Some((2, n)) if n <= 11 => "1.11.4"
case Some((2, n)) => "1.11.6"
}
}
}
val CatsVersion = "1.0.0-RC2"

libraryDependencies ++= Seq(
"org.scalaz" %% "scalaz-core" % scalazVersion.value,
"org.scalaz" %% "scalaz-scalacheck-binding" % scalazVersion.value % "test"
"org.typelevel" %% "cats-free" % CatsVersion,
"org.typelevel" %% "cats-laws" % CatsVersion % "test",
"com.github.alexarchambault" %% "scalacheck-shapeless_1.13" % "1.1.7" % "test"
)

scalacOptions in (Compile,doc) := Seq("-groups", "-implicits")
1 change: 0 additions & 1 deletion core/src/main/scala/Context.scala
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,3 @@ case class GrContext[N,A,B](inAdj: Map[N, Set[B]],
outAdj: Map[N, Set[B]]) {
def toContext(v: N): Context[N,A,B] = Context(toAdj(inAdj), v, label, toAdj(outAdj))
}

6 changes: 3 additions & 3 deletions core/src/main/scala/Decomp.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
//: ----------------------------------------------------------------------------
package quiver

import scalaz.Comonad
import cats.Comonad

/**
* The decomposition of a graph into possibly a detached context focused on one node,
Expand Down Expand Up @@ -132,9 +132,9 @@ object GDecomp {

implicit def gDecompComonad[N,B]: Comonad[({type λ[α] = GDecomp[N,α,B]})#λ] =
new Comonad[({type λ[α] = GDecomp[N,α,B]})#λ] {
def copoint[A](as: GDecomp[N,A,B]) = as.label
def extract[A](as: GDecomp[N,A,B]) = as.label
def map[A,C](as: GDecomp[N,A,B])(f: A => C) = as map f
def cobind[A,C](as: GDecomp[N,A,B])(f: GDecomp[N,A,B] => C) =
def coflatMap[A,C](as: GDecomp[N,A,B])(f: GDecomp[N,A,B] => C) =
as redecorate f
}
}
Expand Down
16 changes: 7 additions & 9 deletions core/src/main/scala/Graph.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,8 @@

package quiver

import compatibility._

import scalaz._
import scalaz.syntax.std.map._
import scalaz.syntax.monoid._
import scalaz.std.vector._
import cats.Monoid
import cats.implicits._

case class Edge[N](from: N, to: N)

Expand Down Expand Up @@ -324,7 +320,7 @@ case class Graph[N,A,B](rep: GraphRep[N,A,B]) {
*/
def updateNode(n: LNode[N,A]): Graph[N,A,B] =
decomp(n.vertex) match {
case Decomp(Some(Context(p, v, l, s)), rest) =>
case Decomp(Some(Context(p, _, _, s)), rest) =>
rest & Context(p, n.vertex, n.label, s)
case _ => this
}
Expand Down Expand Up @@ -667,6 +663,7 @@ case class Graph[N,A,B](rep: GraphRep[N,A,B]) {
*/
def rdfs(vs: Seq[N]): Seq[N] = xdfsWith(vs, _.predecessors, _.vertex)

/*
/**
* Finds the transitive closure of this graph.
* @group dfs
Expand Down Expand Up @@ -725,8 +722,9 @@ case class Graph[N,A,B](rep: GraphRep[N,A,B]) {
case Decomp(Some(c), g) =>
val (xs, g2) = g.xdfWith(d(c), d, f)
val (ys, g3) = g.xdfWith(vs.tail, d, f)
(Tree.Node(f(c), xs.toStream) +: ys, g3)
(Cofree[Stream, C](f(c), Eval.later(xs.toStream)) +: ys, g3)
}
*/

import scala.collection.immutable.Queue

Expand Down Expand Up @@ -906,7 +904,7 @@ case class Graph[N,A,B](rep: GraphRep[N,A,B]) {
* @group bfs
*/
def cheapestPath[C : Monoid : math.Ordering](s: N, t: N, costFkt: (LNode[N,A],B,LNode[N,A]) => C): Option[LPath[N,B]] = {
def costOfPath(p: LPath[N,B]): C = p._2.foldLeft((LNode(p._1, label(p._1).get),mzero[C])){
def costOfPath(p: LPath[N,B]): C = p._2.foldLeft((LNode(p._1, label(p._1).get), Monoid[C].empty)){
case ((last,cost),(n,edgeLabel)) =>
val next = LNode(n,label(n).get)
val addedCost = costFkt(last,edgeLabel,next)
Expand Down
Empty file added core/src/main/scala/Tree.scala
Empty file.
84 changes: 52 additions & 32 deletions core/src/main/scala/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@
//: limitations under the License.
//:
//: ----------------------------------------------------------------------------
import scalaz._
import scalaz.syntax.std.map._
import scalaz.syntax.monoid._
import scalaz.std.vector._

import cats.{Monoid, Order}
import cats.implicits._

/**
* @groupname creation Graph Construction
Expand Down Expand Up @@ -81,42 +80,45 @@ package object quiver {

/** @group instances */
implicit def nodeOrder[N,A](implicit N: Order[N], A: Order[A]): Order[LNode[N,A]] =
Order.order { (a, b) =>
N.order(a.vertex, b.vertex) |+| A.order(a.label, b.label)
}
Order.whenEqual(
Order.by[LNode[N,A], N](_.vertex),
Order.by[LNode[N,A], A](_.label))

/** @group instances */
implicit def ledgeOrder[N,A](implicit N: Order[N], A: Order[A]): Order[LEdge[N,A]] =
Order.order { (a, b) =>
N.order(a.from, b.from) |+| N.order(a.to, b.to) |+| A.order(a.label, b.label)
}
Order.whenEqualMonoid.combineAll(List(
Order.by(_.from),
Order.by(_.to),
Order.by(_.label)))

/** @group instances */
implicit def edgeOrder[N,A](implicit N: Order[N]): Order[Edge[N]] =
Order.order { (a, b) =>
N.order(a.from, b.from) |+| N.order(a.to, b.to)
}
Order.whenEqualMonoid.combineAll(List(
Order.by(_.from),
Order.by(_.to)))

/** @group instances */
implicit def graphOrder[N,A,B](implicit N: Order[N], A: Order[A], B: Order[B]): Order[Graph[N,A,B]] =
Order.order { (a, b) =>
implicit val L = Order[LNode[N,A]].toScalaOrdering
implicit val E = Order[LEdge[N,B]].toScalaOrdering
Order[Vector[LNode[N,A]]].order(a.labNodes.sorted, b.labNodes.sorted) |+|
Order[Vector[LEdge[N,B]]].order(a.labEdges.sorted, b.labEdges.sorted)
}
implicit def graphOrder[N,A,B](implicit N: Order[N], A: Order[A], B: Order[B]): Order[Graph[N,A,B]] = {
implicit val L = Order[LNode[N,A]].toOrdering
implicit val E = Order[LEdge[N,B]].toOrdering
Order.whenEqual(
Order.by(_.labNodes.sorted),
Order.by(_.labEdges.sorted))
}

implicit def contextOrder[N,A,B](implicit N: Order[N], A: Order[A], B: Order[B]): Order[Context[N,A,B]] =
Order.order { (a, b) =>
import scalaz.std.vector._
import scalaz.std.tuple._
N.order(a.vertex, b.vertex) |+| A.order(a.label, b.label) |+| Order[Adj[N,B]].order(a.inAdj, b.inAdj) |+| Order[Adj[N,B]].order(a.outAdj, b.outAdj)
}
implicit def contextOrder[N,A,B](implicit N: Order[N], A: Order[A], B: Order[B]): Order[Context[N,A,B]] = {
implicit val adj = Order[(B, N)].toOrdering
Order.whenEqualMonoid.combineAll(List(
Order.by(_.vertex),
Order.by(_.label),
Order.by(_.inAdj.sorted),
Order.by(_.outAdj.sorted)))
}

implicit def gdecompOrder[N:Order,A:Order,B:Order]: Order[GDecomp[N,A,B]] =
Order.order { (a, b) =>
contextOrder[N,A,B].order(a.ctx, b.ctx) |+| graphOrder[N,A,B].order(a.rest, b.rest)
}
implicit def gDecompOrder[N:Order,A:Order,B:Order]: Order[GDecomp[N,A,B]] =
Order.whenEqual(
Order.by[GDecomp[N,A,B], Context[N,A,B]](_.ctx),
Order.by[GDecomp[N,A,B], Graph[N,A,B]](_.rest))

/**
* An empty graph
Expand Down Expand Up @@ -253,7 +255,25 @@ package object quiver {
* @group instances
*/
implicit def graphMonoid[N,A,B]: Monoid[Graph[N,A,B]] = new Monoid[Graph[N,A,B]] {
def zero = empty
def append(g1: Graph[N,A,B], g2: => Graph[N,A,B]) = g1 union g2
def empty = quiver.empty
def combine(g1: Graph[N,A,B], g2: Graph[N,A,B]) = g1 union g2
}

// Map utilities lost when porting the old implementation
private[quiver] implicit class MapOps[A, B](val self: Map[A, B]) extends AnyVal {
def alter(k: A)(f: Option[B] => Option[B]): Map[A, B] =
f(self.get(k)) match {
case Some(v0) => self.updated(k, v0)
case None => self - k
}

def insertWith(k: A, v: B)(f: (B, B) => B): Map[A, B] =
self.get(k) match {
case Some(v0) => self.updated(k, f(v0, v))
case None => self.updated(k, v)
}

def mapKeys[A1](f: A => A1): Map[A1, B] =
self.map { case (k, v) => f(k) -> v }
}
}
27 changes: 0 additions & 27 deletions core/src/main/scalaz-7.1/compatibility.scala

This file was deleted.

19 changes: 0 additions & 19 deletions core/src/main/scalaz-7.2/compatibility.scala

This file was deleted.

Loading