Created
November 8, 2013 01:31
-
-
Save f81/7364830 to your computer and use it in GitHub Desktop.
第15章:Scalaの制御構造 ref: http://qiita.com/f81@github/items/b64832468d112b4c9597
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> { | |
| val variable = "a" | |
| val variable = "b" | |
| } | |
<console>:10: error: variable is already defined as value variable | |
val variable = "b" | |
^ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> { | |
| val variable = "a" | |
| | |
| { | |
| println(variable) | |
| } | |
| } | |
a |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> { | |
| val variable = "a" | |
| | |
| { | |
| val variable = "b" | |
| println(variable) | |
| } | |
| } | |
b |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
throw new FileNotFoundException |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> throw new java.io.FileNotFoundException | |
java.io.FileNotFoundException | |
at .<init>(<console>:8) | |
at .<clinit>(<console>) | |
at .<init>(<console>:11) | |
at .<clinit>(<console>) | |
at $print(<console>) | |
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) | |
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) | |
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) | |
at java.lang.reflect.Method.invoke(Method.java:601) | |
at scala.tools.nsc.interpreter.IMain$ReadEvalPrint.call(IMain.scala:704) | |
at scala.tools.nsc.interpreter.IMain$Request$$anonfun$14.apply(IMain.scala:920) | |
at scala.tools.nsc.interpreter.Line$$anonfun$1.apply$mcV$sp(Line.scala:43) | |
at scala.tools.nsc.io.package$$anon$2.run(package.scala:25) | |
at java.lang.Thread.run(Thread.java:722) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> try { | |
| val input = new java.io.FileInputStream("input.txt") | |
| } catch { | |
| case ex: java.io.FileNotFoundException => println("File not found") | |
| case ex: java.io.IOException => println("IOError") | |
| } | |
File not found |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> { | |
| val input = new java.io.FileInputStream("input.txt") | |
| | |
| try { | |
| throw new java.io.IOException // 明示的に例外をスロー | |
| | |
| input.close() // 例外がスローされているので、ここには到達しない | |
| } catch { | |
| case ex: java.io.FileNotFoundException => println("File not found") | |
| case ex: java.io.IOException => println("IOError") | |
| } finally { | |
| println("Finally") | |
| input.close() // 例外が発生しても、しなくてもここに入ってくる | |
| } | |
| } | |
IOError | |
Finally |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> val list = List("Brad Jones", "Steven Gerrard", "Daniel Agger", "Daniel Sturridge") | |
list: List[java.lang.String] = List(Brad Jones, Steven Gerrard, Daniel Agger, Daniel Sturridge) | |
scala> for (name <- list) { println(name) } | |
Brad Jones | |
Steven Gerrard | |
Daniel Agger | |
Daniel Sturridge |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> for (name <- list if name.startsWith("Daniel")) { println(name) } | |
Daniel Agger | |
Daniel Sturridge |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for (name <- list if name.startsWith("Daniel")) { println(name) } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
final Iterator<String> iterator = new FilterIterator(list.iterator(), | |
new Predicate() { | |
@Override | |
public boolean evaluate(Object object) { | |
final String name = (String) object; | |
return StringUtils.startsWith(name, "Daniel"); | |
} | |
}); | |
while (iterator.hasNext()) { | |
System.out.println(iterator.next()); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> val daniels = for { name <- list if name.startsWith("Daniel") } yield name | |
daniels: List[java.lang.String] = List(Daniel Agger, Daniel Sturridge) | |
scala> for (name <- daniels) { println(name) } | |
Daniel Agger | |
Daniel Sturridge |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> var count = 0; | |
count: Int = 0 | |
scala> while (count < 5) { | |
| count += 1 | |
| println(count) | |
| } | |
1 | |
2 | |
3 | |
4 | |
5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> var count = 0; | |
count: Int = 0 | |
scala> do { | |
| count += 1 | |
| println(count) | |
| } while (count < 5) | |
1 | |
2 | |
3 | |
4 | |
5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.File | |
object IfTest1 { | |
def main(args: Array[String]): Unit = { | |
var filename = "config.txt" | |
if (!args.isEmpty) | |
filename = args(0) | |
println("Filename:" + filename) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.File | |
object IfTest1Refactor { | |
def main(args: Array[String]): Unit = { | |
val filename = if (args.isEmpty) "config.txt" else args(0) | |
println("Filename:" + filename) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Arrays; | |
import java.util.Iterator; | |
import java.util.List; | |
import org.apache.commons.collections.Predicate; | |
import org.apache.commons.collections.iterators.FilterIterator; | |
import org.apache.commons.lang3.StringUtils; | |
public class JavaFiltering { | |
public static void main(String[] args) { | |
final List<String> list = Arrays.asList("Brad Jones", "Steven Gerrard", | |
"Daniel Agger", "Daniel Sturridge"); | |
final Iterator<String> iterator = new FilterIterator(list.iterator(), | |
new Predicate() { | |
@Override | |
public boolean evaluate(Object object) { | |
final String name = (String) object; | |
return StringUtils.startsWith(name, "Daniel"); | |
} | |
}); | |
while (iterator.hasNext()) { | |
System.out.println(iterator.next()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment