Skip to content

Instantly share code, notes, and snippets.

View koozdra's full-sized avatar

Dimitri Tishchenko koozdra

View GitHub Profile
@koozdra
koozdra / key_by_multiple.js
Created August 19, 2020 20:19
key array of objects by multiple keys
a = [ { name: 'bill', group: 'b' },
{ name: 'ann', group: 'a' },
{ name: 'ken', group: 'k' },
{ name: 'alex', group: 'a' } ]
_.reduce((sum, v) => {
const { name, group } = v;
const current = _.getOr([], [group, name])(sum);
return _.set([group, name], [...current, v])(sum);
}, {})(a);
@koozdra
koozdra / psf_q_learning.js
Last active June 18, 2020 05:34
post sign flow q-learning
shuffle_array = arr =>
arr
.map(a => [a, Math.random()])
.sort(([, lr], [, rr]) => lr - rr)
.map(([a]) => a);
user_action_for_state = (state, randy) => {
if (state === 'p' || state === 'ps' || state === 'psa' || state === 'psam') {
if (randy < 0.7) {
return primary_action_names[state[state.length - 1]];
@koozdra
koozdra / shuffle.js
Created June 15, 2020 15:27
javascript shuffle
shuffle_array = arr =>
arr
.map(a => [a, Math.random()])
.sort(([, lr], [, rr]) => lr - rr)
.map(([a]) => a);
@koozdra
koozdra / basicbandit.js
Created November 26, 2019 17:58
A basic implementation of bandits for a demo
const times = require('lodash/fp/times');
const map = require('lodash/fp/map');
const sample = require('lodash/fp/sample');
const maxBy = require('lodash/fp/maxBy');
const find = require('lodash/fp/find');
const join = require('lodash/fp/join');
const flow = require('lodash/fp/flow');
const get = require('lodash/fp/get');
const sum = require('lodash/fp/sum');
const constant = require('lodash/fp/constant');
@koozdra
koozdra / argmaxall.js
Created October 2, 2019 23:20
Get all values that have a max value under a getter
const a = [
{ a: 'one', b: 4 },
{ a: 'two', b: 1 },
{ a: 'three', b: 4 },
{ a: 'four', b: 2 }
];
function argmaxAll(getterFunction, arr) {
return reduce(
([max, arr], curr) => {
_.reduce(
([numItemsSeen, mean], num) => [
numItemsSeen + 1,
mean + (num - mean) / (numItemsSeen + 1)
],
[0, 0]
)([3, 4, 5, 6, 10, 100, 9]);
@koozdra
koozdra / dice_triplets.js
Last active November 12, 2017 03:47
dice triplets
const diceTriplets = sides =>
_(sides)
.range()
.flatMap(a =>
_(sides)
.range()
.flatMap(b =>
_(sides)
.range()
.map(c => [a + 1, b + 1, c + 1])
@koozdra
koozdra / lodash_practice.js
Last active November 11, 2017 16:36
implementing some lodash functions
// modification version
function once(f) {
let hasRun = false;
let result;
return () => {
if (!hasRun) {
result = f();
hasRun = true;
}
return result;
@koozdra
koozdra / power set
Created November 8, 2017 22:44
Javascript powerset function
const _ = require('lodash');
function supersetInner(src) {
if (src.length === 0) return [];
const withouts = _.map(src, elem => superset(_.without(src, elem)));
return [src, ..._.flatten(withouts)];
}
function superset(src) {
@koozdra
koozdra / process.js
Created June 7, 2017 16:40
gm image manipulation
const fs = require('fs');
const gm = require('gm');
function getSize(image) {
return new Promise((resolve, reject) => {
image.size((err, size) => {
if (err) {
reject(err);
return;
}