Skip to content

Instantly share code, notes, and snippets.

@bodhi
bodhi / output
Last active August 4, 2016 13:29
Toy templating in Go
<div
><div
><img
src="http://lolcats.com/lolcat.jpg" alt="logo"></img>
<h1
>hello</h1>
</div>
<p
>little bobby tables: &lt;script&gt;alert(&#39;xss!&#39;)&lt;/script&gt;</p>
<ul
@bodhi
bodhi / transform.js
Created July 20, 2016 03:29
Map `<A intlId={blah} intlValues={blahVal} />` to `<A><Message id={blah} values={blahVal} /></A>`
function mapIntl(j, path) {
const intlIdAttr = path.value.openingElement.attributes.find(
({
name: { name }
}) => name === 'intlId'
);
const intlValAttr = path.value.openingElement.attributes.find(
({
name: { name }
@bodhi
bodhi / redux-composition.md
Created June 12, 2016 13:32
Quick thoughts on composing parts of Redux

Redux reducers can compose in a couple of different ways:

combineReducers creates a new state tree with the composed reducers owning/controlling leaves (subtrees).

Or a straight merge, something like:

const reducer = (state, action) => ({
    ...reducerA(state, action),
 ...reducerB(state, action)
@bodhi
bodhi / piggyback.js
Created May 12, 2016 04:40
Piggybacking promises
piggyback(fn) => () => {
if (!fn["@@piggyback"]) {
const promise = fn();
fn["@@piggyback"] = promise
promise.then(() => {
delete fn["@@piggyback"];
});
return promise
} else {
return fn["@@piggyback"].then(res => {
@bodhi
bodhi / README.md
Created February 25, 2016 01:38
Redux is a central dispatcher

I want to do something based on an action being dispatched, rather than the state being a given value. For example:

  • Reload the timeline immediately after I log in on my Twitter client
  • Retrieve data about a notification immediately on receiving a notification

I could set up a reactive signal or promise or something like that and pump events into it, and dispatch events into Redux. But I already have a place that I'm dispatching actions into: the Redux store.

It reminds me of Yaron Minsky's blog post with some discussion about edge-triggering vs level-triggering.

(waitForState is a bit overengineered for this example, compared to waitForAction)

@bodhi
bodhi / authed.js
Created January 9, 2016 11:56
Toy auth wrapper component for React+Redux
require('normalize.css');
require('styles/App.css');
import React from 'react';
import { connect, Provider } from 'react-redux';
import { createStore } from 'redux';
const store = createStore((s, a) => {
switch (a.type) {
case "LOG_IN":
package main
import (
"bufio"
"fmt"
"os"
"regexp"
"strconv"
"strings"
)
@bodhi
bodhi / README.md
Last active August 29, 2015 14:26
The Telegram Problem

Implement the function handle to accept lines of text and output reformatted text, wrapped at columnWidth characters, without breaking words.

So

Yr brunch Godard, readymade pug Pinterest gastropub put a bird on it Tumblr.
Sartorial swag beard, selvage bitters tofu vinyl.
Tattooed kogi organic scenester heirloom, cred four loko cardigan cray meh Portland master cleanse photo booth Shoreditch farm-to-table.

when wrapped at 30 characters becomes

RULES = {
"A" => [[1, 50],
[3, 130]],
"B" => [[1, 30],
[2, 45]],
"C" => [[1, 20]],
"D" => [[1, 15]]
}
@bodhi
bodhi / gist:3fde34d207d7d36532fe
Created March 25, 2015 02:50
I can never remember the scoping rules for Ruby, so here's an example to remind me.
module A
module X
def x
"X::x"
end
end
end
# Won't search in A for X, so need to scope it.
class A::B