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

Commit

Permalink
fixes #207: add a counter parameter to Array analogues that have them
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelficarra committed Aug 9, 2022
1 parent 3e24c09 commit 7b5e897
Showing 1 changed file with 50 additions and 18 deletions.
68 changes: 50 additions & 18 deletions spec.html
Original file line number Diff line number Diff line change
Expand Up @@ -441,14 +441,16 @@ <h1>Iterator.prototype.map ( _mapper_ )</h1>
1. Let _iterated_ be ? GetIteratorDirect(*this* value).
1. If IsCallable(_mapper_) is *false*, throw a *TypeError* exception.
1. Let _closure_ be a new Abstract Closure with no parameters that captures _iterated_ and _mapper_ and performs the following steps when called:
1. Let _counter_ be 0.
1. Repeat,
1. Let _next_ be ? IteratorStep(_iterated_).
1. If _next_ is *false*, return *undefined*.
1. Let _value_ be ? IteratorValue(_next_).
1. Let _mapped_ be Completion(Call(_mapper_, *undefined*, &laquo; _value_ &raquo;)).
1. Let _mapped_ be Completion(Call(_mapper_, *undefined*, &laquo; _value_, 𝔽(_counter_) &raquo;)).
1. IfAbruptCloseIterator(_mapped_, _iterated_).
1. Let _completion_ be Completion(Yield(_mapped_)).
1. IfAbruptCloseIterator(_completion_, _iterated_).
1. Set _counter_ to _counter_ + 1.
1. Return CreateIteratorFromClosure(_closure_, ~Iterator Helper~, %IteratorHelperPrototype%).
</emu-alg>
</emu-clause>
Expand All @@ -459,15 +461,17 @@ <h1>Iterator.prototype.filter ( _filterer_ )</h1>
1. Let _iterated_ be ? GetIteratorDirect(*this* value).
1. If IsCallable(_filterer_) is *false*, throw a *TypeError* exception.
1. Let _closure_ be a new Abstract Closure with no parameters that captures _iterated_ and _filterer_ and performs the following steps when called:
1. Let _counter_ be 0.
1. Repeat,
1. Let _next_ be ? IteratorStep(_iterated_).
1. If _next_ is *false*, return *undefined*.
1. Let _value_ be ? IteratorValue(_next_).
1. Let _selected_ be Completion(Call(_filterer_, *undefined*, &laquo; _value_ &raquo;)).
1. Let _selected_ be Completion(Call(_filterer_, *undefined*, &laquo; _value_, 𝔽(_counter_) &raquo;)).
1. IfAbruptCloseIterator(_selected_, _iterated_).
1. If ToBoolean(_selected_) is *true*, then
1. Let _completion_ be Completion(Yield(_value_)).
1. IfAbruptCloseIterator(_completion_, _iterated_).
1. Set _counter_ to _counter_ + 1.
1. Return CreateIteratorFromClosure(_closure_, ~Iterator Helper~, %IteratorHelperPrototype%).
</emu-alg>
</emu-clause>
Expand Down Expand Up @@ -530,9 +534,9 @@ <h1>Iterator.prototype.indexed ( )</h1>
1. If _next_ is *false*, return *undefined*.
1. Let _value_ be ? IteratorValue(_next_).
1. Let _pair_ be CreateArrayFromList(&laquo; 𝔽(_index_), _value_ &raquo;).
1. Set _index_ to _index_ + 1.
1. Let _completion_ be Completion(Yield(_pair_)).
1. IfAbruptCloseIterator(_completion_, _iterated_).
1. Set _index_ to _index_ + 1.
1. Return CreateIteratorFromClosure(_closure_, ~Iterator Helper~, %IteratorHelperPrototype%).
</emu-alg>
</emu-clause>
Expand All @@ -543,11 +547,12 @@ <h1>Iterator.prototype.flatMap ( _mapper_ )</h1>
1. Let _iterated_ be ? GetIteratorDirect(*this* value).
1. If IsCallable(_mapper_) is *false*, throw a *TypeError* exception.
1. Let _closure_ be a new Abstract Closure with no parameters that captures _iterated_ and _mapper_ and performs the following steps when called:
1. Let _counter_ be 0.
1. Repeat,
1. Let _next_ be ? IteratorStep(_iterated_).
1. If _next_ is *false*, return *undefined*.
1. Let _value_ be ? IteratorValue(_next_).
1. Let _mapped_ be Completion(Call(_mapper_, *undefined*, &laquo; _value_ &raquo;)).
1. Let _mapped_ be Completion(Call(_mapper_, *undefined*, &laquo; _value_, 𝔽(_counter_) &raquo;)).
1. IfAbruptCloseIterator(_mapped_, _iterated_).
1. Let _innerIterator_ be Completion(GetIterator(_mapped_, ~sync~)).
1. IfAbruptCloseIterator(_innerIterator_, _iterated_).
Expand All @@ -565,6 +570,7 @@ <h1>Iterator.prototype.flatMap ( _mapper_ )</h1>
1. Let _backupCompletion_ be Completion(IteratorClose(_innerIterator_, _completion_)).
1. IfAbruptCloseIterator(_backupCompletion_, _iterated_).
1. Return ? IteratorClose(_completion_, _iterated_).
1. Set _counter_ to _counter_ + 1.
1. Return CreateIteratorFromClosure(_closure_, ~Iterator Helper~, %IteratorHelperPrototype%).
</emu-alg>
</emu-clause>
Expand All @@ -580,13 +586,15 @@ <h1>Iterator.prototype.reduce ( _reducer_ [ , _initialValue_ ] )</h1>
1. Let _accumulator_ be ? IteratorValue(_next_).
1. Else,
1. Let _accumulator_ be _initialValue_.
1. Let _counter_ be 0.
1. Repeat,
1. Let _next_ be ? IteratorStep(_iterated_).
1. If _next_ is *false*, return _accumulator_.
1. Let _value_ be ? IteratorValue(_next_).
1. Let _result_ be Completion(Call(_reducer_, *undefined*, &laquo; _accumulator_, _value_ &raquo;)).
1. Let _result_ be Completion(Call(_reducer_, *undefined*, &laquo; _accumulator_, _value_, 𝔽(_counter_) &raquo;)).
1. IfAbruptCloseIterator(_result_, _iterated_).
1. Set _accumulator_ to _result_.[[Value]].
1. Set _counter_ to _counter_ + 1.
</emu-alg>
</emu-clause>

