Skip to content

Instantly share code, notes, and snippets.

View dir01's full-sized avatar

Andrew Zhukovskiy dir01

  • Tel Aviv, Israel
View GitHub Profile
@dir01
dir01 / circularQueue.spec.js
Created November 26, 2019 22:28
Ring buffer implementation
class CircularQueue {
constructor(size) {
this.size = size;
this.storage = Array.from(new Array(size));
Object.seal(this.storage);
this.head = -1;
this.tail = -1;
}
queue(value) {
@dir01
dir01 / Dockerfile
Last active May 15, 2019 09:07
[email protected] bug reproduction
FROM node:10.14
RUN mkdir /app
WORKDIR /app
RUN npm install [email protected]
COPY ./index.js .
CMD node ./index.js
const flatten = <T>(arr: T[][]): T[] => ([] as T[]).concat(...arr);
test("flatten", () => {
expect(flatten([[1], [2], [3, 4]])).toEqual([1, 2, 3, 4]);
});
const groupBy = <T extends { [k in K]: string }, K extends string>(
arr: T[],
attr: K,
) => {
return arr.reduce<{ [k in T[K]]?: T[] }>((result, item) => {
const k = item[attr];
if (result[k] === undefined) {
result[k] = [];
}
(result[k] as T[]).push(item);
const uniq = <T>(arr: T[], fn: (el: T) => Object = (a) => a) =>
arr.reduce<T[]>(
(acc, curr) => (acc.some((a) => fn(a) === fn(curr)) ? acc : [...acc, curr]),
[],
);
test("uniq", () => {
const deduped = uniq([{ a: 2 }, { a: -2 }], (el) => Math.abs(el.a));
expect(deduped).toEqual([{ a: 2 }]);
});
@dir01
dir01 / removeRedundantPoints.ts
Created April 9, 2019 09:29
Given an array of points, remove those, which are placed on a straight (and almost straight) line between two neighbor points
type Point = {
$type: "PlainPoint";
x: number;
y: number;
};
const removeRedundantPoints = (points: Point[]) => {
const redundantIndexes: number[] = [];
for (let i = 0; i < points.length - 2; i++) {
# -*- coding: utf8 -*-
from datetime import timedelta
from fabric.context_managers import settings
from fabric.contrib.files import sed, exists
from fabric.operations import sudo, run, put
from fabric.state import env
useful_software_list = [
'htop',
@dir01
dir01 / fake_mail.py
Created October 3, 2012 10:12
Fake SMTP server for testing purposes
# -*- coding: utf8 -*-
import base64
import os
import tempfile
from smtpd import DebuggingServer
from json import loads, dumps
class TestSmtpServer(DebuggingServer):
@dir01
dir01 / dynamic_form.py
Created August 17, 2012 10:03
Django dynamic forms
import unittest2
from django import forms
class DynamicForm(forms.Form):
"""
Allows to alter fields amount and requireness via spec, e.g.:
form = DynamicFormSubclass(spec={
'last_name': False, # Drops field from initial form
'gender': {'required': True},
@dir01
dir01 / interface_proxy.py
Created March 29, 2012 09:06
Interface proxy
# -*-coding: utf8 -*-
import unittest2
from zope.interface import Interface
from zope.interface.declarations import implements, classImplements
class Storage(object):
CLASS_TEMPLATE = """
class %(class_name)s(object):
def __init__(self, instance):