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 / filterUsingIntersections.ts
Last active April 1, 2022 13:51
Use intersections to filter unions
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"
//
@jfet97
jfet97 / asConst.ts
Last active May 5, 2022 18:40
as const without as const
// 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
//
@jfet97
jfet97 / mappedTypesDistribute.ts
Last active April 18, 2022 18:45
Mapped types distribute as well
type Nodes =
| {
type: "a";
age: number;
}
| {
type: "b";
size: string;
};
@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 / sum.ts
Last active February 19, 2022 22:33
Sum numbers, but in the type system!
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"];
@jfet97
jfet97 / id_encoding.js
Created January 29, 2022 19:21
My first initial encoding of the Id monad with a stack safe interpreter
const FlatMap = (ma, famb) => ({
ma,
famb,
tag: 'FlatMap'
})
const Pure = (a) => ({ a, tag: 'Pure' })
const expression = FlatMap(
FlatMap(
@jfet97
jfet97 / tfs.scala
Last active January 16, 2022 13:11
Tagless Final Scala
// 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
// 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"
@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))
)
}
function createAsyncArray(...values) {
return values.map((v) => ({
resolver: () => {},
promise: Promise.resolve(v),
}));
}
function get(a, i) {
if (a[i] == void 0) {
let resolver = () => {};