# More concise alternatives to \`Iterator.from()\`?

**URL:** <https://es.discourse.group/t/more-concise-alternatives-to-iterator-from/2234>\
**Category:** 💡 Ideas\
**Created:** [December 3, 2024, 5:17pm UTC](https://es.discourse.group/t/more-concise-alternatives-to-iterator-from/2234 "2024-12-03T17:17:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rauschma](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/rauschma/32/1152_2.png) [@rauschma](https://es.discourse.group/u/rauschma)\
**Post date:** [December 3, 2024, 5:17pm UTC](https://es.discourse.group/t/more-concise-alternatives-to-iterator-from/2234/1 "2024-12-03T17:17:05Z")

</div>

## Verbose example

Example where using `Intl.Segmenter` is a bit verbose:

```js
const segmenter = new Intl.Segmenter('en-us', { granularity: 'word' });
const segments = segmenter.segment('How are you?');
console.log(
  Iterator.from(segments).filter(x => x.isWordLike).map(x => x.segment).toArray()
);

```

## Solution 1: `Iterator()`

[`Iterator`](https://tc39.es/ecma262/#sec-iterator) could become callable and behave like `Iterator.from()`:

```js
Iterator(segments).filter(x => x.isWordLike).map(x => x.segment).toArray()

```

- Precedent: `Object()`, `Number()`, `String()`, etc. already coerce values.

## Solution 2: `Object.prototype.values()`

Preparation:

```js
Object.prototype.values = function () { return this[Symbol.iterator]() };

```

Usage:

```js
segments.values().filter(x => x.isWordLike).map(x => x.segment).toArray()

```

This method could also be called `.iter()`.

## Solution 3: `%IntlSegmentsPrototype%.values()`

> **[ECMAScript® 2026 Internationalization API Specification](https://tc39.es/ecma402/#sec-segments-objects)**
>
> Introduction
> This specification's source can be found at https://github.com/tc39/ecma402.
> The ECMAScript 2026 Internationalization API Specification (ECMA-402 13th Edition), provides key language sensitive functionality as a complement to...

```js
segments.values().filter(x => x.isWordLike).map(x => x.segment).toArray()

```

- Precedent: Many iterable objects have the method `.values()` – e.g. instances of `Array`, `Map`, `Set`.
- Downside: only useful in this context (vs. everywhere the Iterator API is used).

---

<div class="post-metadata">

**Author:** ![bakkot](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/bakkot/32/22_2.png) [@bakkot](https://es.discourse.group/u/bakkot)\
**Post date:** [December 3, 2024, 5:36pm UTC](https://es.discourse.group/t/more-concise-alternatives-to-iterator-from/2234/2 "2024-12-03T17:36:32Z")

</div>

I think iterables should always have a string-keyed name for getting an iterator, as an alias for the `Symbol.iterator` method. It shouldn't universally be named `values` - for example, for strings I think we should have `codepoints` - but I think that's the ideal, rather than trying to make `Iterator.from` more convenient. That would be [a feature request for 402](https://github.com/tc39/ecma402), which they accept as issues.

Note that almost every iterable web API already has `.values` (in [webidl](https://webidl.spec.whatwg.org/#idl-iterable) declaring something as iterable automatically adds `.values`, `.keys`, and a few others), so it's only a few places in JS which are missing such a method.

Re: solution 2, we are never going to add any new string-keyed properties to `Object.prototype` under any circumstances.

---

<div class="post-metadata">

**Author:** ![rauschma](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/rauschma/32/1152_2.png) [@rauschma](https://es.discourse.group/u/rauschma)\
**Post date:** [December 3, 2024, 6:04pm UTC](https://es.discourse.group/t/more-concise-alternatives-to-iterator-from/2234/3 "2024-12-03T18:04:07Z")

</div>

Thanks! Good points. Strings are indeed the only other case I can think of where I’d use `Iterator.from()`.

I filed an issue: [`%IntlSegmentsPrototype%.values()` · Issue #945 · tc39/ecma402 · GitHub](https://github.com/tc39/ecma402/issues/945)

---

<div class="post-metadata">

**Author:** ![bakkot](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/bakkot/32/22_2.png) [@bakkot](https://es.discourse.group/u/bakkot)\
**Post date:** [December 3, 2024, 6:17pm UTC](https://es.discourse.group/t/more-concise-alternatives-to-iterator-from/2234/4 "2024-12-03T18:17:57Z")

</div>

I'm intending to propose `.codepoints` for strings (or some other name) when I get some time, so hopefully we'll have these all rounded out someday.

---

<div class="post-metadata">

**Author:** ![rauschma](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/rauschma/32/1152_2.png) [@rauschma](https://es.discourse.group/u/rauschma)\
**Post date:** [December 3, 2024, 7:28pm UTC](https://es.discourse.group/t/more-concise-alternatives-to-iterator-from/2234/5 "2024-12-03T19:28:33Z")

</div>

Great!

(I’m glad we have Iterator methods now. It took me a while to figure out that iterator-first can fit well into JS—seeing Rust’s iteration API finally made it click.)
