Skip to content

Instantly share code, notes, and snippets.

@robbywashere-zz
robbywashere-zz / email_validator.go
Last active November 2, 2019 06:14
Email validation JSON endpoint, any error is returned as `code` non-zero value with message else "ok"
package main
import (
"flag"
"fmt"
"net"
"net/http"
"net/smtp"
"regexp"
"strings"
@robbywashere-zz
robbywashere-zz / erato.js
Created March 11, 2019 01:45
The Sieve of Eratosthenes in Javascript
function erato(n){
let map = new Map((new Array(n-1).fill(1)).map((n,i)=>[i+2,true]));
for (let i = 2; i <= Math.sqrt(n);i++){
if (!map.get(i)) continue
for (let j = i*2; j <= n; j += i) map.set(j,false)
}
return Array.from(map).filter(([n,b])=>b).map(([n])=>n);
}
console.log(erato(100))
@robbywashere-zz
robbywashere-zz / rx.js
Last active February 17, 2019 06:06
simple wild card regex implementation in javascript (interview question)
/*
Implement regular expression matching with the following special characters:
* . (period) which matches any single character
* * (asterisk) which matches zero or more of the preceding element
That is, implement a function that takes in a string and a valid regular
@robbywashere-zz
robbywashere-zz / lru.ts
Last active February 13, 2019 23:47
simple LRU cache typescript implementation
import { strict as assert } from "assert";
type Node<V> = { parent?: Node<V>; next?: Node<V>; value: V };
class DoublyLinkedList<V> {
private _head?: Node<V>;
private _tail?: Node<V>;
get head() {
return this._head!;
@robbywashere-zz
robbywashere-zz / swagger2ts.js
Created February 9, 2019 00:31
Convert Swagger 2 spec to typescript superagent api client
function typify(spec, reqParams) {
return walk(null, spec, new Set(reqParams));
function walk(name, prop, req = new Set()) {
const {
name: propName,
type,
items,
required = [],
function monthDays(year){
const m = (new Array(12)).fill(1).map((_,x)=>x%2?30:31);
if (year % 4) m[1] = 28;
else if (year % 100) m[1] = 29;
else if (year % 400) m[1] = 28
else m[1] = 28;
return m;
}
@robbywashere-zz
robbywashere-zz / varidiac.ts
Created January 23, 2019 00:18
combine functions with varidiac rest parameters types
function combine<T extends any[]>(...args: ((...fnArgs: T) => void)[]) {
return (...a: T) => args.forEach(fn => fn(...a));
}
function foo(n: number, o: string){
console.log(`string ${n} ${o}`);
}
function bar(n: number, o: string){
class Graph {
constructor(){
this.nodes = {};
}
addNode(n){
if (!this.nodes[n]) this.nodes[n] = [];
}
class Graph {
constructor(){
this.nodes = {};
}
addNode(n){
if (!this.nodes[n]) this.nodes[n] = [];
}
addDirEdge(a,b){
@robbywashere-zz
robbywashere-zz / json_parser.js
Last active January 15, 2019 22:16
a (very naive) json parser in javascript
function parse_json(jsonStr) {
let tokens = [];
for (let i = 0; i < jsonStr.length; i++) {
if (jsonStr[i] === '"') {
value = '';
i++ ;
while (jsonStr[i] !== '"') {
value += jsonStr[i];