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 / graph.ts
Created January 23, 2025 21:01
Useful Data Structures
import { uniqueBy } from "remeda";
import { SuperMap } from "./SuperMap.ts";
export class Graph<T> {
adjacencyList: SuperMap<T, T[]>;
private serialize: (input: T) => string;
private deserialize: (input: string) => T;
private printVal: (input: T) => any;
constructor(
@jjhiggz
jjhiggz / advent.ts
Last active December 9, 2024 06:49
import { piped } from "remeda";
import z from "zod";
const getBlocks = piped(
(a: string) => a.split(""),
(a) =>
a.map((char, index) => {
const isEven = index % 2 === 0;
const charValue = z.number().parse(+char);
@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) {