Skip to content

Instantly share code, notes, and snippets.

Avatar

Tim Ruffles timruffles

View GitHub Profile
@timruffles
timruffles / observables.js
Created Jan 23, 2017
Functional Observables
View observables.js
{
/*
interface Observer<T> {
next(t: T): void;
error(e: Error): void
complete(): void;
}
*/
View hoist.js
// 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 Dec 2, 2016
mapValues -> Functor for functions
View 01_mapValues.js
// (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 Jul 13, 2016
proper auth handling
View auth.md

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 Jun 22, 2016
empirical birthday paradox calculator - for hashes
View bday-paradox.js
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 Jun 16, 2016
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
View dumb-es6-bench.js
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 Jun 7, 2016
functor in TypeScript - this is probably in violation of many functor laws :/
View functors.ts
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 Jun 2, 2016
DI taste question
View di-taste.md

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
(tail) recursive solution to giving change
View recursive-change.js
"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,
You can’t perform that action at this time.