This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://hackage.haskell.org/package/justified-containers-0.3.0.0/docs/src/Data-Map-Justified.html | |
// TODO: not all operations have been translated into TS | |
// TODO: is it possible to avoid continuations? Don't think so, TS doesn't support existentials | |
export type JMap<PH, K, V> = { readonly carrier: ReadonlyMap<K, V>, readonly proof: PH, readonly brand: unique symbol } | |
export type JKey<PH, K> = { readonly carrier: K, readonly proof: PH, readonly brand: unique symbol } | |
// internals |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function isObject(entity) { | |
return typeof entity === "object" && entity !== null; | |
} | |
function getAdjacentNodes(obj) { | |
return ( | |
Object.entries(obj) | |
.filter(([, v]) => isObject(v)) | |
) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class asyncAwaiterQueue { | |
constructor(...values) { | |
this.promise = new Promise(resolve => { | |
this.resolve = resolve; | |
}); | |
values.forEach(v => this.put(v)); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
function flatten_it(a) { | |
let res = [...a] | |
while (res.some(el => Array.isArray(el))) { | |
for (let i = 0; i < res.length; i++) { | |
if (Array.isArray(res[i])) { | |
res.splice(i, 1, ...res[i]) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export type Extends<A, B, Y = unknown, N = never> = | |
[A] extends [never] // <- ottimizzo evitando la distribution, altrimenti nello step dopo farei tot A extends B identici | |
? N | |
: A extends B | |
? Y | |
: N | |
// type IsNever<A> = A extends never ? true : false risulta in never se A = never | |
// (entra in gioco la distribution, ma non c'è nulla da distribuire!) | |
// |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function invert(tree, toInvR = [], path = [], res = null) { | |
// tree == nodo corrente | |
// path[path.length - 1] == nodo padre | |
if (!tree) { | |
return null; | |
} | |
const parent = path[path.length - 1]; // is undefined iif res === null | |
const newNode = Node(tree.value); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Entries<T> = { | |
[P in keyof T]: { key: P; value: T[P] }; | |
}[keyof T]; | |
// ----------------------------------- | |
interface Person { | |
name: string; | |
age: number; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Equal<X, Y> = (<T>() => T extends X ? 1 : 2) extends <T>() => T extends Y | |
? 1 | |
: 2 | |
? true | |
: false; | |
interface StringCostrainType { | |
readonly _N: number; | |
readonly _E: unknown; | |
readonly _type: "exact" | "atleast" | "atmost"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 6 any perché l'ultimo "viene inferito" come la stringa vuota "", gli altri 5 acchiappano un carattere ciascuno | |
type String5<T extends string> = T extends `${any}${any}${any}${any}${any}${any}` ? T : "The string must be at least 5 characters long" // oppure never | |
type testmeno1 = String5<string> // nope | |
type test0 = String5<""> // nope | |
type test1 = String5<"a"> // nope | |
type test2 = String5<"ab"> // nope | |
type test3 = String5<"abc"> // nope | |
type test4 = String5<"abcd"> // nope | |
type test5 = String5<"abcde"> // "abcde" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type AllKeys<T> = T extends unknown ? keyof T : never; | |
type Lookup<T, K extends PropertyKey> = T extends any ? T[K & keyof T] : never; | |
type MergeAsUnion<T> = { [K in AllKeys<T>]: Lookup<T, K> }; | |
type IfEquals<T, U, Y = unknown, N = never> = (<V>() => V extends T | |
? 1 | |
: 2) extends <V>() => V extends U ? 1 : 2 | |
? Y | |
: N; |
NewerOlder