{"id":74,"date":"2012-09-15T19:06:26","date_gmt":"2012-09-15T19:06:26","guid":{"rendered":"http:\/\/blog.tmyymmt.net\/en\/?p=74"},"modified":"2012-09-15T19:06:26","modified_gmt":"2012-09-15T19:06:26","slug":"scala-solution-for-nested-match-statements-for-practice","status":"publish","type":"post","link":"https:\/\/blog.tmyymmt.net\/en\/?p=74","title":{"rendered":"[Scala] Solution for nested match statements (for practice)"},"content":{"rendered":"<p>practice about <a href=\"https:\/\/gist.github.com\/2382341\">https:\/\/gist.github.com\/2382341<\/a>.<br \/>\n<script src=\"https:\/\/gist.github.com\/3729301.js\"><\/script><noscript><pre><code class=\"language-scala scala\">\/\/ see https:\/\/gist.github.com\/2382341\n\n\/\/ scalaz for only solution3\nimport scalaz._\nimport Scalaz._\n\nobject SolutionForMultiNestedMatchforMyStudy {\n\n  def f(num: Int): Option[Int] = {\n    num match {\n      case 0 =&gt; Some(1)\n      case 1 =&gt; Some(2)\n      case 2 =&gt; Some(3)\n      case _ =&gt; None\n    }\n  }\n\n  def solution1(num: Int): Int = {\n    f(num) match {\n      case Some(v1) =&gt; {\n        f(v1) match {\n          case Some(v2) =&gt; {\n            f(v2) match {\n              case Some(v3) =&gt; v3\n              case _ =&gt; -3\n            }\n          }\n          case _ =&gt; -2\n        }\n      }\n      case _ =&gt; -1\n    }\n  }\n\n  def solution2(num: Int): Int = {\n    val v1 = f(num)\n    val v2 = v1.flatMap(f)\n    val v3 = v2.flatMap(f)\n\n    (v1, v2, v3) match {\n      case (Some(_), Some(_), Some(result)) =&gt; result\n      case (Some(_), Some(_), None) =&gt; -3\n      case (Some(_), None, None) =&gt; -2\n      case _ =&gt; -1\n    }\n  }\n\n  def solution3(num: Int): Int = {\n    f(num).toSuccess(-1).flatMap(f(_).toSuccess(-2)).flatMap(f(_).toSuccess(-3)).fold(identity, a =&gt; a)\n  }\n\n  def solution4(num: Int): Int = {\n    (for {\n      v1 &lt;- f(num).toRight(-1).right\n      v2 &lt;- f(v1).toRight(-2).right\n      v3 &lt;- f(v2).toRight(-3).right\n    } yield v3).merge\n  }\n\n  def solution5(num: Int): Int = {\n    val v1 = f(num).getOrElse { return -1 }\n    val v2 = f(v1).getOrElse { return -2 }\n    f(v2).getOrElse { return -3 }\n  }\n\n  def example {\n    (0 to 3).foreach{n =&gt; println(solution1(n))}\n    println\n    \n    (0 to 3).foreach{n =&gt; println(solution2(n))}\n    println\n    \n    (0 to 3).foreach{n =&gt; println(solution3(n))}\n    println\n    \n    (0 to 3).foreach{n =&gt; println(solution4(n))}\n    println\n    \n    (0 to 3).foreach{n =&gt; println(solution5(n))}\n  }\n}\n<\/code><\/pre><\/noscript><\/p>\n","protected":false},"excerpt":{"rendered":"<p>practice about https:\/\/gist.github.com\/2382341.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-74","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p2SiSL-1c","_links":{"self":[{"href":"https:\/\/blog.tmyymmt.net\/en\/index.php?rest_route=\/wp\/v2\/posts\/74","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.tmyymmt.net\/en\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.tmyymmt.net\/en\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.tmyymmt.net\/en\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.tmyymmt.net\/en\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=74"}],"version-history":[{"count":1,"href":"https:\/\/blog.tmyymmt.net\/en\/index.php?rest_route=\/wp\/v2\/posts\/74\/revisions"}],"predecessor-version":[{"id":75,"href":"https:\/\/blog.tmyymmt.net\/en\/index.php?rest_route=\/wp\/v2\/posts\/74\/revisions\/75"}],"wp:attachment":[{"href":"https:\/\/blog.tmyymmt.net\/en\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=74"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.tmyymmt.net\/en\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=74"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.tmyymmt.net\/en\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=74"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}