Skip to content

Instantly share code, notes, and snippets.

View IronSavior's full-sized avatar

Erik Elmore IronSavior

View GitHub Profile
@IronSavior
IronSavior / make-git-dag-alias.sh
Created January 24, 2020 22:51
Custom git log graph
#!/bin/bash
commit='%C(dim yellow)%h'
time='%C(bold yellow)%ad'
author='%C(dim cyan)%an'
refs='%C(auto)%d'
subject='%C(dim white)%s'
fmt="$commit $time $author ${refs}%n${subject}%n"
git config --global alias.dag "log --graph --date=relative --pretty=format:'${fmt}'"
@IronSavior
IronSavior / s3_list_objects_stream.js
Last active December 6, 2018 02:18
S3.listObjectsV2 as stream
"use strict";
const {Readable} = require('readable-stream');
const {S3} = require('aws-sdk');
// Convert the given S3::listObjectsV2 request to a Readable stream
// req should be an un-sent S3::listObjectsV2() request
// opts are typical stream options. Object mode is required.
function s3_list_stream( req, opts ){
opts = Object.assign({}, opts, {read, objectMode: true});
const stream = new Readable(opts);
@IronSavior
IronSavior / README.md
Last active September 5, 2018 21:03
Xen Dom0 on Debian Stretch with VLAN (with and without Open vSwitch)

Xen Dom0 on Debian Stretch

Version 2018-01-17: Erik Elmore :octocat: erik@elmore.io

Install Dom0 System

Use the conventional Debian installer to install the base system. Disk layout:

  • 256M boot ext4. Needs to be big enough for all kernel versions and modules for guests
  • Rest of the disk to LVM2. Create logical volumes for:
    • root ext4 (8G-16G)
    • swap (optional, whatever size you like)
@IronSavior
IronSavior / to-function.js
Created August 7, 2017 22:30
JavaScript named predicate helper (Symbol#to_proc from Ruby)
"use strict";
// Make predicate funtions more convenient.
// Works like Ruby's Symbol#to_proc, which is usually called with the unary & operator and a Symbol literal:
// (ruby) %w[one two three].map(&:upcase) # returns ["ONE", "TWO", "THREE"]
// (js) ['one', 'two', 'three'].map(toFn('toUpperCase')); // returns ["ONE", "TWO", "THREE"]
// (js) ['one', 'two', 'three'].map(toFn`toUpperCase`); // same as above, but called via tagged string literal
// @param name {String} Name of method
// @param ...bound_args Arguments to bind NOTE: Args cannot be bound when calling as a tagged-template
// @return {Function} Function to call named method with bound args on any object given at time of invocation
module.exports = function toFn( name, ...bound_args ){
@IronSavior
IronSavior / cgow_coroutine.py
Created January 4, 2018 01:02
Conway's Game of Life via Python Coroutines / Generators
# Conway's Game of Life via Python coroutines
# Inspired by https://effectivepython.com/2015/03/10/consider-coroutines-to-run-many-functions-concurrently/
from enum import Enum
from collections import namedtuple
from itertools import islice, count, product, chain, repeat, accumulate, starmap
from functools import partial, lru_cache
from time import sleep
import operator as op
@IronSavior
IronSavior / concurrent-transform-stream.js
Last active September 18, 2017 19:59
A through2-compatible Transform stream which processes chunks with limited (or unlimited) concurrency
"use strict";
const {Transform} = require('readable-stream');
const PRIVATE = Symbol('ConcurrentTransform private state');
const NOOP_TRANSFORM = (chunk, _, done) => done(null, chunk);
const NOOP_FLUSH = done => done();
// Transform stream which processes chunks with limited (or unlimited) concurrency
class ConcurrentTransform extends Transform {
// @param opts {Object} Stream options. Set concurrency limit with "concurrent" (default concurrency: 1)
constructor( opts = {} ){
@IronSavior
IronSavior / simple-demux-stream.js
Created September 12, 2017 00:40
Simple stream demuxer
const {Readable, Writable} = require('stream');
const PRIVATE = Symbol('private state accessor');
// Writable stream that routes each chunk through one of its output streams as determined by a user-supplied fdunction.
//
// An output stream is dynamically created whenver the channel selector function returns a new channel ID. The channel
// IDs may be any value which can function as a Map's key. Listeners to the "channel" event will receive a new Readable
// channel output stream when a new channel is created.
//
// Please note that this stream respects backpressure signaled by its outputs. This means that the overall throughput
@IronSavior
IronSavior / error-policy-examples.js
Last active August 7, 2017 23:05
Composed error handling policy -- mix sync/async without missing errors
const error_policy = require('./error-policy');
// EventEmitter example
const {Readable} = require('stream');
class MyReadable extends Readable {
constructor(){
super(...arguments);
this.policy = error_policy(e => this.emit('error', e));
}
require 'set'
# Invokes the block, passing in all or some of the input. If the block throws, the block will be invoked
# repeatedly for a progressively smaller subset of the batch until all elements are either processed or
# failed. Elements contained in sub-batches which succeed are never re-tried. Returns two Arrays, the first
# contains all items which succeeded and the second contains all elements which failed.
#
# This can be useful when a batch API will throw when it receies one or more malformed items, but doesn't
# tell you which input items were at fault.
#
@IronSavior
IronSavior / main.rs
Created October 7, 2016 07:04
Rust Hexdump
use std::io::{self, Read, BufRead};
use std::cmp;
use std::fmt::{self, Write};
const HR_BYTES_PER_LINE: usize = 16;
struct HexReader<T> {
inner: T,
buf: String,
buf_pos: usize,