简体   繁体   English

如何使Scala canBuildFrom构建从Seq到Set的集合类型

[英]how to make Scala canBuildFrom to build collection type from Seq to Set

I did experiment on CanBuildFrom trait on Scala, it looks fine when I try to convert Array type to Seq type automatically, I think the reason is that we have CanBuildFrom[Array, T, Seq[T]] in the scope. 我在Scala上尝试了CanBuildFrom trait,当我尝试自动将Array类型转换为Seq类型时看起来很好,我认为原因是我们在范围内有CanBuildFrom [Array,T,Seq [T]]。 However, if I try to convert Array to Set, it is not working. 但是,如果我尝试将Array转换为Set,则无法正常工作。 Further, Converting Seq to Set is not working as well. 此外,将Seq转换为Set也不起作用。 I just wonder should I define implicit into same type CanBuildFrom companion object to implement conversion ?. 我只是想知道我是否应该将相同类型的CanBuildFrom隐式定义为同伴对象来实现转换? If it is, why scala doesnt provide it by default, is reason Set is a function ? 如果是,为什么scala默认不提供它,原因是Set是一个函数?

Here is the code which for Array to Seq 这是Array到Seq的代码

def transform[U[_]](col: Array[String])(implicit cbf: CanBuildFrom[Array[String], String, U[String]]): U[String] = {
    val builder = cbf()

    for (ele <- col) builder += ele

    builder.result()
  }

CanBuildFromSpec.transform[Seq](Array("123", "3"))

If I want to convert to Array to Set or List, it is not working 如果我想转换为Array to Set或List,则无效

CanBuildFromSpec.transform[List](Array("123", "3")) //compilation error, cannot construct
CanBuildFromSpec.transform[Set](Array("123", "3")) //compilation error, cannot construct

There is no need to reinvent a wheel - Scala collections have to[C[_]] method which allows conversion as you want it: 没有必要重新发明轮子 - Scala集合必须to[C[_]]方法允许你想要的转换:

scala> List(1, 2, 3).to[Vector]
res0: Vector[Int] = Vector(1, 2, 3)

scala> Array(1, 2, 3).to[Seq]
res1: Seq[Int] = Vector(1, 2, 3)

scala> Seq(1, 2, 3).to[Set]
res2: Set[Int] = Set(1, 2, 3)

BTW, CanBuildFrom was introduced not for easy convertibility. 顺便说一句, CanBuildFrom引入并非易于转换。 It was needed in order for operations like map() or filter() to preserve original collection types. 为了使map()filter()操作保留原始集合类型,需要它。

Also Array is converted to Seq automatically not because of CanBuildFrom (that would imply that array contents are copied into the new sequence, which can be pretty inefficient), but because there is an implicit conversion from arrays to their wrappers providing Seq interface. 此外, Array不会因为CanBuildFrom自动转换为Seq (这意味着数组内容被复制到新序列中,这可能效率很低),但是因为存在从数组到其包装器的隐式转换,提供Seq接口。

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

相关问题 Scala集合CanBuildFrom - Scala Collection CanBuildFrom CanBuildFrom如何知道某个类型是否可以从另一个类型构建? - How does CanBuildFrom know whether a type can build from another? 为什么我们需要Scala的CanBuildFrom中的From type参数 - Why do we need the From type parameter in Scala's CanBuildFrom Scala的另一个CanBuildFrom问题:集合丰富运算符,它包装了另一个不同类型的 - Another Scala CanBuildFrom issue: a collection enrichment operator that wraps another of a different type Scala Seq [Seq [SomeClass]]类型不匹配,用于可变集合 - Scala Seq[Seq[SomeClass]] type mismatch for mutable collection Scala宏如何推断隐式CanBuildFrom - scala macro how to infer implicit CanBuildFrom 如何将Java Collection / List转换为Scala seq? - How to convert a Java Collection/List to a Scala seq? 如何弄平字符串的序列并在Scala中重新构建? - How to flatten a seq to a string and build it back in Scala? 如何修复 scala 中的不匹配错误,其中发现:Seq[scala.collection.immutable.Seq required: scala.collection.Seq? - How can I fix mismatch error in scala where the found : Seq[scala.collection.immutable.Seq required: scala.collection.Seq? 如何从 Java 中的 Java 列表创建 scala.collection.immutable.Seq? - How to create a scala.collection.immutable.Seq from a Java List in Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM