How to return a parameter when reading a vector

Reading from a vector, I want to return it when I try to read an index that is beyond the scope, otherwise. Is there a standard method for doing this?

+6
source share
1 answer

You can use lift :

 val v = Vector(1, 2, 3) v.lift(0) //Some(1) v.lift(5) //None 

Please note that this works for any partial function.

+13
source

Source: https://habr.com/ru/post/977377/


All Articles