# \*list()

[JS](../../..)⟩ [iteration](../..) ⟩ [custom generators]() ⟩ \*list()

{% tabs %}
{% tab title="💾 程式" %}
:floppy\_disk: replit：[list()](https://replit.com/@pegasusroe/list#list.js)

👉 compare： [closedrange](closedrange "mention"), [closedrange](../../iterable/custom/closedrange "mention")

```javascript
// ⭐️ list()  (generator function)
function* list(count, {start=0, step=1}={}){
    for(let i = 0; i < count; i++){
        yield start + step * i;
    }
}
```

💈範例：

```javascript
// ⭐️ for-of loop
for (const i of list(5)) {
    sum += i;
}                                // sum = 10

// ⭐️ generator object -> array
[...list(5)],                           // [ 0, 1, 2, 3, 4 ]
[...list(5, {start: 1})],               // [ 1, 2, 3, 4, 5 ]
[...list(5, {step: 2})],                // [ 0, 2, 4, 6, 8 ]
[...list(5, {start: 2, step: -1})],     // [ 2, 1, 0, -1, -2 ]
```
{% endtab %}

{% tab title="👥 相關" %}
* similar objects：
  * [closedrange](../../iterable/custom/closedrange "mention") - range of integers (the hard way)
  * [closedrange](closedrange "mention") - range of integers (generator: the easy way)
  * [integers](integers "mention") - infinite non-negative integers
  * [sequence](../../iterable/custom/sequence "mention") - finite/infinite sequence of numbers.
* <mark style="color:yellow;">**import**</mark> [iterator](../../iterator/iterator "mention") to <mark style="color:yellow;">**extend**</mark> built-in iterators
{% endtab %}
{% endtabs %}
