Skip to content

Instantly share code, notes, and snippets.

Avatar
🐚

Seb Insua sebinsua

🐚
View GitHub Profile
View generic-maths-in-ts.ts
type CreateArray<V extends number, Arr extends unknown[] = []> =
Arr extends { length: V } ? Arr: CreateArray<V, [...Arr, 1]>;
type Add<A extends number, B extends number> = [...CreateArray<A>, ...CreateArray<B>]['length'];
type V = Add<2, 3>;
type Sub<A extends number, B extends number> = CreateArray<A> extends [...CreateArray<B>, ...infer Els] ? Els['length'] : never;
type V2 = Sub<10, 1>;
View rotate.js
function* unroll(matrix) {
const height = matrix.length;
const width = matrix[0].length;
for (let y = 0; y < height; y++) {
for (let x = 0; x < width; x++) {
yield { y, x };
}
}
}
View clojure.core.js
function createGenerator(maybeIterable) {
const isGenerator = (maybeGenerator) =>
"next" in maybeGenerator &&
typeof maybeGenerator["next"] === "function" &&
"throw" in maybeGenerator &&
typeof maybeGenerator["throw"] === "function" &&
"return" in maybeGenerator &&
typeof maybeGenerator["return"] === "function";
function* generatorFrom(maybeIterable) {
View TicTacToe.js
import { useState } from "react";
import "./styles.css";
function createGrid(n = 3) {
return Array(n)
.fill(() => Array(n).fill(null))
.map((createRow) => createRow());
}
View ShoppingCart.js
import {
useCallback,
useEffect,
useMemo,
useReducer,
useRef,
useState
} from "react";
import { createPortal } from "react-dom";
View BinaryTree.js
class BinaryTreeNode {
constructor(value) {
this.left = null;
this.right = null;
this.values = [value];
}
get value() {
const [firstValue] = this.values;
@sebinsua
sebinsua / Queue.js
Created Aug 11, 2022
Implementing a basic FIFO queue using a DoublyLinkedList. See: https://gist.github.com/sebinsua/d0d15ad44b721d5210a6dc1dd3a1bf7e
View Queue.js
class QueueItem {
constructor(value) {
this.value = value;
this.head = null;
this.tail = null;
}
valueOf() {
return this.value;
}
View DoublyLinkedList.js
class DoublyLinkedListNode {
constructor(value) {
this.value = value;
this.head = null;
this.tail = null;
}
valueOf() {
return this.value;
}
View SinglyLinkedList.js
class SinglyLinkedListNode {
constructor(value) {
this.value = value;
this.tail = null;
}
valueOf() {
return this.value;
}
}
View join.ts
type GetTail<Items extends string[]> = Items extends [any, ...infer Tail]
? Tail
: string[];
type HasTail<Items extends string[]> = Items extends [any, any, ...any[]]
? true
: false;
type Join<Strs extends string[], Delimiter extends string = ""> = HasTail<Strs> extends false
? Strs[0] extends string ? Strs[0] : ''