scala(5)Learn CH7 Programming in Scala from others
CH7 Build-in control structure
If statement
package com.sillycat.easyscala.lesson5
object TestIf {
def main(args: Array[String]): Unit = {
var filename = "default.txt"
println(args.isEmpty)
if (!args.isEmpty){
filename = args(0)
}
println("filename: " + filename)
//if statement will return the last sentence
val filename2 =
if (args.isEmpty) "default.txt" else args(0)
println ("filename2: " + filename2)
}
}
If statement has a return value.
For Statement
package com.sillycat.easyscala.lesson5
object TestFor {
def main(args: Array[String]): Unit = {
val files = (new java.io.File(".")).listFiles()
println("==========file names1 :")
for (file <- files) {
print(file + " ")
}
println();
println("==================================");
println("==========file names2 :")
for (f <- files) print(f + " ")
println();
println("====================================");
// 1 to 5 will return Range(1, 6)
// 1, 2, 3, 4, 5
println("==========numbers 1 :")
for (i <- 1 to 5) print(i + " ")
println();
println("=======================================");
// 0 until 5 will return Range(0, 5)
// 0, 1, 2, 3, 4
println("===========numbers 2: ");
for (i <- 0 until 5) print(i + " ")
println();
println("============================");
// multiple checkpoint
for (
file <- files if (file.getName.endsWith("bin"))
) println(file)
// for
for (
file <- files
if (file.isFile)
if (file.getName.endsWith("gitignore"))
) println(file)
//put all the matched files in array
val sfiles = for (
file <- files if (file.isFile)
) yield file
// files in array
for (i <- 0 to sfiles.length - 1){
print(sfiles(i))
}
}
}
Try Catch System almost the same to Java
package com.sillycat.easyscala.lesson5
import java.io.FileReader
object TestTry {
def main(args: Array[String]): Unit = {
var f = new FileReader("test.txt")
try {
// use the file
} catch {
case ex: java.io.FileNotFoundException =>
println(ex)
case ex: java.io.IOException =>
println(ex)
} finally {
f.close()
}
}
}
Match is much better than switch, not matter int, even string is working.
package com.sillycat.easyscala.lesson5
object TestMatch {
def main(args: Array[String]): Unit = {
val firstArg = if (!args.isEmpty) args(0) else ""
var n = 10000
var friend = "adsfadfasf"
firstArg match {
case "salt" =>
friend = "pepper"
n = 100
case "chips" =>
friend = "salsa"
n = 10
case "eggs" =>
friend = "bacon"
n = 1
//this is the default value
case _ =>
friend = "huh?"
n = 0
}
println(friend + n.toString());
}
}
references:
http://sillycat.iteye.com/blog/1536392
http://snowriver.org/blog/tag/scala/
http://snowriver.org/blog/2011/03/20/programming-in-scala-%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0ch7-built-in-control-structure/
分享到:
相关推荐
《Programming In Scala》是一本权威的Scala编程语言教程,它由Martin Odersky(Scala的创造者)、Lex Spoon 和 Bill Venners 共同编写。中文版包含了1到13章的内容,这些章节涵盖了Scala的基础知识和基本应用,适合...
总之,《Programming in Scala》是一本非常有价值的书籍,无论你是初学者还是有经验的程序员,都能从中获得宝贵的知识和技能。通过本书的学习,你可以更好地理解和运用Scala这一强大而优雅的编程语言。
WHAT'S INSIDE:, Functional programming from square one in clear, readable language, No prior Scala experience needed, Learn both the hows and whys of FP, No prior experience with FP or Scala is ...
《Programming in Scala.pdf》是一本专注于Scala编程语言的书籍。Scala是一种现代的多范式编程语言,专为可扩展性而设计。该书旨在为读者提供深入理解Scala语言的教程。 ### 描述 描述中提及的内容表明,...
英文原版 scala函数式编程 function programming in scala
#### Scala 编程语言简介 Scala是一种多范式编程语言,它融合了面向对象编程和函数式编程的特性。该语言旨在提高代码的可读性和表达能力,同时支持大规模应用程序的开发。Scala语言由Martin Odersky创建,并由他领导...
《Programming in Scala》是Scala编程语言的一本权威指南,由Martin Odersky、Lex Spoon 和 Bill Venners 合著,被广泛认为是学习Scala的首选教材。这本书深入介绍了Scala语言,涵盖了从基础语法到高级特性的全方位...
《Programming in Scala》第三版是一本深入探讨Scala编程语言的权威指南,由Martin Odersky、Lex Spoon 和 Bill Venners合著。这本书以高清英文原版的形式呈现,旨在帮助读者全面理解Scala的精髓,无论你是初学者...
《Programming in Scala》第二版是一本全面详尽的Scala编程指南,由Scala语言的主要设计者Martin Odersky、Lex Spoon和Bill Venners共同撰写。本书是为Scala 2.8版本更新的,因此包含了最新的特性和技术改进。 ####...
注意是Programming Scala的第二版,而不是Programming in Scala的第二版,更注重于与Spark相关的知识!强烈推荐!Programming Scala- Scalability = Functional Programming + Objects, 2 edition
Programming in Scala is the definitive book on Scala, the new language for the Java Platform that blends object-oriented and functional programming concepts into a unique and powerful tool for ...
通过阅读《Programming in Scala》第三版并实践提供的源代码,学习者将能够掌握Scala的核心概念,包括其面向对象和函数式编程的融合、强大的类型系统、模式匹配以及并发处理能力。这将为开发者打开新的编程视角,...
《Programming in Scala, Third Edition》是一本关于Scala编程语言的权威指南,由Martin Odersky、Lex Spoon和Bill Venners共同编写。这本书通过详细的解释和实例,为读者提供了一个深入理解Scala语言的机会,包括它...
总之,《Programming in Scala, 3rd edition》被认为是一本全面覆盖Scala语言及其库的书籍,如容器和actor库。这本书不仅适合作为新手学习Scala的教材,也适合那些希望更深入理解Scala内部工作原理的程序员。它能够...
Programming in Scala 3rd 英文pdf
Concurrent and parallel programming have progressed from niche disciplines, of interest only to kernel programming and high-performance computing, to something that every competent programmer must ...
综上所述,《Functional Programming in Scala》是一本全面覆盖函数式编程基础知识到高级技术的权威指南。无论你是初学者还是有一定经验的开发者,都可以从中获得丰富的知识和实践经验。通过本书的学习,你不仅能够...
《Programming in Scala》介绍了一种新的编程语言,它把面向对象和函数式编程概念有机地结合为整体,从而形成一种完整统一、语义丰富的新思维体系。《Scala编程》循序渐进,由浅入深,经作者精心组织、仔细编排,将...