Skip to content

Instantly share code, notes, and snippets.

View jfet97's full-sized avatar
🖤
Loving programming

Andrea Simone Costa jfet97

🖤
Loving programming
View GitHub Profile
@jfet97
jfet97 / justifiedMap.ts
Last active November 4, 2023 18:11
Once you prove that a key is present, you can use it `undefined`-free in any number of other operations.
// 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
@jfet97
jfet97 / DFS.js
Last active August 26, 2023 10:02
Simple Depth First Search in JavaScript
function isObject(entity) {
return typeof entity === "object" && entity !== null;
}
function getAdjacentNodes(obj) {
return (
Object.entries(obj)
.filter(([, v]) => isObject(v))
)
}
@jfet97
jfet97 / asyncAwaiterQueue.js
Last active November 9, 2022 11:19
A simple implementation of a promise queue that is temporal independent
class asyncAwaiterQueue {
constructor(...values) {
this.promise = new Promise(resolve => {
this.resolve = resolve;
});
values.forEach(v => this.put(v));
}
@jfet97
jfet97 / flattens.js
Created August 3, 2022 20:38
Recursive, tail-recursive and iterative versions of flatten
{
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])
}
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!)
//
@jfet97
jfet97 / invert.js
Last active May 27, 2022 15:57
tail-call recursive binary tree inverter
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);
@jfet97
jfet97 / entries.ts
Last active May 15, 2022 21:36
Retrieve key-value union entries from on object
type Entries<T> = {
[P in keyof T]: { key: P; value: T[P] };
}[keyof T];
// -----------------------------------
interface Person {
name: string;
age: number;
}
@jfet97
jfet97 / string_types.ts
Last active May 10, 2022 20:36
types for string long exact/at least/at most n chars
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";
@jfet97
jfet97 / stringa_lunga_almeno_5.ts
Last active May 10, 2022 14:22
Il tipo di una stringa lunga almeno 5
// 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"
@jfet97
jfet97 / merge2.ts
Last active May 8, 2022 20:38
merge union into object mantaining keys union and modifiers
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;