Expand Down Expand Up @@ -619,12 +627,14 @@ <h1>Iterator.prototype.forEach ( _fn_ )</h1>
<emu-alg>
1. Let _iterated_ be ? GetIteratorDirect(*this* value).
1. If IsCallable(_fn_) is *false*, throw a *TypeError* exception.
1. Let _counter_ be 0.
1. Repeat,
1. Let _next_ be ? IteratorStep(_iterated_).
1. If _next_ is *false*, return *undefined*.
1. Let _value_ be ? IteratorValue(_next_).
1. Let _result_ be Completion(Call(_fn_, *undefined*, &laquo; _value_ &raquo;)).
1. Let _result_ be Completion(Call(_fn_, *undefined*, &laquo; _value_, 𝔽(_counter_) &raquo;)).
1. IfAbruptCloseIterator(_result_, _iterated_).
1. Set _counter_ to _counter_ + 1.
</emu-alg>
</emu-clause>

Expand All @@ -633,13 +643,15 @@ <h1>Iterator.prototype.some ( _fn_ )</h1>
<emu-alg>
1. Let _iterated_ be ? GetIteratorDirect(*this* value).
1. If IsCallable(_fn_) is *false*, throw a *TypeError* exception.
1. Let _counter_ be 0.
1. Repeat,
1. Let _next_ be ? IteratorStep(_iterated_).
1. If _next_ is *false*, return *false*.
1. Let _value_ be ? IteratorValue(_next_).
1. Let _result_ be Completion(Call(_fn_, *undefined*, &laquo; _value_ &raquo;)).
1. Let _result_ be Completion(Call(_fn_, *undefined*, &laquo; _value_, 𝔽(_counter_) &raquo;)).
1. IfAbruptCloseIterator(_result_, _iterated_).
1. If ToBoolean(_result_) is *true*, return ? IteratorClose(_iterated_, NormalCompletion(*true*)).
1. Set _counter_ to _counter_ + 1.
</emu-alg>
</emu-clause>

