-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add Scala 3 to the build * Migrate Selector from shapeless-2 to Scala 3 * Migrate Replacer from shapeless-2 to Scala 3 * Migrate ForImpl * Fix UnzipDerivationSpec for Scala 3 * Apply DRY to ForImpl for Scala 2 sources * Add some notes for Replacer and Selector * Add rectification on the site about available Scala versions
- Loading branch information
Showing
10 changed files
with
139 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package zipper | ||
|
||
import shapeless.{Generic, HList} | ||
import shapeless.ops.hlist.{Replacer, Selector} | ||
|
||
private[zipper] trait ForImpl extends ForImplScalaVersionSpecific { | ||
implicit def `Unzip List-based`[A, L <: HList]( | ||
implicit generic: Generic.Aux[A, L], | ||
select: Selector[L, List[A]], | ||
replace: Replacer.Aux[L, List[A], List[A], (List[A], L)] | ||
): Unzip[A] = new Unzip[A] { | ||
def unzip(node: A): List[A] = select(generic.to(node)) | ||
|
||
def zip(node: A, children: List[A]): A = generic.from(replace(generic.to(node), children)._2) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package zipper | ||
|
||
import contrib.shapeless3.{Replacer, Selector} | ||
import shapeless3.deriving.K0 | ||
import shapeless3.deriving.K0.* | ||
|
||
import scala.collection.Factory | ||
|
||
private[zipper] trait ForImpl { | ||
given unzipListBased[A, L <: Tuple](using | ||
generic: K0.ProductGeneric[A] { type MirroredElemTypes = L }, | ||
selector: Selector[L, List[A]], | ||
replacer: Replacer.Aux[L, List[A], List[A], (List[A], L)] | ||
): Unzip[A] with { | ||
def unzip(node: A): List[A] = selector(generic.toRepr(node)) | ||
def zip(node: A, children: List[A]): A = { | ||
val repr = replacer(generic.toRepr(node), children) | ||
generic.fromRepr(repr._2) | ||
} | ||
} | ||
|
||
class For[A, Coll[X] <: Seq[X]]: | ||
/** Derive an instance of `Unzip[A]` */ | ||
inline given derive[L <: Tuple](using | ||
generic: K0.ProductGeneric[A] { type MirroredElemTypes = L }, | ||
selector: Selector[L, Coll[A]], | ||
replacer: Replacer.Aux[L, Coll[A], Coll[A], (Coll[A], L)], | ||
factory: Factory[A, Coll[A]] | ||
): Unzip[A] with { | ||
def unzip(node: A): List[A] = selector(generic.toRepr(node)).toList | ||
def zip(node: A, children: List[A]): A = { | ||
val repr = replacer(generic.toRepr(node), children.to(factory)) | ||
generic.fromRepr(repr._2) | ||
} | ||
} | ||
|
||
object For: | ||
/** | ||
* @tparam A The type of the tree-like data structure | ||
* @tparam Coll The type of the collection used for recursion (e.g. Vector) | ||
*/ | ||
def apply[A, Coll[X] <: Seq[X]]: For[A, Coll] = new For[A, Coll] | ||
} |
33 changes: 33 additions & 0 deletions
33
shared/src/main/scala-3/zipper/contrib/shapeless3/Replacer.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package contrib.shapeless3 | ||
|
||
/** | ||
* This is ported from [[shapeless.ops.hlist.Replacer Replacer]] from shapeless-2. | ||
* At the moment of implementation, there is no direct support in shapeless-3. | ||
* We should give up on it once it arrives in the library. | ||
*/ | ||
trait Replacer[L <: Tuple, U, V]: | ||
type Out <: Tuple | ||
def apply(t: L, v: V): Out | ||
|
||
object Replacer: | ||
def apply[L <: Tuple, U, V](using r: Replacer[L, U, V]): Aux[L, U, V, r.Out] = r | ||
|
||
type Aux[L <: Tuple, U, V, Out0] = Replacer[L, U, V] { type Out = Out0 } | ||
|
||
given tupleReplacer1[T <: Tuple, U, V]: Aux[U *: T, U, V, (U, V *: T)] = | ||
new Replacer[U *: T, U, V] { | ||
type Out = (U, V *: T) | ||
|
||
def apply(l: U *: T, v: V): Out = (l.head, v *: l.tail) | ||
} | ||
|
||
given tupleReplacer2[H, T <: Tuple, U, V, OutT <: Tuple](using | ||
ut: Aux[T, U, V, (U, OutT)]): Aux[H *: T, U, V, (U, H *: OutT)] = | ||
new Replacer[H *: T, U, V] { | ||
type Out = (U, H *: OutT) | ||
|
||
def apply(l: H *: T, v: V): Out = { | ||
val (u, outT) = ut(l.tail, v) | ||
(u, l.head *: outT) | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
shared/src/main/scala-3/zipper/contrib/shapeless3/Selector.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package contrib.shapeless3 | ||
|
||
/** | ||
* This is ported from [[shapeless.ops.hlist.Selector Selector]] from shapeless-2. | ||
* At the moment of implementation, there is no direct support in shapeless-3. | ||
* We should give up on it once it arrives in the library. | ||
*/ | ||
trait Selector[L <: Tuple, U]: | ||
def apply(t: L): U | ||
|
||
object Selector: | ||
given[H, T <: Tuple]: Selector[H *: T, H] with { | ||
def apply(t: H *: T): H = t.head | ||
} | ||
|
||
given[H, T <: Tuple, U] (using s: Selector[T, U]): Selector[H *: T, U] with { | ||
def apply(t: H *: T): U = s (t.tail) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,3 @@ | ||
package zipper | ||
|
||
import shapeless.{HList, Generic} | ||
import shapeless.ops.hlist.{Selector, Replacer} | ||
|
||
private[zipper] trait GenericUnzipInstances extends ForImpl { | ||
implicit def `Unzip List-based`[A, L <: HList]( | ||
implicit generic: Generic.Aux[A, L], | ||
select: Selector[L, List[A]], | ||
replace: Replacer.Aux[L, List[A], List[A], (List[A], L)] | ||
): Unzip[A] = new Unzip[A] { | ||
def unzip(node: A): List[A] = select(generic.to(node)) | ||
def zip(node: A, children: List[A]): A = generic.from(replace(generic.to(node), children)._2) | ||
} | ||
} | ||
private[zipper] trait GenericUnzipInstances extends ForImpl |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters