Skip to content

Instantly share code, notes, and snippets.

@f81
Created August 12, 2013 14:46
Show Gist options
  • Save f81/6211440 to your computer and use it in GitHub Desktop.
Save f81/6211440 to your computer and use it in GitHub Desktop.
第7章:タプルにチャレンジ! ref: http://qiita.com/f81@github/items/a8419532c316d190782d
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)
$ scalac Tuple.scala
$ scala Tuple
max=10
index=5
$ 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
$ scalac CaseClassTuple.scala
$ scala CaseClassTuple
fullname_1=清美 椿山
fullname_2=清美 椿山
$ scalac TupleImmutable.scala
TupleImmutable.scala:9: error: reassignment to val
tuple._1 = 3
^
one error found
val max = numbers.max
val index = numbers.indexOf(max)
val maxValue = getMaxValue(numbers)
printf("max=%s \n", maxValue._1)
printf("index=%s \n", maxValue._2)
$ scalac Tuple22.scala
$ scala Tuple22
element_1=1
element_22=22
$ 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
scalac Func22.scala
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;
}
}
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;
}
}
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)
}
}
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)
}
}
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)
}
}
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