Skip to content

Instantly share code, notes, and snippets.

Avatar

yorbb robbywashere

View GitHub Profile
@robbywashere
robbywashere / email_validator.go
Last active Nov 2, 2019
Email validation JSON endpoint, any error is returned as `code` non-zero value with message else "ok"
View email_validator.go
package main
import (
"flag"
"fmt"
"net"
"net/http"
"net/smtp"
"regexp"
"strings"
@robbywashere
robbywashere / erato.js
Created Mar 11, 2019
The Sieve of Eratosthenes in Javascript
View erato.js
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))
View firstSundays.js
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
robbywashere / rx.js
Last active Feb 17, 2019
simple wild card regex implementation in javascript (interview question)
View rx.js
/*
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
robbywashere / lru.ts
Last active Feb 13, 2019
simple LRU cache typescript implementation
View lru.ts
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
robbywashere / swagger2ts.js
Created Feb 9, 2019
Convert Swagger 2 spec to typescript superagent api client
View swagger2ts.js
function typify(spec, reqParams) {
return walk(null, spec, new Set(reqParams));
function walk(name, prop, req = new Set()) {
const {
name: propName,
type,
items,
required = [],
@robbywashere
robbywashere / varidiac.ts
Created Jan 23, 2019
combine functions with varidiac rest parameters types
View varidiac.ts
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){
@robbywashere
robbywashere / json_parser.js
Last active Jan 15, 2019
a (very naive) json parser in javascript
View json_parser.js
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];
View bfs.js
class Graph {
constructor(){
this.nodes = {};
}
addNode(n){
if (!this.nodes[n]) this.nodes[n] = [];
}
View topo_sort.js
class Graph {
constructor(){
this.nodes = {};
}
addNode(n){
if (!this.nodes[n]) this.nodes[n] = [];
}
addDirEdge(a,b){
You can’t perform that action at this time.