Skip to content

Instantly share code, notes, and snippets.

View therealklanni's full-sized avatar
👨‍💻
Learning Rust

Kevin Lanni therealklanni

👨‍💻
Learning Rust
View GitHub Profile
/*global document*/
//a function to get DOM nodes by nodeType property.
//If you do not supply a value I will give you every DOM node.
//
//example:
// var allComments = document.getNodesByType(8);
// or
// var allComments = document.getNodesByType("COMMENT_NODE");
//
//The accepted string values are the actual node type names, so that the
@therealklanni
therealklanni / distill.js
Created March 29, 2014 04:13
Array of Objects distiller
// Found myself needing to do this the other day...
//
// Map an array of objects down to a single object in one pass
function distill (a, b) {
a[b.name] = b.value;
return a;
}
// Say you have objects that follow this format
var obj1 = {
@therealklanni
therealklanni / unique.js
Created March 29, 2014 04:30
Get unique elements of an Array
// Another useful reducer
//
// Get unique values of a 1d array
function unique (a, b) {
if (a.indexOf(b) < 0) a.push(b)
return a
}
// Very simple use case
var arr = [1, 5, 7, 2, 2, 3, 3, 4, 5, 5, 52, 4, 1, 2, 9, 1, 3, 5]
@therealklanni
therealklanni / yc.js
Last active August 29, 2015 14:00
Memoized Y-combinator
// Y-combinator with memoization
var Yc = function (f, x) {
x = x || {};
return function () {
var y = JSON.stringify([].slice.call(arguments));
return x[y] || (x[y] = f(function (z) {
return Yc(f, x)(z);
}).apply(this, arguments));
@therealklanni
therealklanni / Node.sublime-build
Last active August 29, 2015 14:01
Sublime Text Node.js Buildtool
{
"cmd": ["/usr/local/bin/node", "$file"],
"selector": "*.js"
}
var fs = require('fs');
var rfs = fs.readFileSync;
var wfs = fs.writeFileSync;
var path = require('path');
var yaml = require('js-yaml');
var inquirer = require('inquirer');
var program = require('commander');
var version = require('./package.json').version;
program
from Crypto.Cipher import AES
from Crypto import Random
from libmproxy.flow import decoded
BS = 16
pad = lambda s: s + (BS - len(s) % BS) * chr(BS - len(s) % BS)
unpad = lambda s : s[0:-ord(s[-1])]
def decrypt( enc, key ):
iv = enc[:16]
@therealklanni
therealklanni / 0_reuse_code.js
Last active August 29, 2015 14:07
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
@therealklanni
therealklanni / y-combinator.md
Last active August 29, 2015 14:23
The Mysterious Y-Combinator

The Mysterious Y-Combinator

You may have heard the term before—you may even know what it is already. If you want a really technical description of the Y-combinator, look no further than Wikipedia (though we can certainly do better). However, this will leave most of us laymen more confused than we were going in. So what is the Y-combinator, really?

Getting right to it, written in ES6 this would look like the following (courtesy Brendan Eich):

let Y = f => (x => f(v => x(x)(v)))(x => f(v => x(x)(v)))
// pronounce Yl like "while" ;)
let Yl = Y(f => (g, i) => {
let r = g(i - 1)
return r !== false ? r : f(g, i - 1)
})
// extremely contrived example using Yl
let people = [ 'Shannon', 'Joseph', 'Eric', 'Mike']