Reputation: 51
val x=(1,(2,(3,(4,5))))
how the get the 4 from above using Scala?
can someone please to find the value using the Scala code
Upvotes: 3
Views: 181
Reputation: 143531
You can access individual elements of tuple by position with individual elements being named _1
, _2
etc. :
val four = x._2._2._2._1
Or using pattern matching:
val (_,(_,(_,(four,_)))) = x
Upvotes: 5
Reputation: 17528
You can use pattern matching to decompose the expression:
scala> val (a,(b,(c,(d,e)))) = (1,(2,(3,(4,5))))
a: Int = 1
b: Int = 2
c: Int = 3
d: Int = 4
e: Int = 5
In case you are not interested in other values than 4:
scala> val (_, (_, (_, (x, _)))) = (1,(2,(3,(4,5))))
x: Int = 4
Upvotes: 6