Expand All @@ -648,13 +660,15 @@ <h1>Iterator.prototype.every ( _fn_ )</h1>
<emu-alg>
1. Let _iterated_ be ? GetIteratorDirect(*this* value).
1. If IsCallable(_fn_) is *false*, throw a *TypeError* exception.
1. Let _counter_ be 0.
1. Repeat,
1. Let _next_ be ? IteratorStep(_iterated_).
1. If _next_ is *false*, return *true*.
1. Let _value_ be ? IteratorValue(_next_).
1. Let _result_ be Completion(Call(_fn_, *undefined*, &laquo; _value_ &raquo;)).
1. Let _result_ be Completion(Call(_fn_, *undefined*, &laquo; _value_, 𝔽(_counter_) &raquo;)).
1. IfAbruptCloseIterator(_result_, _iterated_).
1. If ToBoolean(_result_) is *false*, return ? IteratorClose(_iterated_, NormalCompletion(*false*)).
1. Set _counter_ to _counter_ + 1.
</emu-alg>
</emu-clause>

Expand All @@ -663,13 +677,15 @@ <h1>Iterator.prototype.find ( _fn_ )</h1>
<emu-alg>
1. Let _iterated_ be ? GetIteratorDirect(*this* value).
1. If IsCallable(_fn_) is *false*, throw a *TypeError* exception.
1. Let _counter_ be 0.
1. Repeat,
1. Let _next_ be ? IteratorStep(_iterated_).
1. If _next_ is *false*, return *undefined*.
1. Let _value_ be ? IteratorValue(_next_).
1. Let _result_ be Completion(Call(_fn_, *undefined*, &laquo; _value_ &raquo;)).
1. Let _result_ be Completion(Call(_fn_, *undefined*, &laquo; _value_, 𝔽(_counter_) &raquo;)).
1. IfAbruptCloseIterator(_result_, _iterated_).
1. If ToBoolean(_result_) is *true*, return ? IteratorClose(_iterated_, NormalCompletion(_value_)).
1. Set _counter_ to _counter_ + 1.
</emu-alg>
</emu-clause>

