gpt4 book ai didi

Scala 3 - 在一阶类型上提取包装器元组和 InverseMap

转载 作者:行者123 更新时间:2023-12-03 21:10:18 26 4
gpt4 key购买 nike

我正在尝试创建一个函数,它采用高级类型的元组并将函数应用于高级类型中的类型。
在下面的例子中,有一个 trait Get[A]这是我们的高级类型。还有一个Get的元组:(Get[String],Get[Int])以及来自 (String,Int) => Person 的功能.
Scala-3 有一个名为 InverseMap 的匹配类型,它将类型 (Get[String], Get[Int]) 转换为本质上的类型 (String,Int)。
所以最终目标是编写一个函数,它可以接受一个具有任意数量 Get[_] 的元组。类型和输入匹配 InserveMap 类型并最终返回 Get[_] 的函数,其中包装类型是函数的结果。
我试图创建一个名为 genericF 的函数下面显示了所需的行为,尽管它可能不正确 - 但我认为它至少显示了正确的意图。

  case class Person(name: String, age: Int)
trait Get[A] {
def get: A
}
case class Put[A](get: A) extends Get[A]

val t: (Get[String], Get[Int]) = (Put("Bob"), Put(42))

val fPerson: (String,Int) => Person = Person.apply _

def genericF[T<:Tuple,I<:Tuple.InverseMap[T,Get],B](f: I => B, t: T): Get[B] = ???
val person: Get[Person] = genericF(fPerson, t)
我在这里设置了一个 Scastie: https://scastie.scala-lang.org/OleTraveler/QIyNHPLHQIKPv0lgsYbujA/23

最佳答案

您的代码差不多 compiling已经 - 唯一的事情就是 fPerson类型为 (String, Int) => Person而不是 ((String, Int)) => Person (采用元组而不是 2 个单独的参数)。
这个下面的解决方案并不好,尽管它对于 TupleXXL 来说可能更有效。这是一个带有类型类的更好的版本( Scastie ):

val fPerson: ((String, Int)) => Person = Person.apply _

opaque type Extract[GT <: Tuple, RT <: Tuple] = GT => RT
given Extract[EmptyTuple, EmptyTuple] = Predef.identity
given [A, PG <: Tuple, PR <: Tuple](using p: Extract[PG, PR])
as Extract[Get[A] *: PG, A *: PR] = {
case h *: t => h.get *: p(t)
}

def genericF[GT <: Tuple, RT <: Tuple, B](
f: RT => B,
t: GT
)(using extract: Extract[GT, RT]): Get[B] = Put(f(extract(t)))

Here是您可以实现的一种方式 genericF使用 Tuple.InverseMap (请注意,我将两个参数切换为 genericF :
val fPerson: ((String, Int)) => Person = Person.apply _

type ExtractG = [G] =>> G match {
case Get[a] => a
}

type AllGs[T <: Tuple] = T match {
case EmptyTuple => DummyImplicit
case Get[_] *: t => AllGs[t]
case _ => Nothing
}

def extract[T <: Tuple](t: T)(using AllGs[T]): Tuple.InverseMap[T, Get] =
t.map {
[G] => (g: G) => g.asInstanceOf[Get[_]].get.asInstanceOf[ExtractG[G]]
}.asInstanceOf[Tuple.InverseMap[T, Get]]

def genericF[B](
t: Tuple,
f: Tuple.InverseMap[t.type, Get] => B
)(using AllGs[t.type]): Get[B] = Put(f(extract(t)))

val person: Get[Person] = genericF(t, fPerson)
ExtractG是使 PolyFunction编译,因为它要求您将类型构造函数应用于其类型参数。 AllGs是验证元组仅包含 Get s,因为正如 Dmytro Mitin 所指出的那样,否则它不是类型安全的。如果这一切 Get s,类型变为 DummyImplicit ,这是 Scala 为我们提供的。否则,它是 Nothing .我想它可能与其他隐式/给定的冲突 Nothing s 在范围内,但如果你已经有一个,无论如何你都被搞砸了。
请注意,这仅在您拥有 Get 时才有效。如果您还希望它适用于像 (Put[String], GetSubclass[Int]) 这样的元组,则需要进行一些修改。 .

OP 的 Travis Stevens 已设法使上面的解决方案工作而无需创建 AllGs ,通过使用 IsMappedBy .这是他们得到的( Scastie ):
val fPerson: ((String, Int)) => Person = Person.apply _

type ExtractG = [G] =>> G match {
case Get[a] => a
}

def extract[T <: Tuple, I <: Tuple.InverseMap[T, Get]](
t: T
)(using Tuple.IsMappedBy[Get][T]): I =
t.map {
[G] => (g: G) => g.asInstanceOf[Get[_]].get.asInstanceOf[ExtractG[G]]
}.asInstanceOf[I]

def genericF[T <: Tuple, I <: Tuple.InverseMap[T, Get], B](
t: T,
f: I => B
)(using Tuple.IsMappedBy[Get][T]): Get[B] = Put(f(extract(t)))

这是使用依赖类型的一种,只是为了好玩( Scastie ):
type Extract[T <: Tuple] <: Tuple = T match {
case EmptyTuple => EmptyTuple
case Get[a] *: t => a *: Extract[t]
}

type AllGs[T <: Tuple] = T match {
case EmptyTuple => DummyImplicit
case Get[_] *: t => AllGs[t]
case _ => Nothing
}

def genericF[T <: Tuple : AllGs, B](
t: T,
f: Extract[t.type] => B
): Get[B] = {
def extract[T <: Tuple](t: T): Extract[T] = t match {
case _: EmptyTuple => EmptyTuple
case (head *: tail): (Get[_] *: _) => head.get *: extract(tail)
}
Put(f(extract(t)))
}
我希望 Extract不会为 (Put("foo"), 3) 之类的元组编译,但不幸的是, AllGs还是有必要的。

关于Scala 3 - 在一阶类型上提取包装器元组和 InverseMap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64339583/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com