Skip to content

Instantly share code, notes, and snippets.

View gabrielelana's full-sized avatar
🎯
Focusing

Gabriele Lana gabrielelana

🎯
Focusing
View GitHub Profile
@gabrielelana
gabrielelana / CombinationsOf.ts
Created August 8, 2023 10:37
Compute all ordered combinations of a list of values at compile type in TypeScript
type Tuple<X, Y> = [X, Y]
type Equal<X, Y> =
(<T>() => T extends X ? 1 : 2) extends
(<T>() => T extends Y ? 1 : 2) ? Tuple<true, Y> : Tuple<false, Y>
type Assert<T extends Tuple<true, any>> = T[1];
type AppendToAll<X, A extends Array<Array<any>>, R extends Array<any> = []> =
A extends []
? R
: A extends [infer H extends Array<any>, ...infer T extends Array<Array<any>>]
@gabrielelana
gabrielelana / entries.ts
Created May 25, 2023 16:23
Type safe and preserving version of `Object.entries` and `Object.fromEntries`
type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (
k: infer I
) => void
? I
: never;
type UnionToOvlds<U> = UnionToIntersection<
U extends any ? (f: U) => void : never
>;
@gabrielelana
gabrielelana / combineN.ts
Created January 31, 2023 16:02
combineN TypeScript
const combineN = <T,>(n: number, xs: T[]): T[][] => {
if (n > xs.length) return [];
if (n == 1) return xs.map(x => [x]);
const [h, ...t] = xs;
return [
...combineN(n - 1, t).map(ts => [h, ...ts]),
...combineN(n, t)
];
}
@gabrielelana
gabrielelana / oss-faq.md
Last active December 5, 2023 17:00
Open Source Saturday - FAQ

FAQ

  • Lavoreremo su un'unico progetto? - Assolutamente no, il numero e il tipo di progetti non sono decisi da nessuno a priori.
  • Come si decidono i progetti su cui si lavorerà nel corso della giornata? - All'inizio della giornata chi vuole ha a disposizione 3 minuti di tempo per presentare una propria idea/progetto sul quale lavorare durante la giornata. Alla fine delle presentazioni si fanno i gruppi, ognuno può decidere di aggregarsi ad un progetto presentato oppure lavorare singolarmente su un proprio progetto, non è obbligatorio lavorare in coppia/gruppo anche se farlo è decisamente più divertente che farlo da soli.
  • Chi propone di lavorare su un progetto deve essere un'esperto della materia? - Non obbligatoriamente, è lecito chiedere aiuto per una cosa che ci piacerebbe realizzare ma per la quale non abbiamo le competenze necessarie, vi chiedo solo di esplicitarlo quando fate la proposta in modo che le persone ne siano consapevoli.
  • Dobbiamo portare il nostro portatile? - Noi no
@gabrielelana
gabrielelana / routeParameters.ts
Created August 3, 2022 15:06
Typescript utility type to parse route parameters
type Tuple<X, Y> = [X, Y]
type Equal<X, Y> =
(<T>() => T extends X ? 1 : 2) extends
(<T>() => T extends Y ? 1 : 2) ? Tuple<true, Y> : Tuple<false, Y>
type Assert<T extends Tuple<true, any>> = T[1];//
// P = collecting param names
// N = current param name
// I = true if we are collecting a name
type RouteParameters<R extends string, P extends string = never, N extends string = "", I extends boolean = false> =
@gabrielelana
gabrielelana / inferredTypePlusCheckType.ts
Created January 14, 2022 18:21
Keep the inferred type but check if it's compatible with some structure
type CompanyName = "dallbogg" | "wakam"
type ActionNames = ["Quote", "Save", "Emit"]
type ActionName = ActionNames[number]
type Tuple<X, Y> = [X, Y];
type CompatibleWith<X, Y> = X extends Y ? Tuple<X, true> : Tuple<X, false>;
type Equals<X, Y> = [X] extends [Y] ? [Y] extends [X] ? Tuple<Y, true> : Tuple<Y, false> : Tuple<Y, false>
@gabrielelana
gabrielelana / unionTypeCombinations.ts
Last active January 14, 2022 18:21
From Union Type to Union Type of all Combinations without Repetition in TypeScript
type Tuple<X, Y> = [X, Y];
type CompatibleWith<X, Y> = X extends Y ? Tuple<X, true> : Tuple<X, false>;
type Equals<X, Y> = [X] extends [Y] ? [Y] extends [X] ? Tuple<Y, true> : Tuple<Y, false> : Tuple<Y, false>
type Assert<X extends Tuple<any, true>> = X extends Tuple<infer Y, true> ? Y : never;
const assertUnreacheable = (x: never): never => { throw new Error() };
@gabrielelana
gabrielelana / ParserCombinators.hs
Last active March 21, 2021 18:20
Parser Combinators from zero in Haskell
module Parser.Combinators where
import Test.Hspec
import Data.Either
import Data.Char
import Control.Applicative
import Data.Foldable
newtype Parser a = Parser (String -> (Either String a, String))
@gabrielelana
gabrielelana / functional-ladder.md
Created November 7, 2019 15:45
Functional Ladder

LEGEND

  • x-- means: I know about it
  • xx- means: I understand it
  • xxx means: I can explain it

NOVICE

CONCEPTS

@gabrielelana
gabrielelana / io.hs
Last active October 10, 2019 08:25
IO Inside - Welcome to the Real World
import Debug.Trace (trace)
-- Try to implement and use a `putString` function without the RW and
-- see the behaviour of calling it and calling it multiple times
-- type RW = RW
type RW = Integer
type MIO a = (RW -> (a, RW))