This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/************************ | |
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'); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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()}`; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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![]); |
NewerOlder