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 / 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 / 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 / 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 / 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 / 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 / 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 / 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 / composition.ts
Last active April 14, 2025 16:57
Composition vs Inheritance
// Define individual animal types
type Dog = {
type: "dog";
name: string;
sound: "bark";
};
type Cat = {
type: "cat";
name: string;