我使用的是Ubuntu 18.04 + Scala 2.11.12(OpenJDK 64位服务器VM,Java 1.8.0_162).
一旦我打开scala shell,我就无法看到我输入的任何内容.它虽然打字了.
下面是我println("Hello, world!")在控制台输入时的情况:
$ scala
Welcome to Scala 2.11.12 (OpenJDK 64-Bit Server VM, Java 1.8.0_162).
Type in expressions for evaluation. Or try :help.
scala> Hello, world!
scala>
Run Code Online (Sandbox Code Playgroud)
关于我们如何通过的任何想法?
我正在使用spark-shell(Spark版本2.1.0,使用Scala版本2.11.8,OpenJDK 64位服务器VM,1.7.0_151).
我导入Column类:
scala> import org.apache.spark.sql.Column
import org.apache.spark.sql.Column
Run Code Online (Sandbox Code Playgroud)
我可以定义一个Column对象:
scala> val myCol: Column = col("blah")
myCol: org.apache.spark.sql.Column = blah
Run Code Online (Sandbox Code Playgroud)
并Column在函数定义中使用:
scala> def myFunc(c: Column) = ()
myFunc: (c: org.apache.spark.sql.Column)Unit
Run Code Online (Sandbox Code Playgroud)
到现在为止还挺好.但是在定义类时,Column找不到:
scala> case class myClass(c: Column)
<console>:11: error: not found: type Column
case class myClass(c: Column)
Run Code Online (Sandbox Code Playgroud)
然而,单线程工作:
scala> case class myClass(c: org.apache.spark.sql.Column)
defined class myClass
Run Code Online (Sandbox Code Playgroud)
要么
scala> import org.apache.spark.sql.Column; case class myClass(c: Column)
import org.apache.spark.sql.Column
defined class myClass
Run Code Online (Sandbox Code Playgroud) 当我尝试在终端中使用scala解释器时,我遇到了一个奇怪的问题,当我尝试写入时,光标不会移动,尽管按下后输入"工作".
这是我应该看到的:
Welcome to Scala 2.11.12 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_171).
Type in expressions for evaluation. Or try :help.
scala> val x = 1
x: Int = 1
Run Code Online (Sandbox Code Playgroud)
这是我实际看到的:
Welcome to Scala 2.11.12 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_171).
Type in expressions for evaluation. Or try :help.
scala> x: Int = 1
Run Code Online (Sandbox Code Playgroud)
总而言之,我看不出我写的东西.
这只发生在这个解释器上,终端的每个其他程序/功能似乎都运行得很好.
它发生在我升级到18.04并将JVM替换为8之后.
为什么以下代码的最后一行在scala REPL中引发错误?
import scala.concurrent._
import scala.concurrent.duration._
import scala.concurrent.ExecutionContext.Implicits.global
// This is okay
val futureInt = Future[Int] { 42 }
val v1 = Await.result(futureInt, 1.second)
// This throw error: java.lang.NoClassDefFoundError: Could not initialize class $line8.$read$$iw$$iw$$iw$$iw$$iw$$iw$
val v2 = Await.result(Future[Int]{ 42 }, 1.second)
Run Code Online (Sandbox Code Playgroud)
但是,当我创建一个主类并执行相同的代码时,它可以正常工作:
import scala.concurrent._
import scala.concurrent.duration._
import scala.concurrent.ExecutionContext.Implicits.global
object Main {
def main(args: Array[String]): Unit = {
val futureInt = Future[Int] { 42 }
val v1 = Await.result(futureInt, 1.second)
val v2 = Await.result(Future[Int] { 44 }, 1.second)
println(s"v1=$v1, v2=$v2 ") …Run Code Online (Sandbox Code Playgroud) 我在学习Paul Chiusano和Runar Bjanarson的著作“ Scala中的函数编程”(第7章-纯函数并行性)时遇到了以下情况。
package fpinscala.parallelism
import java.util.concurrent._
import language.implicitConversions
object Par {
type Par[A] = ExecutorService => Future[A]
def run[A](s: ExecutorService)(a: Par[A]): Future[A] = a(s)
def unit[A](a: A): Par[A] = (es: ExecutorService) => UnitFuture(a) // `unit` is represented as a function that returns a `UnitFuture`, which is a simple implementation of `Future` that just wraps a constant value. It doesn't use the `ExecutorService` at all. It's always done and can't be cancelled. Its `get` method simply returns the value …Run Code Online (Sandbox Code Playgroud) java parallel-processing scala java.util.concurrent scala-repl
为什么值“ $”的名称时字符串插值不起作用?
在以下代码中,为什么不打印$的值?使用字符串插值打印x的值时有什么错误?
repl> val x="test value"
repl> val $="some value"
repl> println($)
some value
repl> println(s"value:$x")
value:test value
repl> println(s"value:$$")
value:$
Run Code Online (Sandbox Code Playgroud)
为什么不用$的值代替$?
我注意到在 REPL 中声明类型别名时,换行符会导致语句成功:
这有效:
scala> type a
| 3 == 3
defined type alias a
res32: Boolean = true
Run Code Online (Sandbox Code Playgroud)
这不会:
scala> type a 3 == 3 ^
error: `=`, `>:`, or `<:` expected
Run Code Online (Sandbox Code Playgroud) nvm(节点版本管理器)可以快速选择要使用的替代节点版本。Scala 是否有类似的方式在当前 shell 中快速切换不同版本?例如,假设我想用 2.12.10 启动 REPL,然后执行类似
scala use 2.12.10
Run Code Online (Sandbox Code Playgroud)
会打招呼
Welcome to Scala 2.12.10 (OpenJDK 64-Bit Server VM, Java 1.8.0_202).
Type in expressions for evaluation. Or try :help.
Run Code Online (Sandbox Code Playgroud)
请注意,问题不是关于 SBT via scalaVersion,而是scala直接从命令行使用命令。
我想在REPL中创建一个Either使用实例asRight:
import cats._
import cats.data._
import cats.implicits._
scala> val x = "xxx".asRight
<console>:20: error: value asRight is not a member of String
val x = "xxx".asRight
^
scala> import cats.syntax.either._
import cats.syntax.either._
scala> val x = "xxx".asRight
<console>:23: error: value asRight is not a member of String
val x = "xxx".asRight
^
Run Code Online (Sandbox Code Playgroud)
上面的代码有什么问题?可以asRight在REPL中使用吗?
我想知道是否有任何方法可以避免 Scala REPL 通过设置环境变量或其他方式来截断输出?
例子
scala> typeOf[Iterator[_]].members.mkString("\n")
res6: String =
override def toString(): String
def toStream: scala.collection.immutable.Stream[A]
def toIterator: Iterator[A]
def toTraversable: Traversable[A]
def sameElements: <?>
def copyToArray[B >: A](xs: Array[B],start: Int,len: Int): Unit
def patch: <?>
def duplicate: <?>
def length: <?>
def sliding$default$2: <?>
def sliding: <?>
def grouped: <?>
class GroupedIterator extends
def buffered: <?>
def indexOf: <?>
def indexOf: <?>
def indexWhere: <?>
def indexWhere: <?>
def find(p: A => Boolean): Option[A]
def contains: <?>
def …Run Code Online (Sandbox Code Playgroud) 我在 Scala REPL 上创建了这个函数
scala> val multiDouble = (input :Double) =>
| {
| val toMulti = 2;
| toMulti * input
| }: Double
Run Code Online (Sandbox Code Playgroud)
输出是val multiDouble: Double => Double = Lambda$1351/1709493124@7b44bfb8
根据我的研究,应该是val multiDouble: Double => Double = <function01>
我不明白为什么 Scala REPL 打印Lambda$1351/1709493124@7b44bfb8而不打印<function01>.
scala ×11
scala-repl ×11
apache-spark ×1
either ×1
java ×1
scala-cats ×1
shell ×1
truncate ×1
types ×1
ubuntu ×1
ubuntu-18.04 ×1