小编Ste*_*ill的帖子

如何在Scala中对Python或运算符进行近似设置比较?

在听到最新的Stack Overflow播客后,Peter Norvig的紧凑型Python拼写检查器引起了我的兴趣,所以我决定在Scala中实现它,如果我能用功能Scala成语表达它,还要看看需要多少行代码.

这是整个问题.(我们还不比较代码行.)

(两个注意事项:如果你愿意,你可以在Scala解释器中运行它.如果你需要big.txt或整个项目的副本,它就在GitHub上.)

import scala.io.Source

val alphabet = "abcdefghijklmnopqrstuvwxyz"

def train(text:String) = {
  "[a-z]+".r.findAllIn(text).foldLeft(Map[String, Int]() withDefaultValue 1)
    {(a, b) => a(b) = a(b) + 1}
}

val NWORDS = train(Source.fromFile("big.txt").getLines.mkString.toLowerCase)

def known(words:Set[String]) =
  {Set.empty ++ (for(w <- words if NWORDS contains w) yield w)}

def edits1(word:String) = {
  Set.empty ++
  (for (i <- 0 until word.length) // Deletes
    yield (word take i) + (word drop (i + 1))) ++ 
  (for (i <- 0 until word.length - …
Run Code Online (Sandbox Code Playgroud)

python scala anonymous-function

6
推荐指数
2
解决办法
542
查看次数

标签 统计

anonymous-function ×1

python ×1

scala ×1