如果在Scala IDE中尝试以下操作:
val chars = Array[Char](256)
Run Code Online (Sandbox Code Playgroud)
一切都很好.但如果我这样做:
val len = 256
val chars = Array[Char](len)
Run Code Online (Sandbox Code Playgroud)
它说它期望一个Char而不是len?为什么?我希望行为是一样的!为什么我认为我想把那个东西放在数组中而不是指定它的大小?据我所知,没有数组的构造函数只需要一个参数就可以将它放在数组中.
这个问题可能看起来很愚蠢,但我找不到任何有关如何在Play 2.0中回滚演变的说明.谷歌只发现一个页面,说明进化文件的"唐氏"部分用于那个,而这就是全部.任何指针或说明将不胜感激.
我有以下类层次结构:
class A
class B extends A
class C extends A
Run Code Online (Sandbox Code Playgroud)
然后,还有另一个类,它接受这些类的实例,并且有一个方法,其中两种模式匹配的情况是这样的:
class D (one: A, two: A) {
def work {
(one, two) match {
case (o, t): (B, B) => ... blablabla
case (o, t): (B, C) => ... blablabla
case _ =>
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是,当它应该解决匹配而支持第二种情况时(B, C),它会尝试将其解析为(B, B)并提出类强制转换异常C cannot be cast to B.为什么?该怎么办?我怎么能绕过这个?
最近我一直在研究像Neo4j这样的图形数据库,以及Prolog和miniKanren中的逻辑编程.根据我迄今所学到的知识,它们都允许指定它们之间的事实和关系,并且还可以查询生成的系统以进行某些选择.所以,实际上我看不出它们之间的差别很大,因为它们都可以用来构建图形并查询它,但是使用不同的语法.但是,它们是完全不同的软件.
除了数据库可能提出更多时空有效存储技术的技术性,除了像miniKanren这样的微小逻辑核心更简单和可嵌入之外,图形数据库和逻辑编程语言之间的实际区别是什么,如果它们都只是一个图形数据库+查询API?
我必须实现一种数组或序列或列表,它支持最便宜的循环转发和元素回绕方式.看这个例子:
Original sequence: 1 2 3 4 5
Forwarded once: 5 1 2 3 4
Forwarded twice: 4 5 1 2 3
Run Code Online (Sandbox Code Playgroud)
相同但相反的是后绕组.什么是最便宜和最Scala风格的实现方式?在Java中,我可以使用LinkedList,它会很棒...但是,我找不到Scala的任何明确答案.
此外,它还必须易于通过索引替换任何给定元素,如LinkedList中.
更新:
对于最快但不那么惯用的算法变体(你知道什么时候需要它),请参考PetrPudlák的答案!
练习这里写的内容:ScalaForms,我创建了以下形式:
val personCreationForm = Form(
tuple (
"name" -> nonEmptyText,
"age" -> number verifying (min(0), max(100)) /*ERROR*/
) verifying ("Wrong entry", result => result match {
case (name, age) => true
})
)
Run Code Online (Sandbox Code Playgroud)
但是,关于verifying状态的错误value verifying is not a member of (java.lang.String, play.api.data.Mapping[Int]).
如在引用的示例中那样使用mapping而不是tuple没有区别.这有什么不对?
在Clojure中,我希望有一个协议,其中一些方法具有默认实现,而一些方法具有自定义实现.第一个用于配置后者.这是一个例子:
(defprotocol Saving
(save [this] "saves to mongodb")
(collection-name [this] "must return a string representing the associated MongoDB collection"))
;Default implementation
(extend-type Object
Saving
; the `save` method is common for all, so it is actually implemened here
(save [this] (mc/insert (collection-name [this]) this))
; this method is custom to every other type
(collection-name [this] "no_collection"))
;Particular implementations
(defrecord User
[login password]
Saving
(collection-name [this] "users"))
(defrecord NewsItem
[text date]
Saving
(collection-name [this] "news_items"))
Run Code Online (Sandbox Code Playgroud)
但是,它不会以这种方式工作.即使调用collection-name一个User或NewsItem …
阅读示例书中的Scala,当Martin解释第54页的类型界限时,有一个例子:
trait Set[A <: Ordered[A]] {
def incl(x: A): Set[A]
def contains(x: A): Boolean
}
Run Code Online (Sandbox Code Playgroud)
和
trait Set[A <% Ordered[A]] ...
Run Code Online (Sandbox Code Playgroud)
他还说,为了证明类型边界的可能性,<:/ <%是特征集所需的唯一变化.
但是,当我用自己的代码重复示例时,IDE会抱怨特征可能没有视图边界,只有类型边界.将trait关键字更改为抽象类或更改绑定到类型绑定的视图会有所帮助.这是书中的错误吗?
关于使用相同对象的方法访问对象的私有内部类,我有点困惑.这是我在Scala编程中的练习代码(第245-246页):
import Element.elem
abstract class Element {
def contents: Array[String]
def height = contents.length
def width = if(height == 0) 0 else contents(0).length
def above(that: Element): Element = elem(this.contents ++ that.contents)
def beside(that: Element): Element = {
elem( for(
(line1, line2) <- this.contents zip that.contents)
yield line1 + line2 )
}
override def toString = contents mkString "\n"
}
object Element {
private class ArrayElement (
val contents: Array[String]
) extends Element
private class LineElement (s: String) extends ArrayElement(Array(s)) { …Run Code Online (Sandbox Code Playgroud) 我在Play 2.0模板中有以下代码:
@content.toString.lines.map{
case line => // i put `case` here as another attempt to make it work
line match {
case "" => @Html("")
case _ => <li>@Html(line)</li> /*CRASH*/
}
}
Run Code Online (Sandbox Code Playgroud)
它在标记的行上失败了,这样说not found: value line.它的第二个变种:
@for(line <- content.toString.lines){
@line match { /*CRASH*/
case "" => @Html("")
case _ => <li>@Html(line)</li>
}
}
Run Code Online (Sandbox Code Playgroud)
在标记的行上失败,声称'case' expected but identifier found.
更新:
同样的事情val:
@val headID = "head"
Run Code Online (Sandbox Code Playgroud)
想出来了illegal start of simple expression.
更新结束
我想知道,我做错了什么以及如何在Play的模板中正确实现match-case …