Expand Down Expand Up @@ -700,16 +716,18 @@ <h1>AsyncIterator.prototype.map ( _mapper_ )</h1>
1. Let _iterated_ be ? GetIteratorDirect(*this* value).
1. If IsCallable(_mapper_) is *false*, throw a *TypeError* exception.
1. Let _closure_ be a new Abstract Closure with no parameters that captures _iterated_ and _mapper_ and performs the following steps when called:
1. Let _counter_ be 0.
1. Repeat,
1. Let _next_ be ? Await(? IteratorNext(_iterated_)).
1. If ? IteratorComplete(_next_) is *true*, return *undefined*.
1. Let _value_ be ? IteratorValue(_next_).
1. Let _mapped_ be Completion(Call(_mapper_, *undefined*, &laquo; _value_ &raquo;)).
1. Let _mapped_ be Completion(Call(_mapper_, *undefined*, &laquo; _value_, 𝔽(_counter_) &raquo;)).
1. IfAbruptCloseAsyncIterator(_mapped_, _iterated_).
1. Set _mapped_ to Completion(Await(_mapped_)).
1. IfAbruptCloseAsyncIterator(_mapped_, _iterated_).
1. Let _completion_ be Completion(Yield(_mapped_)).
1. IfAbruptCloseAsyncIterator(_completion_, _iterated_).
1. Set _counter_ to _counter_ + 1.
1. Return CreateAsyncIteratorFromClosure(_closure_, ~Async Iterator Helper~, %AsyncIteratorHelperPrototype%).
</emu-alg>
</emu-clause>
Expand All @@ -720,17 +738,19 @@ <h1>AsyncIterator.prototype.filter ( _filterer_ )</h1>
1. Let _iterated_ be ? GetIteratorDirect(*this* value).
1. If IsCallable(_filterer_) is *false*, throw a *TypeError* exception.
1. Let _closure_ be a new Abstract Closure with no parameters that captures _iterated_ and _filterer_ and performs the following steps when called:
1. Let _counter_ be 0.
1. Repeat,
1. Let _next_ be ? Await(? IteratorNext(_iterated_)).
1. If ? IteratorComplete(_next_) is *true*, return *undefined*.
1. Let _value_ be ? IteratorValue(_next_).
1. Let _selected_ be Completion(Call(_filterer_, *undefined*, &laquo; _value_ &raquo;)).
1. Let _selected_ be Completion(Call(_filterer_, *undefined*, &laquo; _value_, 𝔽(_counter_) &raquo;)).
1. IfAbruptCloseAsyncIterator(_selected_, _iterated_).
1. Set _selected_ to Completion(Await(_selected_)).
1. IfAbruptCloseAsyncIterator(_selected_, _iterated_).
1. If ToBoolean(_selected_) is *true*, then
1. Let _completion_ be Completion(Yield(_value_)).
1. IfAbruptCloseAsyncIterator(_completion_, _iterated_).
1. Set _counter_ to _counter_ + 1.
1. Return CreateAsyncIteratorFromClosure(_closure_, ~Async Iterator Helper~, %AsyncIteratorHelperPrototype%).
</emu-alg>
</emu-clause>
Expand Down Expand Up @@ -793,9 +813,9 @@ <h1>AsyncIterator.prototype.indexed ( )</h1>
1. If ? IteratorComplete(_next_) is *true*, return *undefined*.
1. Let _value_ be ? IteratorValue(_next_).
1. Let _pair_ be CreateArrayFromList(&laquo; _index_, _value_ &raquo;).
1. Set _index_ to _index_ + 1.
1. Let _completion_ be Completion(Yield(_pair_)).
1. IfAbruptCloseAsyncIterator(_completion_, _iterated_).
1. Set _index_ to _index_ + 1.
1. Return CreateAsyncIteratorFromClosure(_closure_, ~Async Iterator Helper~, %AsyncIteratorHelperPrototype%).
</emu-alg>
</emu-clause>
Expand All @@ -807,11 +827,12 @@ <h1>AsyncIterator.prototype.flatMap ( _mapper_ )</h1>
1. Let _iterated_ be ? GetIteratorDirect(*this* value).
1. If IsCallable(_mapper_) is *false*, throw a *TypeError* exception.
1. Let _closure_ be a new Abstract Closure with no parameters that captures _iterated_ and _mapper_ and performs the following steps when called:
1. Let _counter_ be 0.
1. Repeat,
1. Let _next_ be ? Await(? IteratorNext(_iterated_)).
1. If ? IteratorComplete(_next_) is *true*, return *undefined*.
1. Let _value_ be ? IteratorValue(_next_).
1. Let _mapped_ be Completion(Call(_mapper_, *undefined*, &laquo; _value_ &raquo;)).
1. Let _mapped_ be Completion(Call(_mapper_, *undefined*, &laquo; _value_, 𝔽(_counter_) &raquo;)).
1. IfAbruptCloseAsyncIterator(_mapped_, _iterated_).
1. Set _mapped_ to Completion(Await(_mapped_)).
1. IfAbruptCloseAsyncIterator(_mapped_, _iterated_).
Expand All @@ -838,6 +859,7 @@ <h1>AsyncIterator.prototype.flatMap ( _mapper_ )</h1>
1. Else if _completion_ is a throw completion, then
1. Assert: Awaiting _innerValue_ during the Yield on step <emu-xref href="#step-async-iterator-flatmap-yield"></emu-xref> threw.
1. Return ? IteratorClose(_completion_, _iterated_).
1. Set _counter_ to _counter_ + 1.
1. Return CreateAsyncIteratorFromClosure(_closure_, ~Async Iterator Helper~, %AsyncIteratorHelperPrototype%).
</emu-alg>
</emu-clause>
Expand All @@ -853,15 +875,17 @@ <h1>AsyncIterator.prototype.reduce ( _reducer_ [ , _initialValue_ ] )</h1>
1. Let _accumulator_ be ? IteratorValue(_next_).
1. Else,
1. Let _accumulator_ be _initialValue_.
1. Let _counter_ be 0.
1. Repeat,
1. Let _next_ be ? Await(? IteratorNext(_iterated_)).
1. If ? IteratorComplete(_next_) is *true*, return _accumulator_.
1. Let _value_ be ? IteratorValue(_next_).
1. Let _result_ be Completion(Call(_reducer_, *undefined*, &laquo; _accumulator_, _value_ &raquo;)).
1. Let _result_ be Completion(Call(_reducer_, *undefined*, &laquo; _accumulator_, _value_, 𝔽(_counter_) &raquo;)).
1. IfAbruptCloseAsyncIterator(_result_, _iterated_).
1. Set _result_ to Await(_result_).
1. IfAbruptCloseAsyncIterator(_result_, _iterated_).
1. Set _accumulator_ to _result_.
1. Set _counter_ to _counter_ + 1.
</emu-alg>
</emu-clause>

