mirror of
https://github.com/jbranchaud/til
synced 2026-01-05 08:08:02 +00:00
Add Break Out Of A While Loop as a reasonml til
This commit is contained in:
@@ -10,7 +10,7 @@ smart people at [Hashrocket](http://hashrocket.com/).
|
|||||||
For a steady stream of TILs from a variety of rocketeers, checkout
|
For a steady stream of TILs from a variety of rocketeers, checkout
|
||||||
[til.hashrocket.com](https://til.hashrocket.com/).
|
[til.hashrocket.com](https://til.hashrocket.com/).
|
||||||
|
|
||||||
_683 TILs and counting..._
|
_684 TILs and counting..._
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -501,6 +501,7 @@ _683 TILs and counting..._
|
|||||||
|
|
||||||
### ReasonML
|
### ReasonML
|
||||||
|
|
||||||
|
- [Break Out Of A While Loop](reason/break-out-of-a-while-loop.md)
|
||||||
- [Defining Variants With Constructor Arguments](reason/defining-variants-with-constructor-arguments.md)
|
- [Defining Variants With Constructor Arguments](reason/defining-variants-with-constructor-arguments.md)
|
||||||
- [Exhaustive Pattern Matching Of List Variants](reason/exhaustive-pattern-matching-of-list-variants.md)
|
- [Exhaustive Pattern Matching Of List Variants](reason/exhaustive-pattern-matching-of-list-variants.md)
|
||||||
- [Helping The Compiler Help Us With Variants](reason/helping-the-compiler-help-us-with-variants.md)
|
- [Helping The Compiler Help Us With Variants](reason/helping-the-compiler-help-us-with-variants.md)
|
||||||
|
|||||||
27
reason/break-out-of-a-while-loop.md
Normal file
27
reason/break-out-of-a-while-loop.md
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
# Break Out Of A While Loop
|
||||||
|
|
||||||
|
The `while` construct is a great way to loop indefinitely. You may
|
||||||
|
eventually want to break out of the loop. For that, you are going to need to
|
||||||
|
invalidate the _while condition_. One way of going about this is creating a
|
||||||
|
mutable ref, changing it from true to false when a certain condition is met.
|
||||||
|
|
||||||
|
```reasonml
|
||||||
|
let break = ref(true);
|
||||||
|
|
||||||
|
while (break^) {
|
||||||
|
switch (Unix.readdir(currentDir)) {
|
||||||
|
| exception End_of_file => break := false
|
||||||
|
| item => print_endline(item);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
Here we have a mutable ref called `break` which starts as `true`. This is
|
||||||
|
our _while condition_. Its actual value can be referenced by appending the
|
||||||
|
`^` character -- hence `break^`. Once a certain condition is met inside our
|
||||||
|
`while` block, we can set `break` to `false` using the `:=` operator.
|
||||||
|
|
||||||
|
The above code snippet can be seen in full details
|
||||||
|
[here](https://github.com/jbranchaud/basic-ls-reason-native/blob/master/src/Index.re).
|
||||||
|
|
||||||
|
[source](https://reasonml.github.io/docs/en/imperative-loops.html#tips-tricks)
|
||||||
Reference in New Issue
Block a user