Skip to content

Instantly share code, notes, and snippets.

@mcfedr
mcfedr / bash_prompt.sh
Created July 23, 2019 12:41
Bash prompt including current AWS profile, gcloud context, k8s context+namespace
# prompt
__aws_ps1() {
local printf_format="$1"
local profile="${AWS_PROFILE:-ekreative}"
if [ "$profile" != "ekreative" ]; then
printf -- "$printf_format" "${AWS_PROFILE:-ekreative}"
fi
}
__gcloud_ps1() {
@mcfedr
mcfedr / Cache.swift
Created October 6, 2017 07:53
This is an copy of NSCache but in a more Swift friendly way
// This is an copy of NSCache but in a more Swift friendly way
// Can store Any, with any Hashable key, so you are not limited
// to NSObjects as with NSCache
//
// Copyright (c) 2017 Fred Cox
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// Based on the NSCache code from the swift project
// https://github.com/apple/swift-corelibs-foundation/blob/master/Foundation/NSCache.swift
@mcfedr
mcfedr / fetch.js
Last active April 14, 2016 09:06
Shows how the content-length can be unknown
'use strict';
const FormData = require('form-data'),
fetch = require('node-fetch');
Promise.all([
fetch('http://httpbin.org/stream/10'),
fetch('http://httpbin.org/stream/10')
]).then(res => {
let data = new FormData();
@mcfedr
mcfedr / file.js
Created April 14, 2016 08:56
Shows how the content-length can be unknown
'use strict';
const FormData = require('form-data'),
fetch = require('node-fetch');
Promise.all([
fetch('http://httpbin.org/stream/10'),
fetch('http://httpbin.org/stream/10')
]).then(res => {
let data = new FormData();
@mcfedr
mcfedr / steps.js
Created December 22, 2015 17:05
Animation cycle
var a = true;
function doStep() {
....
if (a) {
setTimeout(doStep, 10);
}
}
setTimeout(doStep, 10);
@mcfedr
mcfedr / Events.js
Last active August 29, 2015 14:11
Adding events in different places
[].forEach.call(document.querySelectorAll('td[data-href]'), function(el) {
el.addEventListener('click', function (event) {
window.document.location = this.getAttribute('data-href');
});
});
$('td[data-href]').on('click', function() {
window.document.location = $(this).attr('data-href');
});
@mcfedr
mcfedr / recursion.c
Created November 7, 2014 21:16
Example of C compiler tail recursion
int isEven(int i) {
if (i == 0) {
return 1;
}
else if (i == 1) {
return 0;
}
else if (i < 0) {
return isEven(i + 2);
}
@mcfedr
mcfedr / recursion.asm
Last active April 8, 2016 11:24
Example of how V8 compiles recursion
;;; --- Skip lots of stuf ---
;;; <@19,#13> compare-numeric-and-branch
0x49252d6f 47 83f800 cmp eax,0x0
0x49252d72 50 0f843e000000 jz 118 (0x49252db6)
;;; <@20,#17> -------------------- B2 (unreachable/replaced) --------------------
;;; <@24,#24> -------------------- B3 --------------------
;;; <@27,#26> compare-numeric-and-branch
0x49252d78 56 83f802 cmp eax,0x2 ;; debug: position 81
0x49252d7b 59 0f842a000000 jz 107 (0x49252dab)
@mcfedr
mcfedr / gist:3983284
Created October 30, 2012 21:46
Connect middleware for CORS
app.use(function(req, res, next) {
var oneof = false;
if(req.headers.origin) {
res.header('Access-Control-Allow-Origin', req.headers.origin);
oneof = true;
}
if(req.headers['access-control-request-method']) {
res.header('Access-Control-Allow-Methods', req.headers['access-control-request-method']);
oneof = true;
}
@mcfedr
mcfedr / lock.js
Created August 13, 2012 21:02
callback lock queue - simple way to make functions that rely on callbacks execute serially.
/*
* return an object with two functions
* push - push an invocation of t.f(args) which will be called immediately if the q is empty
* next - the function that should be called in f to signal that the next invocation can be run
* important! - every function given to push should call next
*/
function lock() {
var q = [], busy = false;
return {
push: function(f, args, t) {