这是我阅读的有关scala中的逆变和协方差的文章之一的代码片段.但是,我无法理解scala编译器抛出的错误消息"错误:协变类型A出现在值pet2的类型A中的逆变位置
class Pets[+A](val pet:A) {
def add(pet2: A): String = "done"
}
Run Code Online (Sandbox Code Playgroud)
我对这段代码片段的理解是,Pets是协变的并且接受A的子类型的对象.但是,函数add仅接受A类型的参数.Being covariant意味着Pets可以获取A类及其子类型的参数.那怎么会抛出错误呢.从哪里出现逆变问题.
对上述错误消息的任何解释都将非常有用.谢谢
types functional-programming scala covariance contravariance
我正在 coursera 上学习 martin odersky 的 scala 函数式编程课程。
但是,我无法理解第二个作业 Funsets.scala 的解决方案。
type Set = Int => Boolean
/**
* Indicates whether a set contains a given element.
*/
def contains(s: Set, elem: Int): Boolean = s(elem)
/**
* Returns the union of the two given sets,
* the sets of all elements that are in either `s` or `t`.
*/
def union(s: Set, t: Set): Set = (e: Int) => s(e) || t(e)
Run Code Online (Sandbox Code Playgroud)
问题在上面的函数中 e 是什么?它从何而来 ?我知道 …
val schema = df.schema
val x = df.flatMap(r =>
(0 until schema.length).map { idx =>
((idx, r.get(idx)), 1l)
}
)
Run Code Online (Sandbox Code Playgroud)
这会产生错误
java.lang.ClassNotFoundException: scala.Any
Run Code Online (Sandbox Code Playgroud)
我不知道为什么,有什么帮助吗?
假设我有一个包含1到100万元素的巨大列表.
val initialList = List(1,2,3,.....1 million)
and
val myList = List(1,2,3)
Run Code Online (Sandbox Code Playgroud)
现在,当我在myList上应用诸如foldLeft的操作时,将initialList作为起始值,例如
val output = myList.foldLeft(initialList)(_ :+ _)
// result ==>> List(1,2,3,.....1 million, 1 , 2 , 3)
Run Code Online (Sandbox Code Playgroud)
现在我的问题来到这里,两个列表都是不可变的,生成的中间列表是
List(1,2,3,.....1 million, 1)
List(1,2,3,.....1 million, 1 , 2)
List(1,2,3,.....1 million, 1 , 2 , 3)
Run Code Online (Sandbox Code Playgroud)
通过不变性的概念,每次创建新列表并丢弃旧列表.因此,每次必须复制100万个元素的新列表以创建新列表时,此操作不是scala中的性能杀手.
如果我错了,请纠正我,因为我试图理解不可变列表的内部实现.提前致谢.
我想使用键控流,我想知道哪种方法在高吞吐量方面更好让我说我有以下内容
trait A{
val id: Int
def isFoo: Boolean
}
case class Foo(id: Int) extends A{
override def isFoo = true
}
case class Bar(id: Int) extends A{
override def isFoo = false
}
val as = List[A](Foo, Bar)
val fs: List[Foo] = as.flatMap{
case Foo => Some(Foo)
case _ => None
}
Run Code Online (Sandbox Code Playgroud)
我可以拥有以下流
val src: DataStream[A] = env.fromElements(as:_*)
Run Code Online (Sandbox Code Playgroud)
我有这些选择:
src.filter(_.isFoo).keyBy(_.id).map(...more processing...)src.filter(_.isInstanceOf[Foo]).keyBy(_.id).map(...more processing...)//here we can remove the isFoo method
src.flatMap{
case f:Foo => Some(f)
case _ => None
}.keyBy(_.id).map(...more …我有一个扩展 LazyLogging 特性的类
class TaskProcessor()
extends Processor
with LazyLogging {
def a1() = {
logger.info("Test logging")
}
}
Run Code Online (Sandbox Code Playgroud)
现在,我想测试我的日志记录是否有效。所以我使用specs2 + scalalogging按照这个示例单元测试记录器消息并按如下方式编写了我的测试
"TaskProcessor" should "test logging" in {
val mockLogger = mock[Logger]
val testable = new TaskProcessor {
override val logger: Logger = mockLogger
}
verify(mockLogger).info("Test logging")
}
Run Code Online (Sandbox Code Playgroud)
我收到以下错误
Error:(32, 20) overriding lazy value logger in trait LazyLogging of type com.typesafe.scalalogging.Logger;
value logger must be declared lazy to override a concrete lazy value
override val logger: Logger …Run Code Online (Sandbox Code Playgroud) 串联电路中有N个从1到N的灯泡。数组表示从0到(N-1)的灯泡编号。最初,所有灯泡均已关闭,并从阵列的索引0打开。我们需要计算在串联电路中打开灯泡的实例数量。
例如 :
A = [2,1,3,5,4]应返回3
Explanation :-
Instant Bulb number on All bulbs in series switched on
0 2 false
1 1,2 true
2 1,2,3 true
3 1,2,3,5 false
4 1,2,3,4,5 true
Run Code Online (Sandbox Code Playgroud)
因此,在3种情况下,灯泡已打开。数是3。
我的方法
我的解决方案运行得很好,但是时间复杂度为O(n2)。我的解决方法如下
public int solution(int[] A) {
// write your code in Java SE 8
int counter = 0;
for (int i = 0; i < A.length; i++) {
int[] intermediate = Arrays.copyOfRange(A, 0, i);
Arrays.sort(intermediate);
if(checkIfOrdered(intermediate))
counter++;
}
return counter; …Run Code Online (Sandbox Code Playgroud) 我只有一个快速的语法问题,但找不到答案。我有一个元组,例如(2,3),我想比较那些值。为了解决这个问题,我将其归结为一个关于该问题的特定案例。
我试图这样做:
def isNumberOneBigger(tuple: Tuple): Boolean = tuple match {
case tuple._1 > tuple._2 => true
}
Run Code Online (Sandbox Code Playgroud)
没用 当我使用compareTo或类似的建议时,总是出现错误。由于我的代码更长,更复杂,所以我不能只使用if-else。模式匹配很有意义。有人知道吗?感觉很简单,但我是Scala的新手。
scala ×7
algorithm ×1
apache-spark ×1
arrays ×1
covariance ×1
databricks ×1
immutability ×1
java ×1
logging ×1
mockito ×1
scalatest ×1
tuples ×1
types ×1