Skip to content

Instantly share code, notes, and snippets.

@tail-call
Created December 19, 2022 06:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tail-call/e7a348844f7a3015e3c3baaa55804134 to your computer and use it in GitHub Desktop.
Save tail-call/e7a348844f7a3015e3c3baaa55804134 to your computer and use it in GitHub Desktop.
function* iota(n: number) {
for (let i = 0; i < n; i++) yield i;
return 8;
}
function* map<T, U>(generator: Generator<T>, mapper: (value: T) => U) {
for (const value of generator) {
yield mapper(value);
}
}
function* pack<T>(generator: Generator<T>, count: number) {
let box: T[] = [];
for (const value of generator) {
if (box.length === count) {
yield box;
box = [];
}
box.push(value);
}
if (box.length) yield box;
}
type Sum<N extends number, M extends number> = number;
function sum<N extends number, M extends number>(n: N, m: M): Sum<N, M> {
return n + m;
}
class FixedArray<T extends number, V> {
len: T;
items: V[];
constructor(len: T, initalValue: V) {
this.len = len;
this.items = new Array(len).fill(initalValue);
}
joined<N extends number>(otherArray: FixedArray<N, V>, initalValue: V): FixedArray<Sum<N, T>, V> {
return new FixedArray(sum(this.len, otherArray.len), initalValue);
}
}
new FixedArray(8, 0)
function vecLen(vec: FixedArray<2, number>): number {
return (vec.items[0] ** 2 + vec.items[1] ** 2);
}
console.log([
...pack(pack(map(iota(100), x => x * 2), 3), 3)
]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment