Skip to content

Instantly share code, notes, and snippets.

View ericyd's full-sized avatar

Eric Yancey Dauenhauer ericyd

View GitHub Profile
@ericyd
ericyd / ostructify.rb
Created October 11, 2019 17:06
Ostructify: Recursively create OpenStructs of your hash or array or hashes
require 'ostruct'
# Recursively return OpenStructs from a hash or nested hash
# Handles array of hashes too
# Returns primitives when they are neither Hash nor Array
def ostructify(thing, camelize: false, snakify: false)
if thing.class == Hash
thing.reduce(OpenStruct.new) do |ostruct, (key, value)|
key = camelize ? camelize(key) : key
key = snakify ? snakify(key) : key
@ericyd
ericyd / compose.rb
Created November 16, 2018 06:05
Ruby Compose
add = Proc.new{|a| a + 2}
times = Proc.new{|a| a * 2}
def compose2(f, g)
Proc.new {|*x| f.call(g.call(*x))}
end
def compose(*x)
x.reduce { |all, a| compose2(all, a) }
end
@ericyd
ericyd / fuse_web_worker.js
Created September 14, 2018 17:19
Fuse JS Web Worker implementation
/************************
search.js
************************/
// an example of how to implement fuse.js with a web worker
var searchWorker; // initialized in document ready function
if(typeof(Worker) !== "undefined") {
if(typeof(searchWorker) == "undefined") {
searchWorker = new Worker('/scripts/searchWorker.js');
}
/**
* Takes a date object and returns a string in the form YYYY-MM-DD
* e.g. formatDate(newDate()) should return 2017-02-17 for February 17, 2017.
*
* @param {date} d the date object to be formatted
*/
function formatDate (d = new Date()) {
// explain:
// | YYYY format | add leading 0 to Jan-Sep | month is 0-indexed | add leading 0 to days 1-9 | day of month
return `${d.getFullYear() }-${ d.getMonth() < 9 ? '0' : '' }${ d.getMonth() + 1 }-${ d.getDate() < 10 ? '0' : '' }${ d.getDate()}`;
@ericyd
ericyd / get_combinations.rs
Last active April 30, 2018 19:40
Rust script to get all possible combinations of sets of vectors
// some experiments with combinations and deduping
// I think clippy has some good recommendations - I wrote this while learning Rust so it's rough
fn main() {
let arr = vec![vec![1, 1, 1], vec![2, 2, 2], vec![3, 3, 3], vec![4, 4, 4]];
let mut indices = arr.iter().map(|ref _x| 0).collect();
let mut paths: Vec<Vec<usize>> = vec![];
let paths = find_combinations(&arr, &mut indices, 0, &mut paths);
let paths_dedupe1 = dedupe(&paths, &mut vec![]);