코딩해/Scala
[Scala] sequence head 잘 뽑는 방법 | head of empty list 에러
haonly
2022. 1. 12. 15:08
728x90
반응형
Scala best practices에 따르면 sequence(list, map 등) 으로부터 head를 뽑을 때 `head` 보다는 `headOption` 사용을 권장
Seq.empty[Int].head
-> head of empty list exception 발생
headOption은 sequence의 head를 뽑아내는 좀 더 안전한 방법!
Seq(1, 2, 3).headOption
// res0: Option[Int] = Some(1)
Seq.empty[Int].headOption
// res1: Option[Int] = None
exception 방지를 위해서는 None이 아닌 조건을 넣으면 될듯
728x90
반응형