You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
classIteratorTest{// This is just a simple replacement for the data structure that needs to be traversed.// It may actually be a tree or other data structure implemented by a custom traversal.nums: number[]=[1,2,3,4,5];public[Symbol.iterator](): Iterator<number>{letidx=0;return{next: (): IteratorResult<number>=>{if(idx<this.nums.length){return{value: this.nums[idx++],done: false,};}elsereturn{value: undefined,done: true,};},};}}
Enumerable returns an empty array when I try to use it like this:
// in linq.d.ts export function from<T>(obj: Iterator<T>): IEnumerable<T>;// so I called:Enumerable.from(et[Symbol.iterator]()).toArray()
The reason is that during the construction of Enumerable.from():
// linq.jsif(typeofobj!=Types.Function){// array or array-like objectif(typeofobj.length==Types.Number){returnnewArrayEnumerable(obj);}// iterable objectif(typeofSymbol!=='undefined'&&typeofobj[Symbol.iterator]!=='undefined'){// use obj as who has [Symbol.iterator]returnnewEnumerable(function(){returnnewIEnumerator(Functions.Blank,function(){varnext=obj.next();// use obj as iteratorreturn(next.done ? false : (this.yieldReturn(next.value)));},Functions.Blank);});}}
I'm not a typescript expert, but I think this code is dealing with Iterator. obj itself is an Iterator, but here we still try to access obj.[Symbol.iterator].
Then it tries to access obj.next() in the iteration, which is no problem because obj as an Iterator contains a next function.
So I had to change my code to this to get it to work properly:
classETest{nums: number[]=[1,2,3,4,5];public[Symbol.iterator](): Iterator<number>{letidx=0;return{next: (): IteratorResult<number>=>{if(idx<this.nums.length){return{value: this.nums[idx++],done: false,};}elsereturn{value: undefined,done: true,};},// A useless function just to pass the `typeof obj[Symbol.iterator] !== 'undefined'` predicate[Symbol.iterator](): Iterator<number>{return{next(...args): IteratorResult<number,any>{returnundefined;},};},};}}
Is there anything wrong with my usage? In my opinion, the former should be the correct usage of from<T>(obj: Iterator<T>): IEnumerable<T>
The text was updated successfully, but these errors were encountered:
I have a type that implements [Symbol.iterator]:
Enumerable returns an empty array when I try to use it like this:
The reason is that during the construction of Enumerable.from():
I'm not a typescript expert, but I think this code is dealing with Iterator.
obj
itself is an Iterator, but here we still try to accessobj.[Symbol.iterator]
.Then it tries to access
obj.next()
in the iteration, which is no problem becauseobj
as an Iterator contains anext
function.So I had to change my code to this to get it to work properly:
Is there anything wrong with my usage? In my opinion, the former should be the correct usage of
from<T>(obj: Iterator<T>): IEnumerable<T>
The text was updated successfully, but these errors were encountered: