-
-
Notifications
You must be signed in to change notification settings - Fork 160
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Updating list-ops test generic types parameters #1477
Changes from all commits
448aadb
c041be5
3f54610
8890e58
46b9678
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
const Null: Cons = { | ||
const Null: Cons<undefined> = { | ||
get value() { | ||
return undefined | ||
}, | ||
|
@@ -10,17 +10,17 @@ const Null: Cons = { | |
return this.value | ||
}, | ||
|
||
push(item): Cons { | ||
push<T>(item: T): Cons<T> { | ||
// eslint-disable-next-line @typescript-eslint/no-use-before-define | ||
return new Cons(item, this) | ||
return new Cons(item, this) as Cons<T> | ||
}, | ||
length() { | ||
return 0 | ||
}, | ||
append(other: Cons): Cons { | ||
append<T>(other: Cons<T>): Cons<T> { | ||
return other | ||
}, | ||
concat(): Cons { | ||
concat(): Cons<undefined> { | ||
return this | ||
}, | ||
forEach(): void { | ||
|
@@ -38,27 +38,27 @@ const Null: Cons = { | |
): TReturn { | ||
return initial as TReturn | ||
}, | ||
filter(): Cons { | ||
filter(): Cons<undefined> { | ||
return Null | ||
}, | ||
reverse(): Cons { | ||
reverse(): Cons<undefined> { | ||
return this | ||
}, | ||
map(): Cons { | ||
return this | ||
map<TReturn>(): Cons<TReturn> { | ||
return this as Cons<TReturn> | ||
}, | ||
} | ||
class Cons { | ||
class Cons<T> { | ||
constructor( | ||
public readonly value: unknown, | ||
public next: Cons = Null | ||
public readonly value: T, | ||
public next: Cons<T> = Null as Cons<T> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's probably better to define it as: public next: Cons<T> | Cons<undefined> instead of needing the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can make the code a little bit messier with the types, and the only method where this is important is |
||
) {} | ||
|
||
public get(i: number): unknown { | ||
public get(i: number): T | undefined { | ||
return i === 0 ? this.value : this.next.get(i - 1) | ||
} | ||
|
||
public push(item: unknown): this { | ||
public push(item: T): this { | ||
this.next = this.next.push(item) | ||
return this | ||
} | ||
|
@@ -67,83 +67,78 @@ class Cons { | |
return 1 + this.next.length() | ||
} | ||
|
||
public append(other: Cons): Cons { | ||
public append(other: Cons<T>): Cons<T> { | ||
return other.foldl((result, item) => result.push(item), this) | ||
} | ||
|
||
public concat(others: Cons): Cons { | ||
return others.foldl<Cons, Cons>( | ||
(result, other) => result.append(other), | ||
this | ||
) | ||
public concat(others: Cons<Cons<T>>): Cons<T> { | ||
return others.foldl<Cons<T>>((result, other) => result.append(other), this) | ||
} | ||
|
||
public foldl<TValue = unknown>( | ||
callback: (initial: TValue, value: TValue) => TValue | ||
): TValue | ||
public foldl<TValue = unknown, TReturn = unknown>( | ||
callback: (initial: TReturn, value: TValue) => TReturn, | ||
public foldl<TReturn = unknown>( | ||
callback: (initial: TReturn, value: T) => TReturn | ||
): TReturn | ||
public foldl<TReturn = unknown>( | ||
callback: (initial: TReturn, value: T) => TReturn, | ||
initial: TReturn | ||
): TReturn | ||
|
||
public foldl<TValue = unknown, TReturn = unknown>( | ||
callback: (initial: TReturn | undefined, value: TValue) => TReturn, | ||
public foldl<TReturn = unknown>( | ||
callback: (initial: TReturn | undefined, value: T) => TReturn, | ||
initial?: TReturn | ||
): TReturn { | ||
return this.next.foldl<TValue, TReturn>( | ||
callback, | ||
callback(initial, this.value as TValue) | ||
) | ||
return this.next.foldl<TReturn>(callback, callback(initial, this.value)) | ||
} | ||
|
||
public forEach(callback: (value: unknown) => void): void { | ||
public forEach(callback: (value: T) => void): void { | ||
this.foldl((_, item) => callback(item)) | ||
} | ||
|
||
public foldr<TValue = unknown>( | ||
callback: (initial: TValue, value: TValue) => TValue | ||
): TValue | ||
public foldr<TValue = unknown, TReturn = unknown>( | ||
callback: (initial: TReturn, value: TValue) => TReturn, | ||
public foldr<TReturn = unknown>( | ||
callback: (initial: TReturn, value: T) => TReturn | ||
): TReturn | ||
public foldr<TReturn = unknown>( | ||
callback: (initial: TReturn, value: T) => TReturn, | ||
initial: TReturn | ||
): TReturn | ||
|
||
public foldr<TValue = unknown, TReturn = unknown>( | ||
callback: (initial: TReturn, value: TValue) => TReturn, | ||
public foldr<TReturn = unknown>( | ||
callback: (initial: TReturn, value: T) => TReturn, | ||
initial?: TReturn | ||
): TReturn { | ||
return callback( | ||
this.next.foldr<TValue, TReturn>(callback, initial as TReturn), | ||
this.value as TValue | ||
this.next.foldr<TReturn>( | ||
callback as (initial: TReturn, value: T | undefined) => TReturn, | ||
initial as TReturn | ||
), | ||
this.value | ||
) | ||
} | ||
|
||
public filter<TValue = unknown>(predicate: (value: TValue) => boolean): Cons { | ||
return this.foldl<TValue, Cons>( | ||
public filter(predicate: (value: T) => boolean): Cons<T> { | ||
return this.foldl<Cons<T>>( | ||
(result, item) => (predicate(item) && result.push(item)) || result, | ||
Null | ||
Null as Cons<T> | ||
) | ||
} | ||
|
||
public map<TValue = unknown, TReturn = unknown>( | ||
expression: (value: TValue) => TReturn | ||
): Cons { | ||
return this.foldl<TValue, Cons>( | ||
public map<TReturn = unknown>( | ||
expression: (value: T) => TReturn | ||
): Cons<TReturn> { | ||
return this.foldl( | ||
(result, item) => result.push(expression(item)), | ||
Null | ||
Null as Cons<TReturn> | ||
) | ||
} | ||
|
||
public reverse(): Cons { | ||
public reverse(): Cons<T> { | ||
return this.next.reverse().push(this.value) | ||
} | ||
} | ||
export class List { | ||
public static create(...values: unknown[]): Cons { | ||
public static create<T>(...values: T[]): Cons<T> { | ||
const [head, ...tail] = values | ||
|
||
if (head === undefined) { | ||
return Null | ||
return Null as Cons<T> | ||
} | ||
|
||
return new Cons(head, List.create(...tail)) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this necessary? Doesn't
new Cons(item, this)
already inferConst<T>
? If not, shouldn't it benew Cons<T>(item, this)
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this case
Cons
would be of typeCons<T | undefined>
, however because of howNull
works theundefined
part can be omitted, and since is of typeCons<T | undefined>
usingnew Cons<T>(item, this)
throws an error on TypeScript.