<commit_before>## Coroutines

### Example on Python

```python
def calc():
    history = []
    while True:
        x, y = (yield)
        if x == 'h':
            print history
            continue
        result = x + y
        print result
        history.append(result)

c = calc()

print type(c)  # <type 'generator'>

c.next()  # Необходимая инициация. Можно написать c.send(None)
c.send((1,2))  # Выведет 3
c.send((100, 30))  # Выведет 130
c.send((666, 0))  # Выведет 666
c.send(('h',0))  # Выведет [3, 130, 666]
c.close()  # Закрывем генератор
```

### Trying to make this in Silver with module (no generator objects, no prepared methods, but easier usage), but that's not real coroutine

```
module crtn
{
    var history = new List();
    def calc(x: int = 0, y: int = 0)
    {
        if x == 0 and y == 0 { history.print(); }
        else
        {
            var result: int = x + y;
            result.print();
            history.append(result);
        }
    }
    def close() { history.empty(); }
}

module main
{
    import crtn;
    def main()
    {
        calc(1, 2);
        calc(100, 30);
        calc(666, 0);
        calc();
        close();
    }
}
```

### The same in Silver with type and extension functions (posible to create objects)

```
module crtn
{
    type calcable = history: List;
    extend calcable
    {
        def calc(x: int = 0, y: int = 0)
        {
            if x == 0 and y == 0 { self.history.print(); }
            else
            {
                var result: int = x + y;
                result.print();
                self.history.append(result);
            }
        }
        def close() { self.history.empty(); }
    }
}

module main
{
    import crtn;
    def main()
    {
        c = new calcable([]);
        c.calc(1, 2);
        c.calc(100, 30);
        c.calc(666, 0);
        c.calc();
        c.close();
    }
}
```

### Wikipedia example

```
var q := new queue

coroutine produce
    loop<commit_msg>Update coroutines.md<commit_after>## Coroutines

### Example on Python

```python
def calc():
    history = []
    while True:
        x, y = (yield)
        if x == 'h':
            print history
            continue
        result = x + y
        print result
        history.append(result)

c = calc()

print type(c)  # <type 'generator'>

c.next()  # Необходимая инициация. Можно написать c.send(None)
c.send((1,2))  # Выведет 3
c.send((100, 30))  # Выведет 130
c.send((666, 0))  # Выведет 666
c.send(('h',0))  # Выведет [3, 130, 666]
c.close()  # Закрывем генератор
```

### Trying to make this in Silver with module (no generator objects, no prepared methods, but easier usage), but that's not real coroutine

```
module crtn
{
    var history = new List();
    proc calc(x: int = 0, y: int = 0)
    {
        if x == 0 and y == 0 { history.print(); }
        else
        {
            var result: int = x + y;
            result.print();
            history.append(result);
        }
    }
    proc close()
    {
        history.empty();
    }
}

module main
{
    import crtn;
    def main()
    {
        call calc(1, 2);
        call calc(100, 30);
        call calc(666, 0);
        call calc();
        call close();
    }
}
```

### The same in Silver with type and extension functions (posible to create objects)

```
module crtn
{
    type calcable = history: List;
    extend calcable
    {
        proc calc(x: int = 0, y: int = 0)
        {
            if x == 0 and y == 0 { self.history.print(); }
            else
            {
                var result: int = x + y;
                result.print();
                self.history.append(result);
            }
        }
        proc close()
        {
            self.history.empty();
        }
    }
}

module main
{
    import crtn;
    def main()
    {
        c = new calcable([]);
        c.calc(1, 2);
        c.calc(100, 30);
        c.calc(666, 0);
        c.calc();
        c.close();
    }
}
```

### Wikipedia example

```
var q := new queue

coroutine produce
    loop
{'fraction_non_alphanumeric': 0.14140159045725645, 'fraction_numerical': 0.027833001988071572, 'mean_word_length': 1.630718954248366, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '775390', 'n_tokens_mistral': 1427, 'n_tokens_neox': 1354, 'n_words': 397}