scala – 拉出具有依赖关系的无形多态函数

对于无形的新手,我对使用需要一些依赖性的多态函数有疑问.我基本上有这个代码,并希望从run方法中拉出somePoly对象:

import shapeless._
object SomeObject {
    type SomeType = Int :+: String :+: (String,Int) :+: CNil

    def run( someList: List[SomeType],someInt:Int,someWord:String ) = {

       object somePoly extends Poly1 {
           implicit def doIt = at[Int]( i => i + someInt + someWord.length)
           implicit def doIt2 = at[String]( i => i.length + someWord.length)
           implicit def doIt3 = at[(String,Int)]( i => i._1.length + someWord.length)
       }

       someList.map( _.map(somePoly) )
    }
}

我想这样做的一种方式就是这样,但它似乎很乱:

object TypeContainer {
  type SomeType = Int :+: String :+: (String,Int) :+: CNil
}
case class SomePolyWrapper( someList: List[TypeContainer.SomeType],someWord:String ){
    object somePoly extends Poly1 {
       implicit def doIt = at[Int]( i => i + someInt + someWord.length)
       implicit def doIt2 = at[String]( i => i.length + someWord.length)
      implicit def doIt3 = at[(String,Int)]( i => i._1.length + someWord.length)
   }
}  
object SomeObject {

    def run( someList: List[TypeContainer.SomeType],someWord:String ) = {

       val somePolyWrapper = SomePolyWrapper(someList,someInt,someWord)

       someList.map( _.map(somePolyWrapper.somePoly) )
    }
}

有人有什么建议吗?

解决方法

Scala的隐式解析系统的局限性意味着Poly定义需要是一个稳定的标识符,这使得这种事情比它应该更痛苦.正如我在Gitter上提到的,我知道有几种解决方法(可能还有其他的).

一种方法是使Poly1成为PolyN,其中额外的参数用于someInt和someWord值.如果您在HList上进行映射,那么您可以使用mapConst和zip来使输入HList具有正确的形状.我从来没有为共同产品做过这个,但类似的东西可能会起作用.

另一种方法是使用自定义类型类.在您的情况下,可能看起来像这样:

import shapeless._

trait IntFolder[C <: Coproduct] {
  def apply(i: Int,w: String)(c: C): Int
}

object IntFolder {
  implicit val cnilIntFolder: IntFolder[CNil] = new IntFolder[CNil] {
    def apply(i: Int,w: String)(c: CNil): Int = sys.error("Impossible")
  }

  def instance[H,T <: Coproduct](f: (H,Int,String) => Int)(implicit
    tif: IntFolder[T]
  ): IntFolder[H :+: T] = new IntFolder[H :+: T] {
    def apply(i: Int,w: String)(c: H :+: T): Int = c match {
      case Inl(h) => f(h,i,w)
      case Inr(t) => tif(i,w)(t)
    }
  }

  implicit def iif[T <: Coproduct: IntFolder]: IntFolder[Int :+: T] =
    instance((h,w) => h + i + w.length)

  implicit def sif[T <: Coproduct: IntFolder]: IntFolder[String :+: T] =
    instance((h,w) => h.length + i + w.length)

  implicit def pif[T <: Coproduct: IntFolder]: IntFolder[(String,Int) :+: T] =
    instance((h,w) => h._1.length + i + w.length)
}

然后你可以编写一个更通用的运行版本:

def run[C <: Coproduct](
  someList: List[C],someInt: Int,someWord: String
)(implicit cif: IntFolder[C]): List[Int] = someList.map(cif(someInt,someWord))

并像这样使用它:

scala> run(List(Coproduct[SomeType](1)),10,"foo")
res0: List[Int] = List(14)

scala> run(List(Coproduct[SomeType](("bar",1))),"foo")
res1: List[Int] = List(16)

操作的特殊性使得这种方法看起来有点奇怪,但如果我真的需要为不同的副产品做这样的事情,这可能是我选择的解决方案.

相关文章

共收录Twitter的14款开源软件,第1页Twitter的Emoji表情 Tw...
Java和Scala中关于==的区别Java:==比较两个变量本身的值,即...
本篇内容主要讲解“Scala怎么使用”,感兴趣的朋友不妨来看看...
这篇文章主要介绍“Scala是一种什么语言”,在日常操作中,相...
这篇文章主要介绍“Scala Trait怎么使用”,在日常操作中,相...
这篇文章主要介绍“Scala类型检查与模式匹配怎么使用”,在日...