Skip to content

Instantly share code, notes, and snippets.

View JeffML's full-sized avatar
🏠
Barely working

Jeff Lowery JeffML

🏠
Barely working
View GitHub Profile
var CombinatorGenerator = function (opts) {
function* combine(current, remainder) {
if (remainder.length === 0) {
if (current.length >= (opts.min || 0) &&
current.length <= (opts.max || current.length)) {
yield(current);
}
} else {
combine(current.concat(remainder[0]), remainder.slice(1, remainder.length))
combine(current, remainder.slice(1, remainder.length))
var Combinator = function (opts) {
var combinations = [];
function combine(current, remainder) {
if (remainder.length === 0) {
if (current.length >= (opts.min || 0) &&
current.length <= (opts.max || current.length))
combinations.push(current);
} else {
combine(current.concat(remainder[0]), remainder.slice(1, remainder.length));
const choices = [
[['VANILLA'], ['coconut flakes', 'pecans'], ['marshmallow']],
[['CHOCOLATE'], ['pecans'], ['chocolate']],
[['STRAWBERRY', 'VANILLA'], ['pineapple', 'coconut flakes'], ['marshmallow']],
[['STRAWBERRY'], ['pecans'], ['maple']],
[['VANILLA'], ['coconut flakes', 'pineapple'], ['chocolate']],
[['CHOCOLATE, STRAWBERRY'], ['pineapple', 'pecans'], ['butterscotch']],
];
const keys = _.map(choices, c => {
const PouchDB = require('pouchdb');
const db = new PouchDB('choices');
// doc that defines the view
var ddoc = {
_id: '_design/choice',
views: {
by_key: {
map: function (doc) {
emit(doc.key, doc.taste);
const _ = require('lodash');
const hash = require('./hash');
const PouchDB = require('pouchdb');
const db = new PouchDB('choices');
db.allDocs({
include_docs: true
})
.then(docs => {
const crypto = require('crypto');
const _ = require('lodash')
function hash(choice) {
var str = _.chain(choice)
.flatten()
.sortBy()
.join('|')
.value();
var _ = require('lodash');
var PouchDB = require('pouchdb');
var db = new PouchDB('choices');
db.allDocs({
include_docs: true
})
.then(docs => {
_.each(docs.rows, r => {
@JeffML
JeffML / run.js
Last active January 9, 2017 00:16
function run() {
var menu = { //...
}
var iceCreamChoices = new Combinator({ //...
});
var toppingChoices = new Combinator({ //...
});
var syrupChoices = new Combinator({ //...
});
var count = 0;
_.each(iceCreamChoices, function(ic) {
_.each(toppingChoices, function(tp) {
_.each(syrupChoices, function(sy) {
//allChoices.push([ic,tp,sy]);
db.post({choice: [ic,tp,sy]}, function(err, doc){
if (err) console.error(err);
else console.log(`stored ${++count}`);
});
var allChoices = [];
_.each(iceCreamChoices, function(ic) {
_.each(toppingChoices, function(tp) {
_.each(syrupChoices, function(sy) {
allChoices.push([ic,tp,sy]);
})
})
})