Skip to content

Instantly share code, notes, and snippets.

@donabrams
donabrams / deepProxy.js
Last active April 20, 2018 18:28
Deep Read Only
const a = {
foo: "bar",
beep: {
boop: 1,
},
baz: [
true,
]
};
@donabrams
donabrams / choice.js
Last active November 20, 2017 20:40
choice.js - OOP Either
const choice = (decision) => {
let yes = () => {};
let no = () => {};
const a = {
yes: (f) => { yes = f; return a; },
no: (f) => { no = f; return a; },
decide: () => {
const next = decision() ? yes() : no();
return next.decide ? next.decide() : next;
}
@donabrams
donabrams / memoize.js
Last active November 9, 2017 16:24
Memoizer
let set = (cache, val, k, ...rest) => {
if (rest && rest.length) {
if (!cache.has(k)) {
cache.set(k, new Map());
}
set(cache.get(k), val, ...rest);
} else {
cache.set(k, val);
}
};
1. Choose your boundary
2. Identify state
3. Test *just* the boundary
4. Any test that will be maintained (non-TDD) should have a positive and negative test
5. Unit tests can be thrown away at will
6. DRY/inheritance is usually bad in test code, especially if stateful
7. Try to colocate data and code as much as possible
8. Make randomness and time explicit
9. It’s best to write tests FIRST
@donabrams
donabrams / mock-api-gateway.hs
Created June 12, 2017 14:30
Mock API Gateway signature
start :: ApiGateway -> Request -> Response
create :: forall i, o.
{
method,
path,
intReq: Async (HTTPRequest -> i),
lambda: Async (i -> Tuple o Status),
intRes: Status -> o -> Async HTTPResponse
} -> ApiGateway
-- problems: does o differ per status but need to be synced between 7/8?
@donabrams
donabrams / mistate.md
Last active May 27, 2017 17:15
First attempt at spec-ing out a mistate data structure

mistate

A mistate is a Javascript data structure with a schema, underlying immutable data structure, lensing, and efficient subscriber abilities.

Schema

The schema format is the same as GraphQL.

Here's an example:

@donabrams
donabrams / example.js
Last active April 21, 2017 15:06
Monad-ish Maybe in JS
var good = {a: {b: {c: 1}}}
console.log(new Maybe(good).bind(t=>t.a).bind(t=>t.b).bind(t=>t.c).getOrElse(2))
var bad = {a: 3}
console.log(new Maybe(bad).bind(t=>t.a).bind(t=>t.b).bind(t=>t.c).getOrElse(2))
@donabrams
donabrams / navigator.md
Last active April 10, 2017 17:35
navigator deep dive

What's navigator'?

navigator' :: forall r eff. Prop (NavigatorProps r eff) -> r -> SceneRenderer r -> ReactElement
navigator' p initialRoute renderScene = navigatorU $ unsafeApplyProps {initialRoute, renderScene} p

Wrapper around https://facebook.github.io/react-native/docs/navigator.html and the unsafe constructor navigatorU.

@donabrams
donabrams / A.graphql
Created January 18, 2017 15:33
GraphQL API Design Interview Question
stream {
... on Text {
html
}
... on Photo {
url
}
... on CodeSnippet {
embedSrc
}
@donabrams
donabrams / ImmutableEx1.js
Last active September 20, 2016 21:54
React/Redux w/ Immutable.js
import { Map, is } from 'immutable'
import { createStore, bindActionCreators } from 'redux'
import { connect } from 'react-redux'
import { pure } from 'recompose'
// ACTIONS
const INCREMENT_ACTION = "INCREMENT_ACTION"
const LOAD_AUTHOR = "LOAD_AUTHOR"
const increment = () => ({type: INCREMENT_ACTION})
const loadAuthor = (id, data) => ({type: LOAD_AUTHOR, id, ...data})