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 / iterative_mergesort.js
Created July 19, 2024 19:41
iterative mergesort
function mergeSort(array, start, end) {
if (array.length <= 1) return array;
let slices = []
let nextSlices = []
let nextSlicesTemp = []
let unit = 2 // then 4 then 8 and so on
const mid = Math.floor((start + end) / 2);
if(mid + 1 <= end) {
@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 / 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])
}
@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 / 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";
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 / 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;
@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 / index_tuple.ts
Last active April 20, 2022 17:29
Valid numerical index tuple
type IndexesKeysOfTuple<T extends any[]> = Exclude<
keyof T & string,
keyof any[]
>;
type NumberToString<N extends number | bigint> = `${N}`;
type ValidIndex<T extends any[], I extends number> = any[] extends T
? number // T is an array but not a tuple type
: NumberToString<I> extends IndexesKeysOfTuple<T>
/*
Intro:
Filtering requirements have grown. We need to be
able to filter any kind of Persons.
Exercise:
Fix typing for the filterPersons so that it can filter users