Skip to content
This repository has been archived by the owner on Jun 1, 2021. It is now read-only.

Commit

Permalink
Add GeneralizedDescendant and GeneralizedSuccessor
Browse files Browse the repository at this point in the history
  • Loading branch information
sd-yip committed Feb 6, 2019
1 parent 6252f54 commit 6ca9824
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 48 deletions.
29 changes: 16 additions & 13 deletions core/src/main/scala/eyrie/instances/DescendantInstances.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,30 @@ import eyrie.ops._

import scala.Function.const

private[eyrie]
trait GeneralizedDescendantInstances {
implicit def eyrieNonDescendantBasedInstance[A, L, R](
implicit
A: Subdivision[A, L, R],
L: NonDescendant[L],
R: NonDescendant[R]
): NonDescendant[A] =
new NonDescendant[A] { }
}

private[eyrie]
trait DescendantInstances {
implicit def eyrieDescendantBasedInstance[A, LA, RA, B](
implicit def eyrieDescendantBasedInstance[A, L, R, B](
implicit
A: Subdivision[A, LA, RA],
LA: Descendant[LA, B],
RA: Descendant[RA, B]
A: Subdivision[A, L, R],
L: Descendant[L, B],
R: Descendant[R, B]
): Descendant[A, B] =
new Descendant[A, B] {
override
def root: A => B =
A.subdivide(_).fold(LA.root, RA.root)
A.subdivide(_).fold(L.root, R.root)
}

implicit def eyrieNonDescendantBasedInstance[A, LA, RA](
implicit
A: Subdivision[A, LA, RA],
LA: NonDescendant[LA],
RA: NonDescendant[RA]
): NonDescendant[A] =
eyrieDescendantBasedInstance[A, LA, RA, Nothing]
}

private[eyrie]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,17 @@ trait DiPotentialSuccessorInstances {
implicit def eyriePotentialSuccessorBasedInstance[A, LA, RA, LB, RB, C](
implicit
A: Subdivision[A, LA, RA],
L: PotentialSuccessor[LA, LB, C],
R: PotentialSuccessor[RA, RB, C]
LA: PotentialSuccessor[LA, LB, C],
RB: PotentialSuccessor[RA, RB, C]
): DiPotentialSuccessor[A, LB, RB, C] =
new DiPotentialSuccessor[A, LB, RB, C] {
override
def parentEitherOption: A => Option[Either[LB, RB]] =
A.subdivide(_).fold(L.parentOption >>> (_.map(_.asLeft)), R.parentOption >>> (_.map(_.asRight)))
A.subdivide(_).fold(LA.parentOption >>> (_.map(_.asLeft)), RB.parentOption >>> (_.map(_.asRight)))

override
def lastSegmentOption: A => Option[C] =
A.subdivide(_).fold(L.lastSegmentOption, R.lastSegmentOption)
A.subdivide(_).fold(LA.lastSegmentOption, RB.lastSegmentOption)
}
}

Expand Down
15 changes: 13 additions & 2 deletions core/src/main/scala/eyrie/instances/SuccessorInstances.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,20 @@ import cats.instances.all._
import cats.syntax.compose._
import eyrie.ops._

private[eyrie]
trait GeneralizedSuccessorInstances {
implicit def eyrieNonSuccessorBasedInstance[A, L, R](
implicit
A: Subdivision[A, L, R],
L: NonSuccessor[L],
R: NonSuccessor[R]
): NonSuccessor[A] =
new NonSuccessor[A] { }
}

private[eyrie]
trait SuccessorInstances {
implicit def eyrieDiSuccessorBasedInstance[A, B, C, L, R](
implicit def eyrieDiSuccessorBasedInstance[A, L, R, B, C](
implicit
A: DiSuccessor[A, L, R, C],
L: Convertible[L, B],
Expand Down Expand Up @@ -76,7 +87,7 @@ trait PotentialSuccessorInstances {
A.subdivide(_).toOption.map(R.lastSegment)
}

implicit def eyrieDiPotentialSuccessorBasedInstance[A, C, L, R](
implicit def eyrieDiPotentialSuccessorBasedInstance[A, L, R, C](
implicit
A: DiPotentialSuccessor[A, L, R, C],
L: Convertible[L, A],
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/scala/eyrie/ops/Descendant.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package eyrie.ops
import eyrie.instances.{DescendantByInputInstances, DescendantInstances}
import simulacrum.typeclass

trait Descendant[A, B] {
trait Descendant[A, B] extends GeneralizedDescendant[A, B] {
def root: A => B
}

Expand Down
7 changes: 7 additions & 0 deletions core/src/main/scala/eyrie/ops/GeneralizedDescendant.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package eyrie.ops

import eyrie.instances.GeneralizedDescendantInstances

trait GeneralizedDescendant[A, B]

object GeneralizedDescendant extends GeneralizedDescendantInstances
7 changes: 7 additions & 0 deletions core/src/main/scala/eyrie/ops/GeneralizedSuccessor.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package eyrie.ops

import eyrie.instances.GeneralizedSuccessorInstances

trait GeneralizedSuccessor[A, B, C]

object GeneralizedSuccessor extends GeneralizedSuccessorInstances
2 changes: 1 addition & 1 deletion core/src/main/scala/eyrie/ops/Successor.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package eyrie.ops
import eyrie.instances.{SuccessorByInputInstances, SuccessorInstances}
import simulacrum.typeclass

trait Successor[A, B, C] {
trait Successor[A, B, C] extends GeneralizedSuccessor[A, B, C] {
def parent: A => B
def lastSegment: A => C
}
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/scala/eyrie/ops/package.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package eyrie

package object ops {
type NonSuccessor[A] = Successor[A, Nothing, Nothing]
type NonSuccessor[A] = GeneralizedSuccessor[A, Nothing, Nothing]

type TrivialDescendant[A] = Descendant[A, A]
type NonDescendant[A] = Descendant[A, Nothing]
type NonDescendant[A] = GeneralizedDescendant[A, Nothing]
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@ object RelativeFilePathDescendant extends NonDescendant[RelativeFile[Any]]
with Successor[RelativeFile[Any], FilePath.Relative[Any], FileName[Any]] {
import eyrie.file.syntax.asJava._

override
def root: RelativeFile[Any] => Nothing =
_ => throw new UnsupportedOperationException("Unexpected invocation")

private
def relative[S](path: Path): FilePath.Relative[S] =
if (path.getNameCount < 2 && Option(path.getFileName).forall(_.toString.isEmpty)) {
Expand Down Expand Up @@ -81,32 +77,12 @@ object AbsoluteFilePathDescendant extends Descendant[AbsoluteFile[Any], RootDire

private
object FilePathNonDescendant extends NonDescendant[IdentityFilePath[Any]]
with NonSuccessor[IdentityFilePath[Any]] {
override
def root: IdentityFilePath[Any] => Nothing =
_ => throw new UnsupportedOperationException("Unexpected invocation")

override
def parent: IdentityFilePath[Any] => Nothing =
_ => throw new UnsupportedOperationException("Unexpected invocation")

override
def lastSegment: IdentityFilePath[Any] => Nothing =
_ => throw new UnsupportedOperationException("Unexpected invocation")
}
with NonSuccessor[IdentityFilePath[Any]]

private
object FilePathTrivialDescendant extends TrivialDescendant[RootDirectory[Any]]
with NonSuccessor[RootDirectory[Any]] {
override
def root: RootDirectory[Any] => RootDirectory[Any] =
identity

override
def parent: RootDirectory[Any] => Nothing =
_ => throw new UnsupportedOperationException("Unexpected invocation")

override
def lastSegment: RootDirectory[Any] => Nothing =
_ => throw new UnsupportedOperationException("Unexpected invocation")
}

0 comments on commit 6ca9824

Please sign in to comment.