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 Decision = "yes" | "no" | 1 | 0 | true | false; | |
type JustStrings = Decision & string; | |
/// = ("yes" | "no" | 1 | 0 | true | false) & string | |
/// = | ("yes" & string) | ("no" & string) | |
/// | (1 & string) | (0 & string) | |
/// | (true & string)| (false & string)) | |
/// = "yes" | "no" | never | never | never | never | |
/// = "yes" | "no" | |
// |
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
// is always the extends keyword that makes the difference, enabling the narrow infering | |
type Narrowable = string | number | boolean | symbol | undefined | null | void; | |
declare function wrongKeepLiteralTypes(x: Narrowable): Narrowable; | |
declare function keepLiteralTypes<T extends Narrowable>(x: T): T; | |
wrongKeepLiteralTypes(1); // Narrowable | |
keepLiteralTypes(1); // 1 | |
// |
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 Nodes = | |
| { | |
type: "a"; | |
age: number; | |
} | |
| { | |
type: "b"; | |
size: string; | |
}; |
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 SameLenTuple<N extends number, T extends any[] = []> = number extends N | |
? never | |
: T["length"] extends N | |
? T | |
: SameLenTuple<N, [0, ...T]>; | |
type ConcatTuples<T1 extends any[], T2 extends any[]> = [...T1, ...T2]; | |
type TupleToLen<T extends any[]> = T["length"]; |
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
const FlatMap = (ma, famb) => ({ | |
ma, | |
famb, | |
tag: 'FlatMap' | |
}) | |
const Pure = (a) => ({ a, tag: 'Pure' }) | |
const expression = FlatMap( | |
FlatMap( |
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
// expression problem: | |
// evaluating expressions mantaining type safety and returning | |
// the right value of the right type | |
// In functional programming all is an expression | |
object ExpressionProblem { | |
trait Expr | |
case class B(boolean: Boolean) extends Expr | |
case class Or(left: Expr, right: Expr) extends Expr | |
case class And(left: Expr, right: Expr) extends Expr |
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
// cata alg = alg . fmap(cata alg) . unfix | |
function cata(alg, FixF) { | |
return (i) => alg(FixF.fmap(cata(alg, FixF))(FixF.unfix(i))) | |
} | |
// ListF a x = NilF | ConsF a x | |
// where 'a' is the type of the values inside the list, | |
// whereas 'x' is the type on which the list will be evaluated (aka the carrier type) | |
const NilF = () => ({ | |
_tag: "NilF" |
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
function createAsyncArray(...values) { | |
return values.map((v) => ({ | |
resolver: () => {}, | |
promise: Promise.resolve(v), | |
})); | |
} | |
function get(a, i) { | |
if (a[i] == void 0) { | |
let resolver = () => {}; |