我如何打破一个循环?

var largest=0
for(i<-999 to 1 by -1) {
    for (j<-i to 1 by -1) {
        val product=i*j
        if (largest>product)
            // I want to break out here
        else
           if(product.toString.equals(product.toString.reverse))
              largest=largest max product
    }
}

如何将嵌套for循环转换为尾递归?

来自FOSDEM 2009 Scala Talk http://www.slideshare.net/Odersky/fosdem-2009-1013261 第22页:

中断并继续 Scala没有它们。为什么? 它们有点迫不得已;最好使用许多较小的函数 发布如何与闭包交互。 他们是不需要的!

怎么解释呢?

我使用spark-csv加载数据到一个DataFrame。我想做一个简单的查询并显示内容:

val df = sqlContext.read.format("com.databricks.spark.csv").option("header", "true").load("my.csv")
df.registerTempTable("tasks")
results = sqlContext.sql("select col from tasks");
results.show()

山坳似乎被截断了:

scala> results.show();
+--------------------+
|                 col|
+--------------------+
|2015-11-16 07:15:...|
|2015-11-16 07:15:...|
|2015-11-16 07:15:...|
|2015-11-16 07:15:...|
|2015-11-16 07:15:...|
|2015-11-16 07:15:...|
|2015-11-16 07:15:...|
|2015-11-16 07:15:...|
|2015-11-16 07:15:...|
|2015-11-16 07:15:...|
|2015-11-16 07:15:...|
|2015-11-16 07:15:...|
|2015-11-16 07:15:...|
|2015-11-16 07:15:...|
|2015-11-16 07:15:...|
|2015-11-06 07:15:...|
|2015-11-16 07:15:...|
|2015-11-16 07:21:...|
|2015-11-16 07:21:...|
|2015-11-16 07:21:...|
+--------------------+

如何显示列的全部内容?

你可以在网上找到以下资料:

Higher kinded type == type constructor? class AClass[T]{...} // For example, class List[T] Some say this is a higher kinded type, because it abstracts over types which would be compliant with the definition. Higher kinded types are types which take other types and construct a new type These though are also known as type constructor. (For example, in Programming in Scala). Higher kinded type == type constructor which takes type constructor as a type parameter? In the paper Generics of a Higher Kind, you can read ... types that abstract over types that abstract over types ('higher-kinded types') ..." which suggests that class XClass[M[T]]{...} // or trait YTrait[N[_]]{...} // e.g. trait Functor[F[_]] is a higher kinded type.

因此,考虑到这一点,很难区分类型构造函数,高级类型类型和将类型构造函数作为类型参数的类型构造函数,因此出现了上面的问题。

我如何“加入”一个迭代的字符串由另一个字符串在Scala?

val thestrings = Array("a","b","c")
val joined = ???
println(joined)

我希望这段代码输出a,b,c(通过“,”连接元素)。

Scala不像Java那样有类型安全的枚举。给定一组相关的常量,在Scala中如何最好地表示这些常量?

我正在尝试使用字符串的.format方法。但如果我在字符串中放置%1、%2等,则会抛出Java .util. unknownformatconversionexception,指向一个令人困惑的Java源代码段:

private void checkText(String s) {

    int idx;

    // If there are any '%' in the given string, we got a bad format
    // specifier.
    if ((idx = s.indexOf('%')) != -1) {
        char c = (idx > s.length() - 2 ? '%' : s.charAt(idx + 1));
        throw new UnknownFormatConversionException(String.valueOf(c));
    }
}

由此我明白% char是禁止的。如果是这样,那么我应该使用什么参数占位符?

我使用Scala 2.8。

Scala中的var和val定义有什么区别?为什么这两种定义都需要?为什么你会选择val而不是var,反之亦然?

我理解Ruby和Python的优点。Scala的yield是做什么的?

我在学习Scala游戏框架教程时,遇到了这样一段让我困惑的代码:

def newTask = Action { implicit request =>
taskForm.bindFromRequest.fold(
        errors => BadRequest(views.html.index(Task.all(), errors)),
        label => {
          Task.create(label)
          Redirect(routes.Application.tasks())
        } 
  )
}

所以我决定调查一下,看到了这个帖子。

我还是不明白。

这两者的区别是什么:

implicit def double2Int(d : Double) : Int = d.toInt

and

def double2IntNonImplicit(d : Double) : Int = d.toInt

除了它们有不同的方法名之外。

什么时候应该使用隐式,为什么?

在函数式编程和PLT领域,我已经多次听到“共代数”这个术语,特别是在讨论对象、共数、透镜等的时候。在谷歌上搜索这个术语,就会得到一些关于这些结构的数学描述,这对我来说是很难理解的。谁能解释一下在编程环境中共代数的含义,它们的意义是什么,以及它们与对象和共通符的关系是什么?