Skip to content

Instantly share code, notes, and snippets.

View jjhiggz's full-sized avatar
🎯
Focusing

Jonathan Higger jjhiggz

🎯
Focusing
View GitHub Profile
@jjhiggz
jjhiggz / build-matrix.test.ts
Created September 9, 2024 16:05
Multi Item Alignment Stuff
import { buildMatrix } from "./build-matrix";
describe("Build Matrix", () => {
it("Should turn my shit into matrix of rows", () => {
expect(
buildMatrix({
// prettier-ignore
elements: [
1/1,
1/3, 2/3,
@jjhiggz
jjhiggz / closest.ts
Created September 5, 2024 16:47
closest three sum
function reduce<T, U>(
arr: T[],
reducer: (acc: U, currentValue: T, index: number, array: T[]) => U,
initialValue: U,
index: number = 0
): U {
// Base case: if the index is out of bounds, return the accumulated value
if (index >= arr.length) {
return initialValue;
}
@jjhiggz
jjhiggz / fib-answers.ts
Created September 4, 2024 19:10
different Fibonacci answers
const at = <T,>(arr: T[], index: number) => index < 0
? arr[arr.length + index]
: arr[index];
let recursiveFibCount = 0
const rFibonnaci = (num = 1, prevSeq: number[] = [0]): number => {
if(prevSeq.length === num) {
return at(prevSeq, -1)
}
if(prevSeq.length === 1) return rFibonnaci(num, [0, 1])
@jjhiggz
jjhiggz / move-to-end.ts
Created August 31, 2024 04:45
moveToEnd generic
function moveAllInstancesToEnd<T extends any[] | string>(
input: T,
shouldMoveCb: (input: T[number]) => unknown
): T {
if(Array.isArray(input)){
let notMoved: any = []
let moved: any = []
for(let el of input){
if(shouldMoveCb(el)){
moved.push(el)
@jjhiggz
jjhiggz / createDictionary.ts
Created August 31, 2024 04:30
createDictionary.ts
const getCounts = <T,>(
input: Iterable<T>,
shouldCountCb: (input: T) => unknown,
serializeKey: (input: T) => any = input => input,
) => {
let counts = {} as Record<any, number>
for(let val of input){
if(shouldCountCb(val)){
counts[serializeKey(val)] = (counts[serializeKey(val)] ?? 0) + 1
}
@jjhiggz
jjhiggz / accordion.ts
Created August 21, 2024 18:34
Dumb Accordion Component
export const Accordion = (
{
children,
show
} : {
children: ReactNode,
show: boolean
}
) => {
return show ?
@jjhiggz
jjhiggz / result.ts
Created August 15, 2024 04:08
result
const result = <T,E extends Error>(cb: () => T): [null, E] | [T, null] => {
try {
return [cb(), null]
} catch(e){
return [null,e as E]
}
}
const failsSometimes = () => {
if(Math.random() > .5){
throw new Error("Failed")
@jjhiggz
jjhiggz / SyncPromise.ts
Created August 14, 2024 21:22
A syncronous typesafe promise (brought to you by chatGPT)
class SyncPromise<T> {
private value?: T;
private error?: any;
private isFulfilled: boolean = false;
private isRejected: boolean = false;
constructor(executor: () => T) {
try {
this.resolve(executor());
} catch (e) {
@jjhiggz
jjhiggz / index.ts
Created December 17, 2023 15:28
AOC Day 11 2023
import { sumBy } from "remeda";
import { Matrix, MatrixCell, MatrixPosition } from "./data-structures/matrix";
const exampleText = await Bun.file("example.txt").text();
const buildMatrix = (input: string) =>
new Matrix(input.split("\n").map((n) => n.split("")));
const tagValues = (input: Matrix<string>) => {
let count = 0;
@jjhiggz
jjhiggz / example.ts
Created August 11, 2023 13:24
Why Record Types can be annoying in JS / TS
// let's say I want to find the most occurring year here
const years = [1999,1993, 1992, 1991, 1991, 1999, 1993, 1993]
// I can use a Record<number, number> to create a map like so
// {1999: 2, 1993: 3, 1992: 1, 1991: 2, }
const yearMap: Record<number, number> = {}
for(let year of years){
if(year in yearMap){