TypeScript Generic Collection: List -


i trying learn typescript, , need advice on implementing generic collection types. put dictionary , hashset in question, here i'd advice on list type.

especially foreach-operation looks bit strange. think found in question here, , "improved" returning true or false give feedback if iteration stopped or completed.

import { iforeachfunction } "./iforeachfunction"  export class list<t> {     private _items: array<t>;      public constructor() {         this._items = [];     }      public count(): number {         return this._items.length;     }      public item(index: number): t {         return this._items[index];     }      public add(value: t): void {         this._items.push(value);     }      public removeat(index: number): void {         this._items.splice(index, 1);     }      public remove(value: t): void {         let index = this._items.indexof(value);         this.removeat(index);     }      public foreach(callback: iforeachfunction<t>): boolean {         (const element of this._items) {             if (callback(element) === false) {                 return false;             }         }          return true;     } } 

the foreach-iteration relies on interface file:

export interface iforeachfunction<t> {     (callback: t): boolean | void; } 

you use list , foreach-method this:

let mylist: list<a_type> = new list<a_type>(); let completed: boolean = mylist.foreach(xyz => {     // xyz     return false; // aborts iteration     return true; // continues next element }); if (completed) // can see happened "during" iteration 

i think not bad, i'd appreciate input. not sure if use === correctly. question know: how define function interface iforeachfunction? not "re-use" interface visibly, declare anonymous method shown above. if wanted call method having interface definition, possible?

thanks! ralf

one problem see have interface instance:

callback: iforeachfunction<t> 

this contains method called

callback() 

but call callback once. have call callback() method inside interface:

callback.callback() 

also code looks inspired c# or java. in typescript use array. simplifies code constructs.


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -