注意:这是一个常见问题解答,具体问我所以我可以自己回答,因为这个问题似乎经常出现,我想把它放在一个可以(希望)通过搜索很容易找到的地方
根据我在这里的回答评论提示
例如:
"abcde" map {_.toUpperCase} //returns a String
"abcde" map {_.toInt} // returns an IndexedSeq[Int]
BitSet(1,2,3,4) map {2*} // returns a BitSet
BitSet(1,2,3,4) map {_.toString} // returns a Set[String]
Run Code Online (Sandbox Code Playgroud)
查看scaladoc,所有这些都使用了map继承自的操作TraversableLike,那么为什么它始终能够返回最具体的有效集合呢?甚至String,它map通过隐式转换提供.
继我提出的另一个问题,Scala 2.8突破之后,我想了解更多关于Scala方法的信息,TraversableLike[A].map其签名如下:
def map[B, That](f: A => B)(implicit bf: CanBuildFrom[Repr, B, That]): That
Run Code Online (Sandbox Code Playgroud)
请注意有关此方法的一些事项:
A,将遍历中的每个转换为a B.That并采用类型的隐式参数CanBuildFrom[Repr, B, That].我可以这样称呼如下:
> val s: Set[Int] = List("Paris", "London").map(_.length)
s: Set[Int] Set(5,6)
Run Code Online (Sandbox Code Playgroud)
什么我不能很好地领会是怎么说的事实That是必然要B(即,它是B的一些集合)是由编译器执行.类型参数看起来独立于上面的签名和特征CanBuildFrom本身的签名:
trait CanBuildFrom[-From, -Elem, +To]
Run Code Online (Sandbox Code Playgroud)
Scala编译器如何确保That不会强制进入没有意义的东西?
> val s: Set[String] = List("Paris", "London").map(_.length) //will not compile
Run Code Online (Sandbox Code Playgroud)
编译器如何确定CanBuildFrom调用范围内的隐式对象是什么?
大家好 请原谅我在Scala上提出一个愚蠢的问题.虽然我已经在Scala中编程了大约2年,但我仍然觉得很难理解其implicit用法.我们来举一个讨论的例子:
Array(1,2,3,4).map(x => x)
Run Code Online (Sandbox Code Playgroud)
如果你查看scaladoc,你就无法map在Array课堂上找到这个方法.map可以应用的原因是在Array(1,2,3,4)中implicit def intArrayOps (xs: Array[Int]): ArrayOps[Int]定义了隐式函数scala.Predef.
但是,有两个参数列表,第二个参数列表写为implicit bf: CanBuildFrom[Array[T], B, That]).现在我不知道该编译器发现一个适当的参数类型CanBuildFrom申请时map上Array(1,2,3,4).
(我正在使用Scala nightlies,并在2.8.0b1 RC4中看到相同的行为.我是Scala的新手.)
我有两个SortedMap我想组成的联盟.这是我想要使用的代码:
import scala.collection._
object ViewBoundExample {
class X
def combine[Y](a: SortedMap[X, Y], b: SortedMap[X, Y]): SortedMap[X, Y] = {
a ++ b
}
implicit def orderedX(x: X): Ordered[X] = new Ordered[X] { def compare(that: X) = 0 }
}
Run Code Online (Sandbox Code Playgroud)
这里的想法是'隐含'语句意味着Xs可以转换为Ordered[X]s,然后将SortedMaps 组合成另一个SortedMap,而不仅仅是一个映射.
当我编译时,我得到了
sieversii:scala-2.8.0.Beta1-RC4 scott$ bin/scalac -versionScala compiler version
2.8.0.Beta1-RC4 -- Copyright 2002-2010, LAMP/EPFL
sieversii:scala-2.8.0.Beta1-RC4 scott$ bin/scalac ViewBoundExample.scala
ViewBoundExample.scala:8: error: type arguments [ViewBoundExample.X] do not
conform to method …Run Code Online (Sandbox Code Playgroud) TraversableOnce:"集合的模板特征,可以遍历一次或一次或多次."
我不明白这句话.为什么可以遍历更多次?不仅一次吗?谢谢!
Coming from a Java background I am learning Scala and the following has me very confused. Why is the type returned different in these two (very similar but different) constructs, which vary only in how the source collection was build -
val seq1: IndexedSeq[Int] = for (i <- 1 to 10) yield i
Run Code Online (Sandbox Code Playgroud)
vs.
val seq2: Array[Int] = for (i <- Array(1, 2, 3)) yield i
Run Code Online (Sandbox Code Playgroud)
请务必向我指出正确的文献,以便我能够理解此处起作用的核心基础知识。
如何以更通用的方式引用ArrayBuffer和Vector?
例如 - 我的一个函数将Vector作为参数,而另一个函数返回一个ArrayBuffer.我可以使用什么常见的"iterface"?
例如,在Java中,我可以使用List或Collection接口来传递它们.
我已经意识到我传递Scala集合的典型方式可以使用一些改进.
def doSomethingCool(theFoos: List[Foo]) = { /* insert cool stuff here */ }
// if I happen to have a List
doSomethingCool(theFoos)
// but elsewhere I may have a Vector, Set, Option, ...
doSomethingCool(theFoos.toList)
Run Code Online (Sandbox Code Playgroud)
我倾向于编写我的库函数来List作为参数类型,但我确信有一些更通用的东西我可以放在那里以避免.toList我在应用程序代码中偶尔的调用.这特别令人讨厌,因为我的doSomethingCool函数通常只需要调用map,flatMap并且filter在所有集合类型上定义.
对于那种"更普遍的东西",我有哪些选择?