Skip to content

Instantly share code, notes, and snippets.

View michaelhelvey's full-sized avatar

Michael Helvey michaelhelvey

View GitHub Profile
@michaelhelvey
michaelhelvey / stringifyParams.js
Created September 7, 2016 23:33
Stringify HTTP request parameters.
// Simple utility function to string together parameters for a get request.
// For example, using this function you can do something like this:
//
// const params = stringifyParams({
// param1: 'value',
// param2: 'otherValue',
// });
// const url = `https://api.yourthing.com/stuff?${params}`;
//
// This url will get rendered as:
// Callback must returns a promise.
// This function, unlike `Promise.all`, will resolve
// one promise at a time, in the original array order
const serial = (arr, callback) => {
return new Promise((resolve, reject) => {
arr.reduce((previous, current, index) => {
return previous.then(() => callback(current)).then(() => {
if (index === (arr.length - 1)) resolve();
}).catch(e => reject(e));
}, Promise.resolve());
@michaelhelvey
michaelhelvey / power_sets.js
Created April 11, 2018 23:58
Demonstrates an algorithm for calculating power sets
function pretty_print(set) {
set.forEach(item => {
console.log(JSON.stringify(item));
})
}
// where set is something of the form [item, item, item]
function get_power_set(set) {
// our result will itself be a set
let result = [];
@michaelhelvey
michaelhelvey / AuthRoute.js
Last active February 11, 2019 00:49
React-Router authorized route
const RequireAuth = connect(mapStateToProps)(
({ component: Component, ...rest }) => {
return (
<Route
{...rest}
render={props =>
rest.isAuthenticated ? (
<Component {...props} />
) : (
<Redirect
@michaelhelvey
michaelhelvey / dep_injection.py
Last active March 13, 2020 23:10
Single file dependency injection for Python
"""
Simple dependency injection package that builds up global tree of singleton
objects based on __init__ params.
Assumes that __init__ params will be named as the snake case equivalents of
the required class name. So if you need an instance of UserService
AppContainer assumes that the param name will be `user_service`.
Thanks to Python's automatic super() when child does not have __init__,
walks the inheritance tree by default.
@michaelhelvey
michaelhelvey / hash_map.cpp
Last active May 2, 2021 20:14
hash map in C++ (very shit do not look)
#include <cassert>
#include <iostream>
#include <string>
#include <vector>
#include <stddef.h>
template <typename Value>
class Node {
public:
Node(std::string k, Value v) : m_key(k), m_value(v), m_next(nullptr) {};
@michaelhelvey
michaelhelvey / Makefile
Created January 27, 2023 17:55
Simple makefile for C/CPP/ASM
# HT Job Vranish: https://spin.atomicobject.com/2016/08/26/makefile-c-projects/
TARGET_EXEC ?= target.out
BUILD_DIR ?= ./build
SRC_DIRS ?= ./src
SRCS := $(shell find $(SRC_DIRS) -name *.cpp -or -name *.c -or -name *.s)
OBJS := $(SRCS:%=$(BUILD_DIR)/%.o)
DEPS := $(OBJS:.o=.d)
@michaelhelvey
michaelhelvey / lexer.rs
Last active February 21, 2024 16:14
Elegant Rust lexer pattern
use miette::SourceSpan;
use unicode_segmentation::UnicodeSegmentation;
#[derive(Debug, PartialEq)]
pub enum TokenType {
// Operators:
Equals, // ==
NotEquals, // !=
Assignment, // =
Plus,
@michaelhelvey
michaelhelvey / event_loop.js
Created April 21, 2024 13:45
Simple Event Loop
const events = [];
const tasks = [];
function registerInterest(event, task) {
events.push({ event, task });
}
function waitForEvents() {
if (events.length === 0) {
return;