如何解决这个编译错误:
trait Container {
def getInts() : Seq[Int]
def getStrings() : Seq[String]
def put[T](t: T)
def get[T] : Seq[T]
}
class MutableContainer extends Container {
val entities = new mutable.HashMap[Class[_], mutable.Set[Any]]() with mutable.MultiMap[Class[_], Any]
override def getStrings(): Seq[String] = entities.get(classOf[String]).map(_.toSeq).getOrElse(Seq.empty).asInstanceOf[Seq[String]] //strings
override def getInts(): Seq[Int] = entities.get(classOf[Int]).map(_.toSeq).getOrElse(Seq.empty).asInstanceOf[Seq[Int]]
override def get[T]: Seq[T] = entities.get(classOf[T]).map(_.toSeq).getOrElse(Seq.empty).asInstanceOf[Seq[T]]
override def put[T](t: T): Unit = entities.addBinding(t.getClass, t)
}
Run Code Online (Sandbox Code Playgroud)
这是错误:
[error] Container.scala:23: class type required but T found
[error] override def get[T]: Seq[T] = entities.get(classOf[T]).map(_.toSeq).getOrElse(Seq.empty).asInstanceOf[Seq[T]]
Run Code Online (Sandbox Code Playgroud) 我想拆分List[Either[A, B]]两个列表.
有没有更好的办法 ?
def lefts[A, B](eithers : List[Either[A, B]]) : List[A] = eithers.collect { case Left(l) => l}
def rights[A, B](eithers : List[Either[A, B]]) : List[B] = eithers.collect { case Right(r) => r}
Run Code Online (Sandbox Code Playgroud) 为什么asInstanceOf不会抛出ClassCastException?
scala> List("a").asInstanceOf[List[Int]]
res34: List[Int] = List(a)
Run Code Online (Sandbox Code Playgroud) 我正在尝试为SBT配置元空间
export SBT_OPTS="-XX:+CMSClassUnloadingEnabled -XX:MaxMetaspaceSize=512M -XX:MetaspaceSize=256M -Xms2G -Xmx2G"
Run Code Online (Sandbox Code Playgroud)
但是当我跑步时sbt -v,我有以下输出:
[process_args] java_version = '1.8.0_11'
# Executing command line:
java
-XX:+CMSClassUnloadingEnabled
-XX:MaxMetaspaceSize=512M
-XX:MetaspaceSize=256M
-Xms2G
-Xmx2G
-Xms1024m
-Xmx1024m
-XX:ReservedCodeCacheSize=128m
-XX:MaxMetaspaceSize=256m
-jar
/usr/local/Cellar/sbt/0.13.7/libexec/sbt-launch.jar
Run Code Online (Sandbox Code Playgroud)
问题似乎是我的自定义值MaxMetaspaceSize被另一个值覆盖,如上面的输出所示.
SBT版本:0.13.7
Java版本:1.8
操作系统:OSX
我想知道如何使用功能编程在Scala中实现广度优先搜索.
这是我的第一个不纯的代码:
def bfs[S](init: S, f: S => Seq[S], finalS: S => Boolean): Option[S] = {
val queue = collection.mutable.Queue[S]()
queue += init
var found: Option[S] = None
while (!queue.isEmpty && found.isEmpty) {
val next = queue.dequeue()
if (finalS(next)) {
found = Some(next)
} else {
f(next).foreach { s => queue += s }
}
}
found
}
Run Code Online (Sandbox Code Playgroud)
虽然我只使用局部可变性(a var和可变Queue),但它不是纯粹的功能.
我想出了另一个版本:
case class State[S](q: Queue[S], cur: S)
def update[S](f: S => Seq[S])(s: State[S]) : State[S] …Run Code Online (Sandbox Code Playgroud) 我使用IntelliJ Idea 14 Ultimate.
我有一个多模块项目project/Build.scala.这是我项目的布局.
- myproject
- module 1
- module 2
- project
- Build.scala
我尝试使用scala插件中的本机SBT.我选择从外部模型导入项目,然后选择SBT.然后我选择项目的根文件夹,然后单击Finish.
但该项目进口不佳.下载了库,但子模块中的所有导入都是红色的(无法解析).
所以我必须使用sbt gen-idea,它工作正常,但我会很高兴摆脱它......
此错误阻止我运行该项目.
这是版本信息:
~ ??? java -version
java version "1.8.0_45"
Java(TM) SE Runtime Environment (build 1.8.0_45-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.45-b02, mixed mode)
~ ??? scala -version
cat: /release: No such file or directory
Scala code runner version 2.12.1 -- Copyright 2002-2016, LAMP/EPFL and Lightbend, Inc.
Run Code Online (Sandbox Code Playgroud)
我使用IntelliJ 2016.3
在外部终端和sbt,它的工作原理!
Error:scalac: Error: org.jetbrains.jps.incremental.scala.remote.ServerException
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at sbt.compiler.RawCompiler.apply(RawCompiler.scala:26)
at sbt.compiler.AnalyzingCompiler$$anonfun$compileSources$1$$anonfun$apply$2.apply(AnalyzingCompiler.scala:146)
at sbt.compiler.AnalyzingCompiler$$anonfun$compileSources$1$$anonfun$apply$2.apply(AnalyzingCompiler.scala:142)
at sbt.IO$.withTemporaryDirectory(IO.scala:291)
at sbt.compiler.AnalyzingCompiler$$anonfun$compileSources$1.apply(AnalyzingCompiler.scala:142)
at sbt.compiler.AnalyzingCompiler$$anonfun$compileSources$1.apply(AnalyzingCompiler.scala:139)
at sbt.IO$.withTemporaryDirectory(IO.scala:291)
at sbt.compiler.AnalyzingCompiler$.compileSources(AnalyzingCompiler.scala:139)
at …Run Code Online (Sandbox Code Playgroud) 以下代码无法编译:
case object O
trait Show[A] {def show(a: A) : String}
class OShow extends Show[O] {
override def show(a: O): String = "ahoy"
}
Run Code Online (Sandbox Code Playgroud)
编译错误是
Error: not found: type O
class OShow extends Show[O] {
Run Code Online (Sandbox Code Playgroud)
那么,如何将case对象用作多态类型?^
我使用标准的IO monad.
在某些时候,我需要短路.在给定条件下,我不想运行以下ios.
这是我的解决方案,但我发现它太冗长而且不优雅:
def shortCircuit[A](io: IO[A], continue: Boolean) =
io.map(a => if (continue) Some(a) else None)
for {
a <- io
b <- shortCircuit(io, a == 1)
c <- shortCircuit(io, b.map(_ == 1).getOrElse(false))
d <- shortCircuit(io, b.map(_ == 1).getOrElse(false))
e <- shortCircuit(io, b.map(_ == 1).getOrElse(false))
} yield …
Run Code Online (Sandbox Code Playgroud)
例如,对于第3行,第4行和第5行,我需要重复相同的条件.
有没有更好的办法 ?
我第一次使用猫来解决代码出现的第一天,我想知道是否有可能改进.
给定update具有以下签名
的方法def update(i: Instruction): PosAndDir => PosAndDir
我想出来:
val state: State[PosAndDir, List[Unit]] = instructions.map(i => State.modify(update(i))).toList.sequenceU
val finalState = state.runS(PosAndDir(Pos(0, 0), North)).value
Run Code Online (Sandbox Code Playgroud)
并且
def update2(i: Instruction): State[PosAndDir, Option[Pos]] =
State.modify(update(i)).inspect(pad => if (i == Walk) Some(pad.pos) else None)
…
val state = instructions.map(update2).toList.sequenceU
val positions = state.runA(PosAndDir(Pos(0, 0), North)).value.flatten
Run Code Online (Sandbox Code Playgroud)
更确切地说,问题是:
.value(使用scalaz,它是透明的)?update2用理解来提高可读性?Applicative实例Seq(我知道scalaz中没有).?