Skip to content

Instantly share code, notes, and snippets.

View trvswgnr's full-sized avatar
:octocat:
hardly workin'

Travis Wagner trvswgnr

:octocat:
hardly workin'
View GitHub Profile
@trvswgnr
trvswgnr / fearless.rs
Last active September 22, 2023 19:10
fearless concurrency macro
macro_rules! fearless {
($t:ty) => {
std::sync::Arc<std::sync::Mutex<$t>>
};
($e:expr) => {
std::sync::Arc::new(std::sync::Mutex::new($e))
};
(clone $e:expr) => {
std::sync::Arc::clone(&$e)
};
@trvswgnr
trvswgnr / 1-TripMap.ts
Last active September 21, 2023 16:41
triple key map, where the order of keys doesn't matter
export class TripMap<K, V> {
private map: Map<number, V>;
constructor() {
this.map = new Map<number, V>();
}
private generateKey(...items: any[]): number {
return [...new Set(items)]
.map((x) => this.hash(x))
@trvswgnr
trvswgnr / path.txt
Last active September 15, 2023 06:26
available cli commands, tools, and packages for chatgpt advanced data analysis
aclocal
aclocal-1.16
activate-global-python-argcomplete
acyclic
add-apt-repository
add-shell
addgnupghome
addgroup
addpart
addr2line
@trvswgnr
trvswgnr / type-bst.ts
Created September 2, 2023 05:50
type-level bst typescript
type TreeNode<V extends number = number, L = null, R = null> = {
value: V;
left: L;
right: R;
};
type BinarySearchTree<T = null> = {
root: T;
};
@trvswgnr
trvswgnr / prim.rs
Last active August 27, 2023 16:26
prim's algo in crab & ts
use std::cmp::Ordering;
use std::collections::BinaryHeap;
const INF: u32 = u32::MAX;
#[derive(Copy, Clone, Eq, PartialEq)]
struct Edge {
weight: u32,
vertex: usize,
}
@trvswgnr
trvswgnr / leftpad.java
Last active August 27, 2023 23:08
left-pad for enterprise
interface Logger {
void error(String message, Throwable t);
}
class ConsoleLogger implements Logger {
private final String className;
ConsoleLogger(String className) {
this.className = className;
}
@trvswgnr
trvswgnr / nosqlfiles.ts
Created August 25, 2023 21:22
nosql db with files
import { promises as fs } from 'fs';
import { v4 as uuidv4 } from 'uuid';
import { Mutex } from 'async-mutex';
interface Document {
[key: string]: any;
}
interface Index {
[key: string]: { [value: string]: string[] };
@trvswgnr
trvswgnr / 1_connect_four_winner.sh
Last active August 24, 2023 04:10
check connect four winner
#!/usr/bin/env bash
declare -A board
for ((i=1; i<=7; i++)); do
for ((j=1; j<=6; j++)); do
board[$i,$j]=' '
done
done
check_winner() {
@trvswgnr
trvswgnr / invert_binary_tree.rs
Created August 23, 2023 05:44
invert binary tree neg operator crab
fn main() {
// create a new binary tree
let mut root = TreeNode::new(1);
let left_child = TreeNode::new(2);
let right_child = TreeNode::new(3);
root.set_left(left_child);
root.set_right(right_child);
@trvswgnr
trvswgnr / concurrency.js
Last active January 20, 2024 09:29
run up to n concurrently, catching and returning errs
async function processItems(items, fn) {
const errs = [];
let i = 0;
const workers = Array(25)
.fill()
.map(async () => {
while (i < items.length) {
await fn(items[i++]).catch((e) => errs.push(e));
}
});