{"id":963,"date":"2019-11-15T13:18:26","date_gmt":"2019-11-15T00:18:26","guid":{"rendered":"https:\/\/www.ronella.xyz\/?p=963"},"modified":"2019-11-15T13:18:26","modified_gmt":"2019-11-15T00:18:26","slug":"using-reduce-method-to-do-and-and-or-testing","status":"publish","type":"post","link":"https:\/\/www.ronella.xyz\/?p=963","title":{"rendered":"Using Reduce Method to do AND and OR Testing"},"content":{"rendered":"<p>If the <strong>if conditions<\/strong> are becoming longer and complicated. It is possible to separate all the conditions to a list like the following:<\/p>\n<pre style=\"white-space: pre;\">var conditions : List&lt;block() : Boolean&gt;= {\r\n  \\-&gt; {\r\n    print(\"1\")\r\n    return true\r\n  }, \r\n  \\-&gt; {\r\n    print(\"2\")\r\n    return false\r\n  }\r\n}\r\n<\/pre>\n<blockquote><p>The above example is just a simple illustration and not good for actual coding.<\/p><\/blockquote>\n<p>The following code snippet is for hunting <strong>at least one truth condition<\/strong> using <strong>reduce method<\/strong> to do <strong>OR<\/strong> logic testing.<\/p>\n<pre style=\"white-space: pre;\">if (conditions?.reduce(false, \\ ___aggr, ___cond -&gt; ___aggr || ___cond())) {\r\n  print(\"I'm in\")\r\n}\r\n<\/pre>\n<blockquote><p>Once a truth condition was identified it stops checking the rest and the if condition is evaluated to true.<\/p><\/blockquote>\n<p>The following code snippet is for checking <strong>all the conditions are true<\/strong> using the <strong>reduce method<\/strong> to do <strong>AND<\/strong> logic testing.<\/p>\n<pre style=\"white-space: pre;\">if (conditions?.reduce(true, \\ ___aggr, ___cond -&gt; ___aggr &amp;&amp; ___cond())) {\r\n  print(\"I'm in\")\r\n}\r\n<\/pre>\n<blockquote><p>Once a false condition was identified it stops checking the rest and the if condition is evaluated to false.<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>If the if conditions are becoming longer and complicated. It is possible to separate all the conditions to a list like the following: var conditions : List&lt;block() : Boolean&gt;= { \\-&gt; { print(&#8220;1&#8221;) return true }, \\-&gt; { print(&#8220;2&#8221;) return false } } The above example is just a simple illustration and not good for [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[16],"tags":[],"_links":{"self":[{"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/posts\/963"}],"collection":[{"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=963"}],"version-history":[{"count":4,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/posts\/963\/revisions"}],"predecessor-version":[{"id":967,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/posts\/963\/revisions\/967"}],"wp:attachment":[{"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=963"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=963"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=963"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}