Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/shpota/do-while-loop-implementation-in-scala
'Handmade' implementation of DO-WHILE loop using only build-in Scala features.
https://github.com/shpota/do-while-loop-implementation-in-scala
example loop scala scala-example scala-features
Last synced: 18 days ago
JSON representation
'Handmade' implementation of DO-WHILE loop using only build-in Scala features.
- Host: GitHub
- URL: https://github.com/shpota/do-while-loop-implementation-in-scala
- Owner: Shpota
- Created: 2016-10-06T17:46:01.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2016-10-16T20:12:27.000Z (over 8 years ago)
- Last Synced: 2025-01-08T12:02:26.871Z (21 days ago)
- Topics: example, loop, scala, scala-example, scala-features
- Language: Scala
- Homepage:
- Size: 7.81 KB
- Stars: 2
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
'Handmade' implementation of DO-WHILE loop in Scala
===================================================Scala 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.
Scala already has ```do-while``` loop, that is why I'm using ```repeat-until``` naming in the example.
We want to implement something like this:
```scala
repeat {
// do some stuff
} until (condition)
```
To 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.
Sounds complicated, let's take a look on the code:
```scala
def repeat(command: => Unit) = {
new {
def until(condition: => Boolean): Unit = {
command
if (condition)
until(condition)
else ()
}
}
}
```
As 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.
The 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.Let's see how it works on example:
```scala
var i = 0
repeat {
println("Iteration #" + i)
i = i + 1
} until (i < 5)
```
The output as you may expect will be:
```
Iteration #0
Iteration #1
Iteration #2
Iteration #3
Iteration #4
```## How to run the code
1) You need to have Scala SDK installed on your machine2) Run Scala console
```bash
$ scala
```
3) Run the code
```bash
scala> :load loop.scala
```