{"id":3365,"date":"2016-03-12T21:36:14","date_gmt":"2016-03-12T21:36:14","guid":{"rendered":"http:\/\/www.garysieling.com\/blog\/?p=3365"},"modified":"2016-03-12T21:36:14","modified_gmt":"2016-03-12T21:36:14","slug":"scala-while-comprehension","status":"publish","type":"post","link":"https:\/\/www.garysieling.com\/blog\/scala-while-comprehension\/","title":{"rendered":"Scala: while comprehension"},"content":{"rendered":"<p>In scala, you can do for comprehensions like so:<\/p>\n<pre lang=\"scala\">\nimport scala.collection.JavaConverters._\n\nfor (x <- myCollection) \n  yield x.someFunction\n<\/pre>\n<p>Weirdly you can't do this:<\/p>\n<pre lang=\"scala\">\nwhile (myCollection.hasNext) \n  yield x.next\n<\/pre>\n<p>This is a common problem with JDBC - ideally the language should let you do this:<\/p>\n<pre lang=\"scala\">\nval query = \"\"\"\n  | select col1, col2 from my_table;\n\"\"\"\n\nval resultSet = runQuery(query)\nval results: Iterable[Iterable[String]] =\n  while (resultSet.hasNext) \n    resultSet.columns.map(\n      (columnName) => \n        resultSet.get(columnName)\n    )\n<\/pre>\n<p>Obviously this doesn't work, or you wouldn't be here.<\/p>\n<p>To do this the scala way, you can use \"Iterator.continually\", like so:<\/p>\n<pre lang=\"scala\">\nimport scala.collection.JavaConverters._\n\nval data =\n  Iterator.continually {\n    resultSet.next()\n  }.takeWhile(\n    (x) => x\n  ).map(\n    (x) => {\n      resultSet.getColumnNames.asScala.map(\n        (x) => resultSet.getString(x)\n      )\n    }\n  )\n<\/pre>\n<p>This will give you the dataset you wanted - you can also remove the types from the above example, they are included so that this is easier to understand.<\/p>\n<p>You can also wrap the above command in a try {}. If you do this, you may wish to realize the results right away, by adding a \".toList\" at the end of the collection, so that you don't hold on to an iterator that is operating on a closed database connection.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In scala, you can do for comprehensions like so: import scala.collection.JavaConverters._ for (x<\/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,485,486],"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/3365"}],"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=3365"}],"version-history":[{"count":0,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/3365\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/media?parent=3365"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/categories?post=3365"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/tags?post=3365"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}