Feeling a little frustrated as I just spent a considerable amount of time figuring out how to retrieve an item by index from a Seq. Finally figured it out. It can be done using the lift method. This method is also supported by other collections.
This is the code snippet from REPL:
scala> Seq(1,2,3,4,5) res0: Seq[Int] = Seq(1, 2, 3, 4, 5) scala> res0 lift 452 res1: Option[Int] = None scala> res0 lift 0 res2: Option[Int] = Some(1)
From the Scala documentation:
def lift: (Int) ? Option[A] Turns this partial function into a plain function returning an Option result. returns a function that takes an argument x to Some(this(x)) if this is defined for x, and to None otherwise. Definition Classes PartialFunction See also Function.unlift
Seriously, this is meant to be helpful?
Yes it is very simple to retrieve an item by index once you know how to. Hope this saves someone else some frustration. Happy coding 🙂