TypeScript Generic Collection:List

时间:2017-07-25 22:38:03

标签: list typescript collections

我正在尝试学习TypeScript,并且需要一些关于实现泛型集合类型的建议。我把字典和HashSet放在另一个问题中,在这里我喜欢我的列表类型的任何建议。

特别是ForEach-Operation看起来有点奇怪。我想我在这里找到了另一个问题,而且#34;改进了#34;通过返回true或false来提供反馈,如果迭代提前停止或已完成。

import { IForEachFunction } from "./IForEachFunction"

export class List<T> {
    private _items: Array<T>;

    public constructor() {
        this._items = [];
    }

    public get 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 {
        for (const element of this._items) {
            if (callback(element) === false) {
                return false;
            }
        }

        return true;
    }
}

ForEach-Iteration依赖于另一个文件的接口:

export interface IForEachFunction<T> {
    (callback: T): boolean | void;
}

您可以像我这样使用我的列表和ForEach-Method:

let myList: List<a_type> = new List<a_type>();
let completed: boolean = myList.ForEach(xyz => {
    // do something with xyz
    return false; // aborts the iteration
    return true; // continues with the next element
});
if (completed) // we can see what happened "during" the iteration

我认为这不错,但我很欣赏任何意见。我不确定我是否正确使用===。 另一个我真正想知道的问题:如何使用IForEachFunction接口定义函数?我真的没有&#34;重复使用&#34;该界面明显,我总是声明一个匿名方法,如上所示。如果我想调用具有接口定义的方法,那可能吗?

谢谢! 拉尔夫

2 个答案:

答案 0 :(得分:0)

我看到的一个问题是你有一个接口实例:

callback: IForEachFunction<T>

这包含一个名为

的方法
callback()

但是你只召唤一次回调。你可以在你的界面中调用callback()方法:

callback.callback()

此外,您的代码看起来受C#或Java的启发。在TypeScript中,您通常只使用数组。这简化了某些代码构造。

答案 1 :(得分:0)

现在,有一个库可以在打字稿中提供强类型的可查询集合

该库称为 ts-generic-collections

GitHub上的源代码:

https://github.com/VeritasSoftware/ts-generic-collections

使用此库,您可以创建集合(如List<T>)并按如下所示查询它们。

    let owners = new List<Owner>();

    let owner = new Owner();
    owner.id = 1;
    owner.name = "John Doe";
    owners.add(owner);

    owner = new Owner();
    owner.id = 2;
    owner.name = "Jane Doe";
    owners.add(owner);    

    let pets = new List<Pet>();

    let pet = new Pet();
    pet.ownerId = 2;
    pet.name = "Sam";
    pet.sex = Sex.M;

    pets.add(pet);

    pet = new Pet();
    pet.ownerId = 1;
    pet.name = "Jenny";
    pet.sex = Sex.F;

    pets.add(pet);

    //query to get owners by the sex/gender of their pets
    let ownersByPetSex = owners.join(pets, owner => owner.id, pet => pet.ownerId, (x, y) => new OwnerPet(x,y))
                               .groupBy(x => [x.pet.sex])
                               .select(x =>  new OwnersByPetSex(x.groups[0], x.list.select(x => x.owner)));

    expect(ownersByPetSex.toArray().length === 2).toBeTruthy();

    expect(ownersByPetSex.toArray()[0].sex == Sex.F).toBeTruthy();
    expect(ownersByPetSex.toArray()[0].owners.length === 1).toBeTruthy();
    expect(ownersByPetSex.toArray()[0].owners.toArray()[0].name == "John Doe").toBeTruthy();

    expect(ownersByPetSex.toArray()[1].sex == Sex.M).toBeTruthy();
    expect(ownersByPetSex.toArray()[1].owners.length == 1).toBeTruthy();
    expect(ownersByPetSex.toArray()[1].owners.toArray()[0].name == "Jane Doe").toBeTruthy();