scala – 拉出具有依赖关系的无形多态函数
发布时间:2020-12-16 18:50:02 所属栏目:安全 来源:网络整理
导读:对于无形的新手,我对使用需要一些依赖性的多态函数有疑问.我基本上有这个代码,并希望从run方法中拉出somePoly对象: import shapeless._object SomeObject { type SomeType = Int :+: String :+: (String,Int) :+: CNil def run( someList: List[SomeType],s
|
对于无形的新手,我对使用需要一些依赖性的多态函数有疑问.我基本上有这个代码,并希望从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)
操作的特殊性使得这种方法看起来有点奇怪,但如果我真的需要为不同的副产品做这样的事情,这可能是我选择的解决方案. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- Bash:检索给定相对的绝对路径
- twitter-bootstrap – 无法正确显示Twitter Bootstrap glyp
- Bootstrap 3 jquery事件,用于活动标签页更改
- 采用list传输格式的webservice实现控制远程设备
- django – docker-compose:尝试“up”容器时没有枪炮
- RMI、Hessian、Burlap、Httpinvoker、WebService的比较
- “yum历史”中的用户名来自哪里?
- bootstrap 之准备开发
- angularjs – angular复选框,使用ng-click重置绑定到复选框
- bash – 当传递引用的参数时,为什么我得到“/ bin / sh:参
