{"id":16483655,"url":"https://github.com/shpota/do-while-loop-implementation-in-scala","last_synced_at":"2025-06-12T00:05:37.914Z","repository":{"id":78769765,"uuid":"70178085","full_name":"Shpota/do-while-loop-implementation-in-scala","owner":"Shpota","description":"'Handmade' implementation of DO-WHILE loop using only build-in Scala features.","archived":false,"fork":false,"pushed_at":"2016-10-16T20:12:27.000Z","size":8,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-05-31T09:47:00.096Z","etag":null,"topics":["example","loop","scala","scala-example","scala-features"],"latest_commit_sha":null,"homepage":"","language":"Scala","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Shpota.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-10-06T17:46:01.000Z","updated_at":"2019-07-16T13:38:22.000Z","dependencies_parsed_at":null,"dependency_job_id":"efeb5611-e63a-4787-a193-1acb3d600ac2","html_url":"https://github.com/Shpota/do-while-loop-implementation-in-scala","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Shpota%2Fdo-while-loop-implementation-in-scala","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Shpota%2Fdo-while-loop-implementation-in-scala/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Shpota%2Fdo-while-loop-implementation-in-scala/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Shpota%2Fdo-while-loop-implementation-in-scala/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Shpota","download_url":"https://codeload.github.com/Shpota/do-while-loop-implementation-in-scala/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Shpota%2Fdo-while-loop-implementation-in-scala/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":258491320,"owners_count":22709889,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["example","loop","scala","scala-example","scala-features"],"created_at":"2024-10-11T13:14:39.068Z","updated_at":"2025-06-12T00:05:37.883Z","avatar_url":"https://github.com/Shpota.png","language":"Scala","funding_links":[],"categories":[],"sub_categories":[],"readme":"'Handmade' implementation of DO-WHILE loop in Scala\n===================================================\n\nScala is wery powerfool language. Almost every scala feature may be implemented using only built in language features. I want to demonstrate how to implementat a loop.\n\nScala already has ```do-while``` loop, that is why I'm using ```repeat-until``` naming in the example.\n\nWe want to implement something like this:\n```scala\nrepeat {\n  // do some stuff\n} until (condition)\n```\nTo achive this goal we have to declare a function ```repeat``` which will repeat some actions. The second part is the  function ```until```. It will have ```condition``` as an argument to contorol the loop. Finally we need to wire both the functions. To be able to do that we'll declare ```until``` function as a method inside the object returned by ```repeat``` function. \nSounds complicated, let's take a look on the code:\n```scala\ndef repeat(command: =\u003e Unit) = {\n  new {\n    def until(condition: =\u003e Boolean): Unit = {\n      command\n      if (condition)\n        until(condition)\n      else ()\n    }\n  }\n}\n```\nAs you can see we have anonymous object inside function body and one more functions inside. That means that the object will be returned by ```repeat``` so we can invoke ```until``` directly after ```repeat``` invocation.\nThe function body itself is pretty simple. First we invoke ```command``` and then depending on ```condition``` we decide if we need to terminate or we need to make one more recursive call.\n\nLet's see how it works on example:\n```scala\nvar i = 0\nrepeat {\n  println(\"Iteration #\" + i)\n  i = i + 1\n} until (i \u003c 5)\n```\nThe output as you may expect will be:\n```\nIteration #0\nIteration #1\nIteration #2\nIteration #3\nIteration #4\n```\n\n## How to run the code\n1) You need to have Scala SDK installed on your machine\n\n2) Run Scala console\n```bash\n$ scala\n```\n3) Run the code\n```bash\nscala\u003e :load loop.scala\n```\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshpota%2Fdo-while-loop-implementation-in-scala","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fshpota%2Fdo-while-loop-implementation-in-scala","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshpota%2Fdo-while-loop-implementation-in-scala/lists"}