Skip to content

Instantly share code, notes, and snippets.

View ancms2600's full-sized avatar
💭
Toot! Toot!

Bobby Johnson ancms2600

💭
Toot! Toot!
View GitHub Profile
@ancms2600
ancms2600 / hidden-prompt.sh
Created December 18, 2017 15:54
Hidden bash password prompt
# from: https://unix.stackexchange.com/a/165494
echo -n "passwd: "; stty -echo; read passwd; stty echo; echo
some-program -p "$passwd"
passwd= # cleanup
@ancms2600
ancms2600 / notes.txt
Last active February 13, 2018 22:20
Declarative vs. Imperative, Procedural vs. Object-oriented vs. Functional Programming
Declarative programming: Expresses the logic of a computation without describing its control flow. (e.g., no loops)
e.g., SQL, RegExp, Markdown
example: SELECT * FROM table;
Imperative programming: Uses statements that change a program's state. Consists of commands for the computer to perform.
e.g., OpenGL, Canvas, Turtle, GCode, ASM, FORTRAN, SVG
example: while (true): up(1); right(2); stroke(); done();
Procedural programming: Derived from structured programming, based upon the concept of the procedure call.
Procedures, also known as routines, subroutines, or functions (not to be confused with mathematical functions,
@ancms2600
ancms2600 / es6-shorthand-classes.js
Last active March 15, 2018 19:04
lol shorthand es6 class syntax
// LOL! Look at this...
// This class syntax is very shorthand, inline, and anonymous.
// However I'm skeptical that the runtime interpreter is able to optimize it
// like traditional class semantics.
// class
const State = () => ({
// constructor
object: {},
array: [],
@ancms2600
ancms2600 / paste.ahk
Created March 26, 2018 17:42
AutoHotkey Script to type out clipboard contents (useful for VMWare vSphere Remote Console, which cannot paste)
; Ctrl-Shift-O
^!o::
; wait 2 seconds
Sleep, 2000
; type out clipboard contents
sendraw %clipboard%
@ancms2600
ancms2600 / routes.js
Created April 18, 2018 04:00
Express.js render Stylus .styl to .css every request; skips output file to local disk
// render .styl to .css every request; skips output file to local disk
app.use((req, res, next) => {
const stylus = require('stylus');
const join = require('path').join;
const src = join(__dirname, '/../public');
const url = require('url');
const fs = require('fs');
if ('GET' !== req.method && 'HEAD' !== req.method) return next();
const path = url.parse(req.url).pathname;
if (!/\.css$/.test(path)) return next();
@ancms2600
ancms2600 / server-client-browserify-shim.js
Last active May 19, 2018 15:36
Minimalist CommonJS module.exports compatibility shim for Node.js Server + Browser Client
'use strict';
(exports=>{ // browser-client reuse shim
// ...
})(typeof exports === 'undefined' ? window : exports);
@ancms2600
ancms2600 / single-line-variable-declaration-cache-reuse.js
Last active May 12, 2018 20:09
Sneaky single-line temporary scoping of variable declarations and cache-reuse (Javascript trivia)
// e.g., when you want parameters passed in a pre-sorted order, but you don't want to impose on the developer
{ // shadow to avoid mutating input
// three examples computing Math.min() just once, though two assignments:
const toZero = Math.min(a,c), a = a - toZero, c = c - toZero; // toZero still in scope by EOL (wasteful?)
with({toZero:Math.min(a,c)}) const [a,c] = [a-toZero,c-toZero]; // toZero out of scope by EOL (discouraged in "use strict" mode)
const [a,c] = [a,c].map((toZero=>v=>v-toZero)(Math.min(a,c))); // toZero out of scope by EOL
const [a,c] = (()=>{ const toZero = Math.min(a,c); return [a-toZero,c-toZero]; })(); // toZero out of scope by EOL (long-winded)
}
@ancms2600
ancms2600 / array-flatmap.js
Created May 12, 2018 20:00
Array .flatMap() Javascript shim (as per draft proposal)
Array.prototype.flatMap || (Array.prototype.flatMap = function () { // shim for draft proposal
return this.reduce(((acc,v)=>(acc.push(...v),acc)),[]); });
@ancms2600
ancms2600 / es6-template-tags.js
Created May 13, 2018 17:54
ES6 Template Tags exampple
// Template Tag function replaces string references to variable names with eval()'ed variable's value,
// and rpads the value string with spaces so that it takes up exactly the same space that the variable name used to.
// Makes it easier to maintain block column formatting across rows of text.
var apples = 1, bananas = 2, cucumbers = 3,
result = ((apples) * bananas) - cucumbers;
console.log(vpad`((apples) * bananas) - cucumbers = result`);
console.log(vpad`((${'apples'}) * ${'bananas'}) - ${'cucumbers'} = ${'result'}`);
@ancms2600
ancms2600 / cross-join.js
Created May 13, 2018 18:35
CROSS JOIN implementation in Javascript
var crossJoin = (...args) => {
const m = args.reduce((acc,a)=>{
acc.push(0 === acc.length ?
a.length :
(acc[acc.length-1] * a.length));
return acc;
},[]);
const o = [];
for (const [i,letters] of args.entries()) {