Skip to content

Instantly share code, notes, and snippets.

@1206yaya
Created January 21, 2015 04:20
Show Gist options
  • Save 1206yaya/39ae82dd804ccd270a4e to your computer and use it in GitHub Desktop.
Save 1206yaya/39ae82dd804ccd270a4e to your computer and use it in GitHub Desktop.
組み込み制御構造 while for if 例外処理 match
package PSSE.code
object BuitinControlStructures extends App {
def gcdLoop(x: Long, y: Long): Long = {
var a = x
var b = y
while (a != 0) {
val temp = a
a = b % a
b = temp
}
b
}
println(gcdLoop(66, 42))
def gcd(x: Long, y: Long): Long =
if (y == 0) x else gcd(y, x % y)
println(gcd(66, 42))
// var line = ""
// do {
// line = readLine()
// println("Read: "+ line)
// } while (line != "")
val filesHere = (new java.io.File(".")).listFiles
for (file <- filesHere)
println(file)
for (i <- 1 to 4)
println("Iteration " + i)
val filestmp = (new java.io.File("/tmp/scalaLesson/")).listFiles
for (file <- filestmp if file.getName.endsWith(".scala"))
println(file)
// より防御的なコード
for (
file <- filestmp
if file.isFile
if file.getName.endsWith(".scala")
) println(file)
def fileLines(file: java.io.File) =
scala.io.Source.fromFile(file).getLines().toList
// def grep(pattern: String) =
// for (
// file <- filestmp
// if file.getName.endsWith(".scala");
// line <- fileLines(file)
// if line.trim.matches(pattern)
// ) println(file +": "+ line.trim)
//
// grep(".*gcd.*")
println("ループを二回まわす")
def grep(pattern: String) =
for {
file <- filestmp
if file.getName.endsWith(".scala")
line <- fileLines(file)
trimmed = line.trim
if trimmed.matches(pattern)
} println(file + ": " + trimmed)
grep(".*gcd.*")
// コレクションの生成
def scalaFiles =
for {
file <- filestmp
if file.getName.endsWith(".scala")
} yield println(file)
scalaFiles
// for式でArray[File]からArray[Int]を生成する
val forLineLengths =
for {
file <- filestmp
if file.getName.endsWith(".scala")
line <- fileLines(file)
trimmed = line.trim
if trimmed.matches(".*for.*")
} yield println(trimmed)
// 例外処理
val n = 2
val half =
if (n % 2 == 0)
n / 2
else
throw new RuntimeException("n must be even")
import java.io.FileReader
import java.io.FileNotFoundException
import java.io.IOException
try {
val f = new FileReader("/tmp/scalaLesson/fooa.scala")
// Use and close file
} catch {
case ex: FileNotFoundException => // Handle missing file
case ex: IOException => // Handle other I/O error
}
val firstArg = "salt"
firstArg match {
case "salt" => println("pepper")
case "chips" => println("salsa")
case "eggs" => println("bacon")
case _ => println("huh?")
}
// 再帰を使ったループコード searchFrom(i + 1) がcontinueの代わりとなる
def searchFrom(i: Int): Int =
if (i >= filestmp.length) -1
else if (filestmp(i).getName.startsWith("-")) searchFrom(i + 1)
else if (filestmp(i).getName.endsWith(".scala")) i
else searchFrom(i + 1)
val i = searchFrom(0)
println(i)
// 九九表を作成する
def makeRowSeq(row: Int) =
for (col <- 1 to 10) yield {
val prod = (row * col).toString
val padding = " " * (4 - prod.length)
padding + prod
}
// Returns a row as a string
def makeRow(row: Int) = makeRowSeq(row).mkString
// Returns table as a string with one row per line
def multiTable() = {
val tableSeq = // a sequence of row strings
for (row <- 1 to 10)
yield makeRow(row)
tableSeq.mkString("\n")
}
println(multiTable())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment