如果我取消注释指示的行,则下面的代码不会编译.编译器抱怨:"需要稳定的标识符".
val Empty = Stream.empty
val a = Stream.range(0, 5)
a match {
// case Stream.empty => println("nope") <-- does not work
case Empty => println("compiles") <-- works
case _ => println("ok")
}
Run Code Online (Sandbox Code Playgroud)
如果我首先赋值Stream.empty给Empty它,它会起作用,但是如果没有这样的黑客,你就无法在这样的基本值上进行模式匹配.
我错过了什么吗?
所以我正在学习Scala的功能,并且书中说异常会破坏引用透明度,因此Option应该使用它,如下所示:
def pattern(s: String): Option[Pattern] = {
try {
Some(Pattern.compile(s))
} catch {
case e: PatternSyntaxException => None
}
}
Run Code Online (Sandbox Code Playgroud)
这看起来很糟糕; 我的意思是它似乎相当于:
catch(Exception e){
return null;
}
Run Code Online (Sandbox Code Playgroud)
除了我们可以区分"null for error"和"null as genuine value"这一事实.它似乎应该至少返回包含错误信息的内容,如:
catch {
case e: Exception => Fail(e)
}
Run Code Online (Sandbox Code Playgroud)
我错过了什么?
为什么Haskell使用" - >"似乎只能使用"="?
例如,这有什么问题?
take m ys = case (m,ys) of
(0,_) = []
(_,[]) = []
(n,x:xs) = x : take (n-1) xs
Run Code Online (Sandbox Code Playgroud)
要么
(\x = x * x)
Run Code Online (Sandbox Code Playgroud) 这个声明有效,但不是最漂亮的代码.有没有办法让函数不那么难看?我试过(s: String) -> writer.println(s)但这没用.
val writeStuff: (PrintWriter) -> (String) -> Unit = {
val writer = it
val f: (String) -> Unit = {
writer.println(it)
}
f
}
PrintWriter("test").use { writeStuff(it)("TEST") }
Run Code Online (Sandbox Code Playgroud)
编辑:更具体的例子:
val writeStuff: (PrintWriter) -> (String) -> Unit = { writer ->
{ writer.println(it) }
}
val sendStuff: (Any) -> (String) -> Unit = { sender ->
{ sender.equals(it) }
}
@Test fun test1() {
val li = listOf("a", "b", "c")
val process: List<(String) -> Unit> …Run Code Online (Sandbox Code Playgroud) 对不起,可能是一个菜鸟问题.
我明白了:
Prelude> all (\x -> x==1) ([n | n <- [1..20]])
False
Prelude> all (\x -> x == 1) ([n | n <- [1..20]])
<interactive>:17:44:
parse error (possibly incorrect indentation or mismatched brackets)
Prelude>
Run Code Online (Sandbox Code Playgroud)
即x==1工作,但如果我改变x == 1它没有.但那是为什么呢?例如,1 == 1似乎很好地工作.
编译器版本:
$ ghci --version
The Glorious Glasgow Haskell Compilation System, version 7.6.3
Run Code Online (Sandbox Code Playgroud)
添加了证明截图; 很难看到,但你可以看到输入行不包含任何奇怪的东西.

假设我基本上是想Stream.from(0)作为InputDStream.我该怎么做?我能看到的唯一方法是使用StreamingContext#queueStream,但是我必须从另一个线程或子类中排队元素Queue以创建一个行为类似于无限流的队列,这两者都感觉像是一个黑客.
这样做的正确方法是什么?
对不起新手的问题,但我在Scala找不到这个的简写.
val s1 = List(8, 9, 10)
val s2 = List(7, 6, 5)
val s12 = s2.foldLeft(s1)((acc, x) => x :: acc)
assert(s12 === List(5, 6, 7, 8, 9, 10))
Run Code Online (Sandbox Code Playgroud)
我应该使用哪个操作符来达到同样的效果?