在Scala中编程时,我会做越来越多的功能.但是,使用中缀表示法时,很难判断何时需要括号,何时不需要.
例如下面的代码:
def caesar(k:Int)(c:Char) = c match {
case c if c isLower => ('a'+((c-'a'+k)%26)).toChar
case c if c isUpper => ('A'+((c-'A'+k)%26)).toChar
case _ => c
}
def encrypt(file:String,k:Int) = (fromFile(file) mkString) map caesar(k)_
Run Code Online (Sandbox Code Playgroud)
(fromFile(file)mkString)需要括号才能编译.删除后,我收到以下错误:
Caesar.scala:24: error: not found: value map
def encrypt(file:String,k:Int) = fromFile(file) mkString map caesar(k)_
^
one error found
Run Code Online (Sandbox Code Playgroud)
mkString显然返回一个字符串,在其上(通过隐式转换AFAIK)我可以使用map函数.
为什么这个特殊情况需要括号?关于何时以及为何需要它,是否有一般指导原则?
dictionary functional-programming scala infix-notation parentheses
我想从我自己的线程与Akka演员互动.目前,我喜欢这样:
val res = Await.result(aref ? GroupReceive(fromRank), timeout.duration).asInstanceOf[T]
Run Code Online (Sandbox Code Playgroud)
但我不确定这实际上是如何与我的线程交互的?我希望接收是异步的,即我希望在接收时挂起线程以允许完成其他一些工作.我刚刚读到了有关Akka收件箱系统的信息.收件箱akka api
我想我曾经记得Await每次创造一个新演员.await + ask和inbox之间有什么区别,有人能举例说明如何创建收件箱并使用它与"外部"的演员进行交流吗?
编辑 只是为了澄清,我不希望同一个线程继续工作,我希望它停止占用cpu-core并让其他线程工作,直到收到一些东西,然后再次唤醒.
在与类型的上限进行比较时,我在使用Scala中的类型类时遇到了一些麻烦.
请考虑以下代码:
case class NumList[T <: Complex](xs: Complex*) {
def sum = (xs fold new Complex(0, 0))(_ + _)
def map[U <: Complex](f: Complex => U): NumList[U] = NumList(xs.map(f): _*)
override def toString = "[" + xs.mkString(", ") + "]"
}
case class GenList[T](xs: T*) {
def sum(implicit num: Numeric[T]) = xs.sum
def map[U](f: T => U) = GenList(xs.map(f): _*)
override def toString = "[" + xs.mkString(", ") + "]"
}
val r = new Real(2)
val n = new …Run Code Online (Sandbox Code Playgroud) 在C++中我想初始化一个双矩阵(二维双数组),就像我通常没有像这样的指针:
double data[4][4] = {
1,0,0,0,
0,1,0,0,
0,0,1,0,
0,0,0,1
};
Run Code Online (Sandbox Code Playgroud)
但是,由于我想返回并将其传递给函数,我需要它作为double**指针.所以,基本上我需要以一种很好的方式初始化数据(如上所述),但之后我需要将指针保存到2D数组,而不会在函数退出时丢失数据.
对此有何帮助?:-)
我正在为语法创建一个库,它有两种不同的解释:1)根据语法解析字符串2)用语法定义的语言生成字符串.
该库使用cat来创建语法的AST作为免费monad.然而,似乎它可能不是完美的契合,因为免费monad创建了我的AST的列表式表示,这对于语句列表很好,但是语法远离语句列表并且更接近任意树结构.
我设法通过使用~运算符来实现我的树,表示连接的2个语法.然后,AST是一个语法列表,它们本身就是任意的AST.
我的问题是:在免费monad中递归AST的子树有什么好方法?
我目前的实现在这里:
def parserInterpreter: GrammarA ~> ParserInterpreterState =
new (GrammarA ~> ParserInterpreterState) {
def apply[A](fa: GrammarA[A]): ParserInterpreterState[A] =
fa match {
case Regx(regexp) => parseRegex(regexp)
case Optional(b) => parseOptional(b.foldMap(this))
case m @ Multi(g) =>
def x: State[String, A] =
State.apply(state => {
g.foldMap(this)
.run(state)
.map {
case (s, ParseSuccess(_)) => x.run(s).value
case r @ (s, ParseFailure()) => (s, ParseSuccess(s).asInstanceOf[A])
}
.value
})
x
case Choice(a, b) =>
State.apply(state => {
val runA = a.foldMap(this).run(state).value …Run Code Online (Sandbox Code Playgroud) 我试图用scala解析这个文件:
<?xml version="1.0"?>
<model>
<joint name="pelvis">
<joint name="lleg">
<joint name="lfoot"/>
</joint>
<joint name="rleg">
<joint name="rfoot"/>
</joint>
</joint>
</model>
Run Code Online (Sandbox Code Playgroud)
我想用它为我的2D动画引擎创建一个骨架.应将每个关节制成相应的对象,并将所有儿童加入其中.
所以这部分应该产生类似于这样的结果:
j = new Joint("pelvis")
lleg = new Joint("lleg")
lfoot = new Joint("lfoot")
rleg = new Joint("rleg")
rfoot = new Joint("rfoot")
lleg.addJoint(lfoot)
rleg.addJoint(rfoot)
j.addJoint(lleg)
j.addJoint(rleg)
Run Code Online (Sandbox Code Playgroud)
但是,我无法浏览xml代码.首先,我不确定我是否完全理解语法 xml \\ "joint",这似乎产生了包含所有标签的NodeSeq.
主要问题:
xml \\ "...", Elem.child?,xml \\ "@attribute"生成所有属性的连接..?)编辑注意,我需要对此https://github.com/akka/akka/commit/ce014ece3568938b2036c4ccfd21b92faba69607#L13L6进行相反的更改,以使接受的答案适用于AKKA 2.1,这是在akkas主页上找到的稳定版本!
我已经阅读了我在AKKA上找到的所有教程,但我发现的任何内容都没有"开箱即用".
使用eclipse,我想创建2个程序.
Program1:启动actor"joe"并以某种方式使它在127.0.0.1:some_port上可用
Program2:在127.0.0.1:some_port获取对actor"joe"的引用.向"joe"发送hello消息.
程序1应在收到消息时打印一些内容.我想在使用AKKA 2.1的eclipse中运行这个例子.有人可以列出2个程序(program1和program2)以及一个正在运行的application.conf文件吗?
编辑> 让我告诉你到目前为止我得到了什么:
演员
case class Greeting(who: String) extends Serializable
class GreetingActor extends Actor with ActorLogging {
def receive = {
case Greeting(who) ? println("Hello " + who);log.info("Hello " + who)
}
}
Run Code Online (Sandbox Code Playgroud)
计划1:
package test
import akka.actor.ActorSystem
object Machine1 {
def main(args: Array[String]): Unit = {
val system = ActorSystem("MySystem")
}
}
Run Code Online (Sandbox Code Playgroud)
程序2
package test
import akka.actor.ActorSystem
import akka.actor.Props
import akka.actor.actorRef2Scala
object Machine2 {
def main(args: Array[String]): Unit …Run Code Online (Sandbox Code Playgroud) 我正在使用contrib库中的Prettier Printer实现Idris.
当我|//|在Docs 列表上与操作员折叠时,性能会迅速爆炸,即在我失去耐心之前,以下代码不会终止:
*IdrisFMT\PrettyDocs> :exec toString 0 15 $ fold (|//|) $ map text $ words "this is a long sentence with a lot of words that I can use for testing the performance of the prettier printer implementation. I need a few more words to prove my point, though."
Run Code Online (Sandbox Code Playgroud)
请注意,上面的折叠等于预定义的组合子fillCat.
如果我改为使用预定义的catcombinator(= group . vcat),它会在一秒钟内终止:
*IdrisFMT\PrettyDocs> :exec toString 0 15 $ cat $ map text …Run Code Online (Sandbox Code Playgroud) 这里是我认为scala中斐波那契的正确和有用的定义:
lazy val fibs:Stream[Int] = 0 #:: 1 #:: (fibs,fibs.tail).zipped.map(_+_)
Run Code Online (Sandbox Code Playgroud)
但是,我收到以下错误:
fibs take 10 foreach println
0
1
java.lang.StackOverflowError
at scala.collection.mutable.LazyBuilder.(LazyBuilder.scala:25)
at scala.collection.immutable.Stream$StreamBuilder.(Stream.scala:492)
at scala.collection.immutable.Stream$.newBuilder(Stream.scala:483)
at...
Run Code Online (Sandbox Code Playgroud)
我猜zipped与流无法正常工作?关于如何使这项工作,或为什么这不(不应该?)工作的任何建议?
我正在寻找至少Scala 2.8的不可变优先级队列的实现,但最好是更新.在某处有一个很好的实现吗?
scala ×8
actor ×2
akka ×2
asynchronous ×1
c++ ×1
dictionary ×1
fibonacci ×1
free-monad ×1
idris ×1
immutability ×1
inbox ×1
infinite ×1
list ×1
parentheses ×1
parsing ×1
pointers ×1
pretty-print ×1
scala-2.8 ×1
scala-cats ×1
stream ×1
type-bounds ×1
typeclass ×1
xml ×1