Skip to content

Commit

Permalink
fixes #229: flatMap supports returning both iterables and iterators
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelficarra committed Sep 9, 2022
1 parent 4c47229 commit 2f4209a
Showing 1 changed file with 33 additions and 2 deletions.
35 changes: 33 additions & 2 deletions spec.html
Expand Up @@ -149,6 +149,37 @@ <h1>
1. Return _iteratorRecord_.
</emu-alg>
</emu-clause>

<emu-clause id="sec-getiteratorflattenable" type="abstract operation">
<h1>
GetIteratorFlattenable (
_obj_: an ECMAScript language value,
optional _hint_: ~sync~ or ~async~,
): either a normal completion containing an Iterator Record or a throw completion
</h1>
<dl class="header">
</dl>
<emu-alg>
1. If _hint_ is not present, set _hint_ to ~sync~.
1. If _iterator_ is either *undefined* or *null*, throw a *TypeError* exception.
1. Let _method_ be *undefined*.
1. If _hint_ is ~async~, then
1. Set _method_ to ? GetV(_obj_, @@asyncIterator).
1. If IsCallable(_method_) is *false*, then
1. Set _method_ to ? GetV(_obj_, @@iterator).
1. If IsCallable(_method_) is *false*, then
1. Let _iterator_ be _obj_.
1. Else,
1. Let _iterator_ be ? Call(_method_, _obj_).
1. If _iterator_ is not an Object, throw a *TypeError* exception.
1. Let _nextMethod_ be ? Get(_iterator_, *"next"*).
1. If IsCallable(_nextMethod_) is *false*, throw a *TypeError* exception.
1. Let _iteratorRecord_ be the Iterator Record { [[Iterator]]: _iterator_, [[NextMethod]]: _nextMethod_, [[Done]]: *false* }.
1. If _hint_ is ~async~, then
1. Return CreateAsyncFromSyncIterator(_iteratorRecord_).
1. Return _iteratorRecord_.
</emu-alg>
</emu-clause>
</emu-clause>
</emu-clause>

Expand Down Expand Up @@ -557,7 +588,7 @@ <h1>Iterator.prototype.flatMap ( _mapper_ )</h1>
1. Let _value_ be ? IteratorValue(_next_).
1. Let _mapped_ be Completion(Call(_mapper_, *undefined*, &laquo; _value_ &raquo;)).
1. IfAbruptCloseIterator(_mapped_, _iterated_).
1. Let _innerIterator_ be Completion(GetIterator(_mapped_, ~sync~)).
1. Let _innerIterator_ be Completion(GetIteratorFlattenable(_mapped_, ~sync~)).
1. IfAbruptCloseIterator(_innerIterator_, _iterated_).
1. Let _innerAlive_ be *true*.
1. Repeat, while _innerAlive_ is *true*,
Expand Down Expand Up @@ -823,7 +854,7 @@ <h1>AsyncIterator.prototype.flatMap ( _mapper_ )</h1>
1. IfAbruptCloseAsyncIterator(_mapped_, _iterated_).
1. Set _mapped_ to Completion(Await(_mapped_)).
1. IfAbruptCloseAsyncIterator(_mapped_, _iterated_).
1. Let _innerIterator_ be Completion(GetIterator(_mapped_, ~async~)).
1. Let _innerIterator_ be Completion(GetIteratorFlattenable(_mapped_, ~async~)).
1. IfAbruptCloseAsyncIterator(_innerIterator_, _iterated_).
1. Let _innerAlive_ be *true*.
1. Repeat, while _innerAlive_ is *true*,
Expand Down

0 comments on commit 2f4209a

Please sign in to comment.