{"id":2661,"date":"2015-09-23T00:31:26","date_gmt":"2015-09-23T00:31:26","guid":{"rendered":"http:\/\/www.garysieling.com\/blog\/?p=2661"},"modified":"2015-09-23T00:31:26","modified_gmt":"2015-09-23T00:31:26","slug":"scala-corresponds-example","status":"publish","type":"post","link":"https:\/\/www.garysieling.com\/blog\/scala-corresponds-example\/","title":{"rendered":"Scala corresponds example"},"content":{"rendered":"<p>The &#8220;corresponds&#8221; function lets you match up items between two lists. For a good use of this, imagine you have a list of values, and a list of ranges (e.g. numeric or time windows):<\/p>\n<pre lang=\"scala\">\nval a = List(1, 2, 3, 4)\nval b = List((1,4), (2,5), (3,7), (3,11))\n<\/pre>\n<p>From that, you can write a function that compares one item to a range tuple:<\/p>\n<pre lang=\"scala\">\nval cmp = \n  (k: Int, v: (Int, Int)) => { \n    val (start, end) = v \n    k >= start && k < end \n  }\n\ncmp: (Int, (Int, Int)) => Boolean = <function2>\n<\/pre>\n<p>And then call corresponds (note the two sets of parenthesis):<\/p>\n<pre lang=\"scala\">\nscala> a.corresponds(b)(cmp)\n\nres22: Boolean = true\n<\/pre>\n<p>This works as expected. If the arrays aren&#8217;t of the same length, you&#8217;ll also get &#8216;false&#8217; as expected:<\/p>\n<pre lang=\"scala\">\nval a = List(1,2,3)\n\na.corresponds(b)(cmp)\nres24: Boolean = false\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The &#8220;corresponds&#8221; function lets you match up items between two lists. For a good use of this, imagine you have a list of values, and a list of ranges (e.g. numeric or time windows): val a = List(1, 2, 3, 4) val b = List((1,4), (2,5), (3,7), (3,11)) From that, you can write a function &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.garysieling.com\/blog\/scala-corresponds-example\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Scala corresponds example&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[4],"tags":[480,482,488],"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/2661"}],"collection":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/comments?post=2661"}],"version-history":[{"count":0,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/2661\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/media?parent=2661"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/categories?post=2661"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/tags?post=2661"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}