Skip to content
This repository has been archived by the owner on Oct 8, 2024. It is now read-only.

Commit

Permalink
add flatMap, fixes #45
Browse files Browse the repository at this point in the history
  • Loading branch information
devsnek committed Aug 27, 2019
1 parent 11193e5 commit 8373112
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions spec.html
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ <h1>GetIteratorDirect ( _obj_ )</h1>
<emu-alg>
1. If Type(_obj_) is not Object, throw a *TypeError* exception.
1. Let _nextMethod_ be ? GetV(_obj_, `"next"`).
1. If IsCallable(_nextMethod_) is *false*, throw a *TypeError* exception.
1. Let _iteratorRecord_ be Record { [[Iterator]]: _obj_, [[NextMethod]]: _nextMethod_, [[Done]]: *false* }.
1. Return _iteratorRecord_.
</emu-alg>
Expand Down Expand Up @@ -314,6 +315,35 @@ <h1>%Iterator.prototype%.asIndexedPairs ( )</h1>
</emu-alg>
</emu-clause>

<emu-clause id="sec-iteratorprototype-flatmap">
<h1>%Iterator.prototype%.flatMap ( _mapper_ )</h1>
<p>%Iterator.prototype%.flatMap is a built-in generator function which, when called, performs the following steps:</p>
<emu-alg>
1. Let _iterated_ be ? GetIteratorDirect(*this* value).
1. If IsCallable(_mapper_) is *false*, throw a *TypeError* exception.
</emu-alg>
<p>The body of %Iterator.prototype%.flatMap is composed of the following steps:</p>
<emu-alg>
1. Repeat,
1. Let _next_ be ? IteratorNext(_iterated_).
1. If _next_ is *false*, return *undefined*.
1. Let _value_ be ? IteratorValue(_next_).
1. Let _mapped_ be ? Call(_mapper_, *undefined*, &laquo; _value_ &raquo;).
1. Let _usingIterator_ be ? Get(_mapped_, @@iterator).
1. If _usingIterator_ is *undefined*, then
1. Perform ? Yield(_mapped_).
1. Else,

This comment has been minimized.

Copy link
@zloirock

zloirock Aug 27, 2019

Contributor

Get works only on objects, so we are not able to return a primitive from mapper - inconsistency with Array#flatMap. Strings are iterable, so they also will be flattened.

This comment has been minimized.

Copy link
@ljharb

ljharb Aug 27, 2019

Member

That can be solved by adding a ToObject in the GetIterator call, or by only making it if the Type is Object.

This comment has been minimized.

Copy link
@devsnek

devsnek Aug 27, 2019

Author Member

Good catch, i will add a check for Type being Object.

This comment has been minimized.

Copy link
@zloirock

zloirock Oct 22, 2019

Contributor

@devsnek seems it's still not fixed.

1. Let _innerIterator_ be ? GetIterator(_mapped_, ~sync~, _usingIterator_).
1. Let _innerAlive_ be *true*.
1. Repeat, while _innerAlive_ is *true*,
1. Let _innerNext_ be ? IteratorNext(_innerIterator_).
1. If next is *false*, set _innerAlive_ to *false*.
1. Else,
1. Let _innerValue_ be ? IteratorNext(_innerNext_).
1. Perform ? Yield(_innerValue_).
</emu-alg>
</emu-clause>

<emu-clause id="sec-iteratorprototype-reduce">
<h1>%Iterator.prototype%.reduce ( _reducer_ [ , _initialValue_ ] )</h1>
<emu-alg>
Expand Down

1 comment on commit 8373112

@zloirock
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, why the results of yield are not handled, unlike other methods?

Please sign in to comment.