Skip to content

Instantly share code, notes, and snippets.

View Protonk's full-sized avatar
🗯️
Before the hinge

Adam Hyland Protonk

🗯️
Before the hinge
View GitHub Profile
@Protonk
Protonk / fxn2json.R
Last active December 25, 2015 00:19
View R function bodies as JSON Objects
## Once you've loaded the dependencies you don't have to load them again in a session
## May have to install them via `install.packages(c('RJSONIO', 'plyr'))` first and only once per device
# Uses C parser for faster results compared to rjson
require('RJSONIO')
# Get a quick non-trivial function example
require('plyr')
# body(x) extracts the function body for x (also could set the function body of x)
ddBody <- as.list(body(ddply))
@Protonk
Protonk / better.R
Created August 16, 2013 18:40
A smarter solution to matching many to many
# For each element, match against the data frame of longer vectors
compSlightlySmarter <- function(element, table) {
matches <- sapply(table, function(x) {
# finds exact matches of element to the 'table' (one column in the longer df)
locations <- match(element, as.character(x))
# if there are no unbroken or unordered sequences, it's a match!
return(!any(is.na(locations)) & !is.unsorted(locations))
})
if(any(matches)) {
# unlike the dumb version, we return matches
@Protonk
Protonk / dumbest.R
Created August 16, 2013 18:00
The dumbest solution to the problem
long <- data.frame(c("foo","qux","baz","quux"),c("baz","bar","foo","corge"))
shrt <- data.frame(c("foo","bar","baz"),c("baz","bar","foo"))
concatDumb <- function(data) {
listed <- sapply(data, function(x) {
# concatenate the character vectors to use substring matching
paste0('$', paste(unname(x), collapse = '$'), '$')
})
return(unname(listed))
}
@Protonk
Protonk / sigdigit.js
Last active December 21, 2015 00:39
find significant digits. Useful for approximate equality tests where you care about orders of magnitude
/*
* sigDigitFind
*
* Determine position of most significant digit
*
* Returns a positive number for significant digits left of the radix
* and a negative number for right of the radix.
*
* @param {String} inputFloat A String (optionally a Number) representing a
* floating point number.
@Protonk
Protonk / badideajeans.js
Created August 12, 2013 22:05
I can't tell if this is handy or a terrible antipattern
var numerator = [1,2,3,4];
var denominator = [4,3,2,1];
numerator.map(function(elem, index) {
return elem / denominator[index];
});
#!/bin/bash
for remote in gnarf jugglinmike oconnore protonk rwaldron
do
git remote add $remote git@github.com:$remote/gaia
done
function randme(arr) {
var rand = parseInt(Math.random().toString().charAt(3), 10);
return arr[rand % arr.length];
};
@Protonk
Protonk / simpleinttobin.js
Last active December 20, 2015 07:49
For Jory
function intAdd(arr) {
var out;
out = arr.reduce(function(l, r) {
return l + r;
});
return out.toString(2);
}
intAdd([1,2,3])
// "110"
@Protonk
Protonk / weirdgetter.js
Created July 17, 2013 21:03
Needlessly replicating a pattern here.
var initRefs = function(refObj, target) {
Object.keys(refObj).forEach(function(key) {
var value = refObj[key];
Object.defineProperty(target, key, {
// Retain lazy loading of document elements
get: function() {
delete this[key];
return this[key] = "bananas";
},
configurable: true,
@Protonk
Protonk / johnnyfive.R
Created July 17, 2013 14:18
Useful mostly for forcing sub-second timing in R
library(RJSONIO)
library(ggplot2)
library(reshape2)
accel.list <- fromJSON(file.path("~/dev/bocoup", "johnny-five", "accel.json"))
accelToDF <- function(x) {
sm <- x[["smooth"]]
rough <- x[["accel"]]
time <- x[["time"]]
names(sm) <- c("X", "Y")