我使用IntelliJ IDEA作为代码编辑器,所以这不是要替换它.我在我们的服务器和标准命令上使用vi或vim进行简单的文本编辑,unix以查找文件,获取目录列表等.
同事们对emacs发誓,但我想知道是否值得我花时间学习它.我的生产力会提高多少?
编辑:有人指出,此问题之前已被提出过
我最近听到一些建议"从Predef中取消隐式转换" - 我认为这意味着它也可能是unimport不需要的类:
import java.awt._
unimport java.awt.List
Run Code Online (Sandbox Code Playgroud)
但这不是"unmport"的语法(即没有这样的unimport关键字).什么是正确的语法?
这个问题并不意味着火焰诱饵!可能很明显,我最近一直在关注Scalaz.我试图理解为什么我需要库提供的一些功能.这是一些东西:
import scalaz._
import Scalaz._
type NEL[A] = NonEmptyList[A]
val NEL = NonEmptyList
Run Code Online (Sandbox Code Playgroud)
我在我的函数中放了一些println语句来查看发生了什么(除了:如果我试图避免这样的副作用,我会怎么做?).我的职责是:
val f: NEL[Int] => String = (l: NEL[Int]) => {println("f: " + l); l.toString |+| "X" }
val g: NEL[String] => BigInt = (l: NEL[String]) => {println("g: " + l); BigInt(l.map(_.length).sum) }
Run Code Online (Sandbox Code Playgroud)
然后我通过cokleisli将它们组合在一起并传入NEL[Int]
val k = cokleisli(f) =>= cokleisli(g)
println("RES: " + k( NEL(1, 2, 3) ))
Run Code Online (Sandbox Code Playgroud)
这打印什么?
f: NonEmptyList(1, 2, 3)
f: NonEmptyList(2, 3)
f: NonEmptyList(3) …Run Code Online (Sandbox Code Playgroud) Option可以隐式转换为Iterable- 但为什么它不只是直接实现Iterable:
def iterator = new Iterator[A] {
var end = !isDefined
def next() = {
val n = if (end) throw new NoSuchElementException() else get
end = true
n
}
def hasNext = !end
}
Run Code Online (Sandbox Code Playgroud)
编辑: 事实上它甚至比那更糟糕,因为在2.8 Option中声明了一个iterator方法:
def iterator: Iterator[A] =
if (isEmpty) Iterator.empty else Iterator.single(this.get)
Run Code Online (Sandbox Code Playgroud) 我使用以下选项运行G1垃圾收集器的Java程序:
-XX:-UseBiasedLocking
-XX:+UnlockExperimentalVMOptions
-XX:+UseG1GC
-verbose:gc -XX:+PrintGCTimeStamps -XX:+PrintGCDetails -Xloggc:/var/tmp/gclog.out
Run Code Online (Sandbox Code Playgroud)
输出看起来像这样......
44900.297: [GC pause (young)44900.386 (initial-mark), 0.08894851 secs]
: [GC concurrent-mark-start]
[Parallel Time: 83.7 ms]
[GC Worker Start Time (ms): 44900297.6 44900297.6 44900297.6 44900297.6 44900297.6 44900297.7 44900297.7 44900297.7 44900297.7 44900297.7 44900297.7 44900297.7 44900297.7
Avg: 44900297.7, Min: 44900297.6, Max: 44900297.7, Diff: 0.1]
[Update RS (ms): 23.5 24.3 25.0 25.0 23.9 24.4 25.2 24.1 25.7 24.7 24.8 24.4 24.7
Avg: 24.6, Min: 23.5, Max: 25.7, Diff: 2.1]
[Processed Buffers : 16 19 19 23 …Run Code Online (Sandbox Code Playgroud) 在Java中我可能会这样做:
class MyClass {
private List<? extends MyInterface> list;
public void setList(List<MyImpl> l) { list = l; }
}
Run Code Online (Sandbox Code Playgroud)
......假设(MyImpl implements MyInterface当然).
使用?时,Scala中的模拟是Buffer什么?
import java.lang.reflect._
import scala.collection.mutable._
class ScalaClass {
val list:Buffer[MyInterface] = null
def setList(l: Buffer[MyImpl]) = {
list = l
}
}
Run Code Online (Sandbox Code Playgroud)
这(当然)不编译 - 但是如何list以这样的方式声明变量呢?
编辑 ; 我要补充一点.显然,这与Java中的泛型在T中从不协变这一事实有关,而在Scala中,它们可以是协变的,也可以不是协变的.例如,Scala类List在T中是协变的(并且必然是不可变的).因此以下将编译:
class ScalaClass {
val list:List[MyInterface] = null
def setList(l: List[MyImpl]) = {
list = …Run Code Online (Sandbox Code Playgroud) 我最近一直在阅读有关函数式语言的文章.在10多年的OO开发过程中,我发现很难理解人们如何能够指出纯粹的功能方法(即使用相同参数调用相同的方法做同样的事情) (在OO程序中)我需要缓存数据.
我们是否承认在程序中可能需要一个不可变的actor(即缓存).我刚看了Joe Armstrong关于infoq的演讲,他在这方面看起来很教条!
我们是否只是承认查找数据可能很昂贵(因为我们永远无法缓存它)?如果是这样,我们如何控制,例如,某些共享资源(例如数据库)上的负载
是否有一些神奇的尘埃,我还不知道,这解决了整个问题,然后喝了一杯好茶.
当然谷歌搜索"Erlang Cache"似乎返回了一些公平的结果......
我知道这样做的反制方法.我想知道是否有一个很好的和紧凑的方式来做到这一点.
我试图了解如何使用scalaz State执行复杂的有状态计算.这是问题所在:
给定一个
List[Int]潜在的除数和一个List[Int]数字,找到一个List[(Int, Int)匹配对(除数,数字),其中允许除数最多匹配一个数.
作为测试:
def findMatches(divs: List[Int], nums: List[Int]): List[(Int, Int)]
Run Code Online (Sandbox Code Playgroud)
并通过以下输入:
findMatches( List(2, 3, 4), List(1, 6, 7, 8, 9) )
Run Code Online (Sandbox Code Playgroud)
我们最多可以获得3场比赛.如果我们规定必须按照遍历列表lr的顺序进行匹配,那么匹配必须是:
List( (2, 6) , (3, 9) , (4, 8) )
Run Code Online (Sandbox Code Playgroud)
所以需要通过以下两个测试:
assert(findMatches(List(2, 3, 4), List(1, 6, 7, 8, 9)) == List((2, 6), (3, 9), (4, 8)))
assert(findMatches(List(2, 3, 4), List(1, 6, 7, 8, 11)) == List((2, 6), (4, 8)))
Run Code Online (Sandbox Code Playgroud)
这是一个迫切的解决方案:
scala> def findMatches(divs: …Run Code Online (Sandbox Code Playgroud) scala类型Nothing表示(据我所知)类型层次结构的底部,也用符号denoted表示.也就是说,Nothing是任何给定类型的子类型.James Iry对我们这些没有理论背景理论背景的人解释了对Nothing类型的要求!
所以我的问题是,如果Nothing是每种类型的子类型,为什么我不能调用任何类型的方法Nothing?显然,我无法实例化Nothing但为什么不进行以下编译?
var n: Nothing = _
def main(args: Array[String]) {
println(n.length) //compile error: value length is not a member of Nothing
}
Run Code Online (Sandbox Code Playgroud)
当然,Nothing这个子类型String应该可以吗?请注意,以下编译就好了!
var n: Nothing = _
def foo(s: String) : Int = s.length
def main(args: Array[String]) {
println(foo(n))
}
Run Code Online (Sandbox Code Playgroud)
同样如下:
def main(args: Array[String]) {
println(n.asInstanceOf[String].length)
}
Run Code Online (Sandbox Code Playgroud)