Skip to content

Instantly share code, notes, and snippets.

@heroqu
heroqu / es6_generator_fibo
Last active August 29, 2015 14:24
ES6 generator for Fibonacci
function* fibo(max) {
var a = 0,
b = 1,
c;
while (!max || a <= max) {
yield a;
c = a + b;
a = b;
b = c;
@heroqu
heroqu / es6_generator_fibo_cb
Last active August 29, 2015 14:24
ES6 generator for Fibonacci with callback and index
function* fibo(max, callback, _index) {
var a = 0,
b = 1,
c;
if (!_index) {
_index = 0;
};
while (!max || a <= max) {
yield callback(a, _index++);
c = a + b;
@heroqu
heroqu / osx-kill-menu-dupes.sh
Created September 21, 2015 10:02
Remove duplicates from OS X’s “Open With” menu
/System/Library/Frameworks/CoreServices.framework /Versions/A/Frameworks/LaunchServices.framework/Versions/A/Support/lsregister -kill -r -domain local -domain user
@heroqu
heroqu / github_repo_bulk_delete.sh
Created January 6, 2016 18:03
Bash script for bulk deletion of repos under your github account
#! /bin/bash
## Based on and inspired by
# https://medium.com/@icanhazedit/clean-up-unused-github-rpositories-c2549294ee45
## Vars
# 1. Your github user name
USERNAME=john
@heroqu
heroqu / vbga_install.sh
Created February 11, 2016 02:10
VirtualBoxGuestAddition install/upgrade for Mac OSX
#! /bin/bash
# VirtualBoxGuestAddition install/upgrade for Mac OSX
#
# VirtualBox should be installed prior to running this script
#
if [ "$EUID" -ne 0 ]; then
echo "Please run as root"
exit
fi
@heroqu
heroqu / permutations.rb
Created February 27, 2016 19:27
Print out all unique permutations of first N natural numbers
def permutations(n)
_p n-1, (1..n).to_a, ''
end
def _p(n, arr, res)
if n == -1
puts res
return
end
(0..n).each do |m|
@heroqu
heroqu / kill_node.sh
Last active April 11, 2016 09:27
Bash function (for OSX) to kill all running node processes, except for Atom editor
# kill all running node processes, except for Atom editor
function kinod {
read -r -p "Delete all node processes, except for Atom? [y/N] " response
case $response in
[yY][eE][sS]|[yY])
kill -9 `ps aux | grep node | grep -v grep | grep -v 'Atom.app' | awk '{print $2}'`
;;
*)
echo -- cancel --
;;
@heroqu
heroqu / add_months.js
Created May 16, 2016 09:51
Add / subtract number of months from given date (original date in param stays immutable).
function addMonths(date, months) {
var d = new Date(date.getTime());
var m = d.getMonth();
var y = d.getFullYear();
var ym = y * 12 + m;
ym = ym + months;
m = ym % 12;
y = (ym - m) / 12;
d.setYear(y);
d.setMonth(m);
@heroqu
heroqu / today.js
Created May 16, 2016 09:57
Get today date without time part (beginning of the current day)
function today() {
return new Date((new Date()).setHours(0, 0, 0, 0));
}
@heroqu
heroqu / flat_numeric_arr.js
Last active June 7, 2016 16:28
flatten an array of arbitrarily nested arrays of integers into a flat array of integers
'use strict'
/**
* flatten an array of arbitrarily nested arrays of integers into a flat array of integers
*/
function flat_numeric_arr(x, res) {
res = res || [];
if (typeof x === 'number') {
res.push(x);
} else if (Array.isArray(x)) {