Как инкапсулировать метод соединения или сглаживания в связанном списке? - PullRequest
3 голосов
/ 16 апреля 2019

У меня есть базовая сборка связанного списка в машинописи с дискриминационным объединением.

type ListType<T> = {
Kind: "Cons",
Head: T,
Tail: List<T>
} | {
 Kind: "Empty"
}

type ListOperations<T> = {
 reduce: <U>(this: List<T>, f: (state: U, x: T) => U, accumulator: U) => U
 map: <U>(this: List<T>, f: (_: T) => U) => List<U>
 reverse: (this: List<T>) => List<T>
 concat: (this: List<T>, l: List<T>) => List<T>
 toArray: (this: List<T>) => T[]
 join: (this: List<List<T>>) => List<T>
}

type List<T> = ListType<T> & ListOperations<T>

У меня также есть несколько конструкторов как для пустых, так и для минусов:

export const Cons = <T>(head: T, tail: List<T>): List<T> => ({
 Kind: "Cons",
 Head: head,
 Tail: tail,
 ...ListOperations()
})

export const Empty = <T>(): List<T> => ({
   Kind: "Empty",
   ...ListOperations()
})

И, наконец, у меня есть реализация различных методов:

const ListOperations = <T>(): ListOperations<T> => ({
reduce: function <U>(this: List<T>, f: (state: U, x: T) => U, accumulator: U): U {
    return this.Kind == "Empty" ? accumulator : this.Tail.reduce(f, f(accumulator, this.Head))
},
map: function <U>(this: List<T>, f: (_: T) => U): List<U> {
    return this.reduce((s, x) => Cons(f(x), s), Empty())
},
reverse: function (this: List<T>): List<T> {
    return this.reduce((s, x) => Cons(x, s), Empty())
},
concat: function (this: List<T>, l: List<T>): List<T> {
    return this.reverse().reduce((s, x) => Cons(x, s), l)
},
toArray: function (this: List<T>): T[] {
    return this.reduce<T[]>((s, x) => s.concat([x]), [])
},
join: function (this: List<List<T>>): List<T> {
    return this.reduce((s, x) => s.concat(x), Empty())
}

})

Все работает нормально, но я получаю ошибку компиляции при попытке запустить следующее:

let x = Cons(1, Cons(2, Cons(3, Cons(4, Empty()))))
let y = x.map(x => x + 4)

let z = Cons(x, Cons(y, Empty()))
z.join()

Контекст 'this' типа List<List<number>> не может быть назначен метод 'this' типа List<List<List<number>>>.

Это из-за метода join (или flatten, как некоторые из вас могут его назвать). Когда я пишу объединение вне типа List, оно работает, поэтому мой вопрос: Есть ли способ явно указать компилятору, что this должен иметь тип List<List<T>>?

Я уже пытался использовать extends

join: function <T1 extends List<T>>(this: List<T1>): List<T>

1 Ответ

2 голосов
/ 17 апреля 2019

Это потому, что ваш список List<T>, тогда как T сам по себе List<T>. Правильный ввод будет:

 join(this: List<T>): T {

Чтобы затем убедиться, что T является самим списком, используйте условный тип:

 join(this: T extends List<*> ? List<T> : "Only nested lists can be joined!"): T
...