本当にちょっとしたことだけど、Option
の中身が期待通りかを確認するののスッキリした書き方をメモ。
exists
までは思いつくけど、 Option
に contains
があるのは、最初はピンとこなかった。
val op: Option[Int] = ???
op.map(_ == 334).getOrElse(false) // △
op.exists(_ == 334) // ○
op.contains(334) // ◎
More than 5 years have passed since last update.
本当にちょっとしたことだけど、Option
の中身が期待通りかを確認するののスッキリした書き方をメモ。
exists
までは思いつくけど、 Option
に contains
があるのは、最初はピンとこなかった。
val op: Option[Int] = ???
op.map(_ == 334).getOrElse(false) // △
op.exists(_ == 334) // ○
op.contains(334) // ◎
Register as a new user and use Qiita more conveniently