flatMap 与 Shapeless 产量 FlatMapper 未找到 [英] flatMap with Shapeless yield FlatMapper not found
问题描述
我正在尝试定义一些这样的结构
I'm trying to define some structure like this
case class Transformer[From, To](
name: String,
get: PaymentEvent => From,
to: From => To
我想过滤名称属于 Set 的元素
I want to filter elements with names that are part of a Set
class filterName(names: Set[String]) extends lowPriority {
implicit def get[From, To] = at[Transformer[From, To]]{ trans =>
if (names.contains(trans.name))
trans :: HNil
else
HNil
}
}
这是具体的值:
type HTransformer = Transformer[String, String] :: Transformer[Long, Long] :: HNil
当我想将函数应用到值时
When I want to apply the function to the value
private def filter(fields: HTransformer, names: Set[String]): HTransformer = {
fields.flatMap(new filterName(names))
}
编译器报错:
Error:(42, 19) could not find implicit value for parameter mapper: shapeless.ops.hlist.FlatMapper[f.type,shapeless.::[com.ingenico.datalake.Transformer[String,String],shapeless.::[com.ingenico.datalake.Transformer[Long,Long],shapeless.HNil]]]
fields.flatMap(new filterName(names))
推荐答案
我猜你误解了类型级计算.
I guess you misunderstand type-level calculations.
如果你想根据一个元素是否是 Set 的一部分来过滤一个 hlist,那么你必须在编译时知道这一点(如果一个元素是 Set 的一部分),但实际上你只在运行时知道这一点.所以 filterName
将不起作用.
If you want to filter an hlist depending on whether an element is a part of the Set then you must know this (if an element is a part of the Set) at compile time but actually you know this only at runtime. So filterName
will not work.
例如,您可以将 hlist 转换为列表,并在运行时将其过滤为普通集合.
For example you can transform the hlist to a list and filter it as an ordinary collection at runtime.
这篇关于flatMap 与 Shapeless 产量 FlatMapper 未找到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!