繁体   English   中英

无形 - 在Coproduct中重复数据删除类型

[英]Shapeless - Deduplicating types in Coproduct

鉴于我有一种Int :+: Int :+: String :+: CNil ,有一种简单的方法可以将它变成Int :+: String :+: CNil

这取决于你所说的“简单”。 我很确定通过编写在Shapeless中现成的联产品操作来执行此操作没有直接的方法,但编写自己的类型类来完成此操作相当简单(至少就这些事情而言) 。

我将假设一些未在您的要求中指定的内容:

  1. 您希望生成的联合产品中的类型是唯一的(例如,您不仅仅像在示例中那样折叠相邻元素)。
  2. 对于不相邻的重复类型,您希望最后一个副本包含在结果中。

如果这些假设不准确,那么调整下面的解决方案并不会太难 - 核心思想是相同的。

完整的解决方案如下所示:

import shapeless.{ :+:, CNil, Coproduct, DepFn1, Inl, Inr }
import shapeless.ops.coproduct.Inject

trait Unique[C <: Coproduct] extends DepFn1[C] {
  type Out <: Coproduct
}

object Unique extends LowPriorityUnique {
  type Aux[C <: Coproduct, Out0 <: Coproduct] = Unique[C] { type Out = Out0 }

  def apply[C <: Coproduct](implicit unC: Unique[C]): Aux[C, unC.Out] = unC

  implicit val uniqueCNil: Aux[CNil, CNil] = new Unique[CNil] {
    type Out = CNil
    def apply(c: CNil): CNil = c
  }

  implicit def uniqueCCons1[L, R <: Coproduct](implicit
    inj: Inject[R, L],
    unR: Unique[R]
  ): Aux[L :+: R, unR.Out] = new Unique[L :+: R] {
    type Out = unR.Out

    def apply(c: L :+: R): unR.Out = unR(
      c match {
        case Inl(l) => inj(l)
        case Inr(r) => r
      }
    )
  }
}

class LowPriorityUnique {
  implicit def uniqueCCons0[L, R <: Coproduct](implicit
    unR: Unique[R]
  ): Unique[L :+: R] { type Out = L :+: unR.Out } = new Unique[L :+: R] {
    type Out = L :+: unR.Out

    def apply(c: L :+: R): L :+: unR.Out = c match {
      case Inl(l) => Inl(l)
      case Inr(r) => Inr(unR(r))
    }
  }
}

我们可以一步一步地完成这个代码。

trait Unique[C <: Coproduct] extends DepFn1[C] {
  type Out <: Coproduct
}

这是我们的类型。 它表征了副产品C ,并且对于任何实例,它具有由C确定的唯一输出类型,也是副产品。 DepFn1我们得到一个方法apply ,它接受一个C并返回一个Out ; 这是我们将在下面的实例中实现的内容。

在伴随对象中,我们有几行基本上是样板 - 它们不是绝对必要的,但它们支持这种类型的方便,惯用:

type Aux[C <: Coproduct, Out0 <: Coproduct] = Unique[C] { type Out = Out0 }

def apply[C <: Coproduct](implicit unC: Unique[C]): Aux[C, unC.Out] = unC

第一行让我们避免在任何地方编写类型细化( Foo[X] { type Bar = Bar0 } ),第二行让我们编写Unique[C]而不是implicitly[Unique[C]] (并且还返回精炼结果无用的未定义的Unique[C] )。

接下来我们有基本情况:

implicit val uniqueCNil: Aux[CNil, CNil] = new Unique[CNil] {
  type Out = CNil
  def apply(c: CNil): CNil = c
}

这是相当简单的:如果我们得到一个空的副产品,我们知道它的元素已经是唯一的。

接下来我们有几个归纳案例。 第一个, uniqueCCons1 ,涵盖了副产品的头部存在于尾部的情况,第二个, uniqueCCons0 ,涵盖了它不存在的情况。 因为uniqueCCons1适用于uniqueCCons0涵盖的case的子集,所以我们必须明确地优先考虑这两个实例。 我正在使用子类来赋予uniqueCCons0较低的优先级,因为我认为这是最简单的方法。

这两个实例的实现可能看起来有点混乱,但逻辑实际上并不复杂。 在这两种情况下,我们都有一个归纳的Unique[R]实例; 所不同的是在1的情况下,我们首先注入头到尾(依托无形的Inject类型的类地看到, L发生在R然后应用unR ,在其中0情况下,我们它仅适用于尾部和离开头不变。

它的工作原理如下:

scala> type C = Int :+: String :+: CNil
defined type alias C

scala> Unique[C]
res0: Unique[Int :+: String :+: shapeless.CNil]{type Out = Int :+: String :+: shapeless.CNil} = LowPriorityUnique$$anon$3@2ef6f000

scala> Unique[C].apply(Inl(1))
res1: Int :+: String :+: shapeless.CNil = Inl(1)

scala> type C2 = Int :+: String :+: Int :+: CNil
defined type alias C2

scala> Unique[C2].apply(Inr(Inr(Inl(1))))
res2: String :+: Int :+: shapeless.CNil = Inr(Inl(1))

scala> Unique[C2].apply(Inl(1))
res3: String :+: Int :+: shapeless.CNil = Inr(Inl(1))

符合我们上面的要求。

这是一个简单的方法吗?

  import shapeless.{:+:, =:!=, CNil, Coproduct, Inl, Inr, unexpected}

  trait Deduplicate[C <: Coproduct] {
    type Out <: Coproduct
    def apply(c: C): Out
  }
  object Deduplicate {
    type Aux[C <: Coproduct, Out0 <: Coproduct] = Deduplicate[C] { type Out = Out0 }
    def instance[C <: Coproduct, Out0 <: Coproduct](f: C => Out0): Aux[C, Out0] = new Deduplicate[C] {
      override type Out = Out0
      override def apply(c: C): Out = f(c)
    }

    implicit def zero: Aux[CNil, CNil] = instance(_ => unexpected)
    implicit def one[H]: Aux[H :+: CNil, H :+: CNil] = instance(identity)
    implicit def duplicates[H, T <: Coproduct](implicit
      dedup: Deduplicate[H :+: T]): Aux[H :+: H :+: T, dedup.Out] = instance {
      case Inl(h) => dedup(Inl(h))
      case Inr(c) => dedup(c)
    }
    implicit def noDuplicates[H, H1, T <: Coproduct](implicit
      dedup: Deduplicate[H1 :+: T],
      ev1: H =:!= H1): Aux[H :+: H1 :+: T, H :+: dedup.Out] = instance {
      case Inl(h) => Inl(h)
      case Inr(c) => Inr(dedup(c))
    }
  }
  implicit class DeduplicateOps[C <: Coproduct](c: C) {
    def deduplicate(implicit dedup: Deduplicate[C]): dedup.Out = dedup(c)
  }

  implicitly[Deduplicate.Aux[String :+: Int :+: Int :+: String :+: String :+: CNil,
    String :+: Int :+: String :+: CNil]]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM