Loops

Source: reference-book. Raw markdown: /raw/docs/reference-book/9-common-patterns/loops.md

As you know Simorg doesn't have any third party entity sitting in the code dictating its own rules to the data. Instead, we believe in data-driven approaches by which the data is being streamed in certain paths which demonstrate certain behaviors.

Loops are a great example of this data-driven approach.

This code is demonstrating a typical classical loop in the C language,

for (int i = 10; i>0; i--) {
  printf("index: %d", i);
}

Now the same code according to a data-driven pattern is like,

$i >0 -- i ?
10 i

The above code is having a post-declaration syntax of a variable that continues reducing the counter and pushing data back into pipeline's head until it reaches 0.

We can even use a gate and make it a self-starting counter.

:[10, :] $i > 0 -- i ?

The point here is that we made the behavior of a loop using a data-driven approach.

If the initializer is always a positive value then the above loop can be even more simplified as,

$i--&i ?
10 i

Here, the truthy check operator <<<&>>> is replacing $$$KeywordSnippet keyword=>0 $$$ . This is possible because <<<&>>> will pass the vibration only if the incoming is a truthy value hence, preventing the flow to continue when <<<i>>> is <<<0>>>.

A typical usage of loop in CPLs is when we want to iterate over collections of items. Simorg doesn't need that approach either. As the collection itself naturally is iterable using <<<Collector Operator>>> so still the behavior of <<<foreach>>> is following a data-driven pattern.