Created
August 12, 2013 14:46
-
-
Save f81/6211440 to your computer and use it in GitHub Desktop.
第7章:タプルにチャレンジ! ref: http://qiita.com/f81@github/items/a8419532c316d190782d
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
object CaseClassTuple { | |
def get() = { | |
val name = new Name("清美", "椿山") | |
(Human(name, 15), name) | |
} | |
def main(args: Array[String]){ | |
val tuple = get() | |
printf("fullname_1=%s \n", tuple._1.name.fullname) | |
printf("fullname_2=%s \n", tuple._2.fullname) | |
} | |
} | |
class Name(first: String, last: String) { | |
def fullname() = first + " " + last | |
} | |
case class Human(name: Name, age: Int) |
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
$ scalac Tuple.scala | |
$ scala Tuple | |
max=10 | |
index=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
$ scalac Func23.scala | |
Func23.scala:3: error: type Function23 is not a member of package scala | |
val func23 : (Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int) => Int = null; | |
^ | |
one error 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
$ scalac CaseClassTuple.scala | |
$ scala CaseClassTuple | |
fullname_1=清美 椿山 | |
fullname_2=清美 椿山 |
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
$ scalac TupleImmutable.scala | |
TupleImmutable.scala:9: error: reassignment to val | |
tuple._1 = 3 | |
^ | |
one error 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
val max = numbers.max | |
val index = numbers.indexOf(max) |
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
val maxValue = getMaxValue(numbers) | |
printf("max=%s \n", maxValue._1) | |
printf("index=%s \n", maxValue._2) |
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
$ scalac Tuple22.scala | |
$ scala Tuple22 | |
element_1=1 | |
element_22=22 |
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
$ scalac Tuple23.scala | |
Tuple23.scala:4: error: object Tuple23 is not a member of package scala | |
(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23) | |
^ | |
one error 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
scalac Func22.scala |
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
object Func22 { | |
def main(args: Array[String]) { | |
val func22 : (Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int) => Int = null; | |
} | |
} |
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
object Func23 { | |
def main(args: Array[String]) { | |
val func23 : (Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int) => Int = null; | |
} | |
} |
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
object Tuple { | |
def getMaxValue(numbers: List[Int]) = { | |
val max = numbers.max | |
val index = numbers.indexOf(max) | |
(max, index) | |
} | |
def main(args: Array[String]){ | |
val numbers = List(1, 2, 3, 4, 5, 10, 6) | |
val maxValue = getMaxValue(numbers) | |
printf("max=%s \n", maxValue._1) | |
printf("index=%s \n", maxValue._2) | |
} | |
} |
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
object Tuple22 { | |
def get() = { | |
(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22) | |
} | |
def main(args: Array[String]){ | |
val tuple = get() | |
printf("element_1=%s \n", tuple._1) | |
printf("element_22=%s \n", tuple._22) | |
} | |
} |
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
object Tuple23 { | |
def get() = { | |
(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23) | |
} | |
def main(args: Array[String]){ | |
val tuple = get() | |
printf("element_1=%s \n", tuple._1) | |
printf("element_23=%s \n", tuple._23) | |
} | |
} |
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
object TupleImmutable { | |
def get(x1: Int, x2: Int) = { | |
(x1, x2) | |
} | |
def main(args: Array[String]){ | |
val tuple = get(1, 2) | |
tuple._1 = 3 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment