Skip to content

Instantly share code, notes, and snippets.

View timruffles's full-sized avatar

Tim Ruffles timruffles

View GitHub Profile
@timruffles
timruffles / troll_table.sql
Last active June 8, 2017 11:19
I tried to find a limit to Posgres's table+column names. There is none! 👍
scratch=# create table "delete from account;" ("""id""" text primary key, "sutff %\n" text, "&" text, "1" text, U&"\+01F600" text);
CREATE TABLE
scratch=# \d "delete from account;"
Table "public.delete from account;"
Column | Type | Modifiers
-----------+------+-----------
"id" | text | not null
sutff %\n | text |
& | text |
1 | text |
@timruffles
timruffles / observables.js
Created January 23, 2017 12:42
Functional Observables
{
/*
interface Observer<T> {
next(t: T): void;
error(e: Error): void
complete(): void;
}
*/
@timruffles
timruffles / hoist.js
Created January 5, 2017 21:31
hoistage
// Difference between lintability/RTE of hoisting vs non-hoisted code
// ReferenceError: foo is not defined
// at <anonymous>:3:5
try {
foo()
const x = 1;
const foo = () => x;
} catch(e) {
console.log(e)
@timruffles
timruffles / 01_mapValues.js
Last active December 2, 2016 10:43
mapValues -> Functor for functions
// (a -> a) -> (a -> b) -> (a -> b)
var mapValue = transform => fn => v => fn(transform(v));
// (string -> string) - so string is a above
var louder = x => x.toUpperCase();
// (string -> Element) - so element is b above
var hi = (m) => ({ type: 'h1', value: m })
// (string -> Element) - so we end up with a (a -> b) again
var titleLoud = mapValue(louder)(h1);
@timruffles
timruffles / auth.md
Last active July 13, 2016 10:42
proper auth handling

Proper client-side auth

At the mo we have the boilerplate project's getCurrentUser() method, that synchronously returns the user. If it's not fetched yet, bad luck!

This isn't good. We should handle:

  1. waiting to confirm if we're logged in or not. (unless we block the rest of app startup till we know, not too much of a UX cost)
  2. currently this happens at startup
  3. we could embed this on app startup, as we generate the index.html client-side now, e.g window.USER = { id: 15, name: "bob... }
  4. expiration (i.e starting to get 401 Unauthorized responses)
@timruffles
timruffles / bday-paradox.js
Created June 22, 2016 13:35
empirical birthday paradox calculator - for hashes
const hashBits = parseInt(process.env.bits || 32);
const hashValues = Math.pow(2, hashBits);
const checkPoints = Array.from({ length: 9 }, (e, i) => 1 - (i+1)/10);
for(var uniqueHashValuesRemaining = hashValues, p = 1;
p > 0.1;
uniqueHashValuesRemaining -= 1, p *= uniqueHashValuesRemaining/hashValues
@timruffles
timruffles / dumb-es6-bench.js
Last active June 16, 2016 09:29
dumb micro bench of es5 vs es6 key value stores. combined set/get. paste into a console or `pbpaste | node` near you! I got es6: 57.397ms es5: 92.984ms for node 6.0.0
var m = new Map;
var o = {};
var value;
var rs = [];
var i = 1e5;
while(i--) rs.push(Math.random().toString())
var i = 1e5; console.time("es6");
while(i--) {
@timruffles
timruffles / functors.ts
Created June 7, 2016 09:30
functor in TypeScript - this is probably in violation of many functor laws :/
interface Functor<T> {
map(mapper: (x: T) => T): Functor<T>;
ret(x: T): Functor<T>;
}
class Maybe<T> implements Functor<T> {
constructor(private value: T) {}
private nowt() {
@timruffles
timruffles / di-taste.md
Last active June 2, 2016 11:53
DI taste question

In the DI DOCs the 'why' given for the component injector tree is to get multiple instances of a dependency. The editor component explicitly provides RestoreService to ensure it gets its own instance.

                                 providers                   instance of `RestoreService`
rootInjector                     [provide(RestoreService)]   instance A
 - heroEditorComponentInjector   [provide(RestoreService)]   instance B
   - childOfHero                 []                          instance B
 - someOtherEditor               []                          instance A
@timruffles
timruffles / recursive-change.js
Created May 17, 2016 19:43
(tail) recursive solution to giving change
"use strict";
/**
* The function will use a Greedy method to solve the problem of giving
* change.
*
* @param {Integer} the amount for which we have to give change
* @param {Array} the values of the coins that are available
*
* @return {Array} the list of the selected coins, sorted ascending by value,