Expand All @@ -885,14 +909,16 @@ <h1>AsyncIterator.prototype.forEach ( _fn_ )</h1>
<emu-alg>
1. Let _iterated_ be ? GetIteratorDirect(*this* value).
1. If IsCallable(_fn_) is *false*, throw a *TypeError* exception.
1. Let _counter_ be 0.
1. Repeat,
1. Let _next_ be ? Await(? IteratorNext(_iterated_)).
1. If ? IteratorComplete(_next_) is *true*, return *undefined*.
1. Let _value_ be ? IteratorValue(_next_).
1. Let _r_ be Completion(Call(_fn_, *undefined*, &laquo; _value_ &raquo;)).
1. Let _r_ be Completion(Call(_fn_, *undefined*, &laquo; _value_, 𝔽(_counter_) &raquo;)).
1. IfAbruptCloseAsyncIterator(_r_, _iterated_).
1. Set _r_ to Await(r).
1. IfAbruptCloseAsyncIterator(_r_, _iterated_).
1. Set _counter_ to _counter_ + 1.
</emu-alg>
</emu-clause>

Expand All @@ -902,15 +928,17 @@ <h1>AsyncIterator.prototype.some ( _fn_ )</h1>
<emu-alg>
1. Let _iterated_ be ? GetIteratorDirect(*this* value).
1. If IsCallable(_fn_) is *false*, throw a *TypeError* exception.
1. Let _counter_ be 0.
1. Repeat,
1. Let _next_ be ? Await(? IteratorNext(_iterated_)).
1. If ? IteratorComplete(_next_) is *true*, return *false*.
1. Let _value_ be ? IteratorValue(_next_).
1. Let _result_ be Completion(Call(_fn_, *undefined*, &laquo; _value_ &raquo;)).
1. Let _result_ be Completion(Call(_fn_, *undefined*, &laquo; _value_, 𝔽(_counter_) &raquo;)).
1. IfAbruptCloseAsyncIterator(_result_, _iterated_).
1. Set _result_ to Await(_result_).
1. IfAbruptCloseAsyncIterator(_result_, _iterated_).
1. If ToBoolean(_result_) is *true*, return ? AsyncIteratorClose(_iterated_, NormalCompletion(*true*)).
1. Set _counter_ to _counter_ + 1.
</emu-alg>
</emu-clause>

Expand All @@ -920,15 +948,17 @@ <h1>AsyncIterator.prototype.every ( _fn_ )</h1>
<emu-alg>
1. Let _iterated_ be ? GetIteratorDirect(*this* value).
1. If IsCallable(_fn_) is *false*, throw a *TypeError* exception.
1. Let _counter_ be 0.
1. Repeat,
1. Let _next_ be ? Await(? IteratorNext(_iterated_)).
1. If ? IteratorComplete(_next_) is *true*, return *true*.
1. Let _value_ be ? IteratorValue(_next_).
1. Let _result_ be Completion(Call(_fn_, *undefined*, &laquo; _value_ &raquo;)).
1. Let _result_ be Completion(Call(_fn_, *undefined*, &laquo; _value_, 𝔽(_counter_) &raquo;)).
1. IfAbruptCloseAsyncIterator(_result_, _iterated_).
1. Set _result_ to Await(_result_).
1. IfAbruptCloseAsyncIterator(_result_, _iterated_).
1. If ToBoolean(_result_) is *false*, return ? AsyncIteratorClose(_iterated_, NormalCompletion(*false*)).
1. Set _counter_ to _counter_ + 1.
</emu-alg>
</emu-clause>

Expand All @@ -938,15 +968,17 @@ <h1>AsyncIterator.prototype.find ( _fn_ )</h1>
<emu-alg>
1. Let _iterated_ be ? GetIteratorDirect(*this* value).
1. If IsCallable(_fn_) is *false*, throw a *TypeError* exception.
1. Let _counter_ be 0.
1. Repeat,
1. Let _next_ be ? Await(? IteratorNext(_iterated_)).
1. If ? IteratorComplete(_next_) is *true*, return *undefined*.
1. Let _value_ be ? IteratorValue(_next_).
1. Let _result_ be Completion(Call(_fn_, *undefined*, &laquo; _value_ &raquo;)).
1. Let _result_ be Completion(Call(_fn_, *undefined*, &laquo; _value_, 𝔽(_counter_) &raquo;)).
1. IfAbruptCloseAsyncIterator(_result_, _iterated_).
1. Set _result_ to Await(_result_).
1. IfAbruptCloseAsyncIterator(_result_, _iterated_).
1. If ToBoolean(_result_) is *true*, return ? AsyncIteratorClose(_iterated_, NormalCompletion(_value_)).
1. Set _counter_ to _counter_ + 1.
</emu-alg>
</emu-clause>

Expand Down

0 comments on commit 7b5e897

Please sign in to comment.