Skip to content

Instantly share code, notes, and snippets.

@gakuzzzz
Created March 3, 2015 17:58
Show Gist options
  • Save gakuzzzz/78f52e39d8275309df8a to your computer and use it in GitHub Desktop.
Save gakuzzzz/78f52e39d8275309df8a to your computer and use it in GitHub Desktop.
Option.apply の使いどころ
scala> val f: (Option[Int], Int) => Option[Int] = _ orElse Some(_)
f: (Option[Int], Int) => Option[Int] = <function2>
scala> val seq: Seq[Int] = Seq(1,2,3)
seq: Seq[Int] = List(1, 2, 3)
scala> seq.foldLeft(Some(0))(f)
<console>:10: error: type mismatch;
found : (Option[Int], Int) => Option[Int]
required: (Some[Int], Int) => Some[Int]
seq.foldLeft(Some(0))(f)
^
scala> seq.foldLeft(Option(0))(f)
res1: Option[Int] = Some(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment