Skip to content

Instantly share code, notes, and snippets.

View greim's full-sized avatar

Greg Reimer greim

View GitHub Profile
@greim
greim / gist:2dcedcf3c42ad6b5258f
Created February 9, 2015 16:36
Binary walk function
/*
* Home in on a number in binary search fashion.
* binWalk(2048) === 1024
* binWalk(2048, -1) === 512
* binWalk(2048, -1, -1) === 256
* binWalk(2048, -1, -1, -1) === 128
* binWalk(2048, -1, 1) === 1536
*/
function binWalk(n){
@greim
greim / soft-model.js
Created April 3, 2012 19:09
Backbone.SoftModel
/**
Extend Backbone.SoftModel and then bind to the 'soft:change' event.
It will only fire once given a sequence of rapid-fire changes.
This avoids for example having multiple redundant renders,
which can be a performance killer. That way you don't have to
splatter your code with speculative {silent:true}s.
Warning!
This code is untested. This is just exploring an idea.
*/
@greim
greim / gist:8221971
Created January 2, 2014 16:36
I'm building a decorative wooden header for some shelves with arches cut out at the top. The board was 5" wide and I wanted the arch to be half that, so I wrote this program to calculate the radius.
/*
For an arch with a rise of X, what's the radius.
Start with this right triangle:
b
|
a----------------c
a = left edge of arch
@greim
greim / queue.elm
Created June 24, 2016 20:26
Queue data structure in Elm
type Queue a
= Empty
| Mono a
| Multi a (Queue a) a
enq : a -> Queue a -> Queue a
enq val queue =
case queue of
Empty ->

Elm: Supplementary Notes

In which I get angry because some aspect of Elm seems... well, weird to me, and the docs aren't helping, so I jot down these notes in order to force myself to better grasp the topic, because writing forces me to think deeply about things in a way that I'm incapable of doing otherwise gaaasspp

Grokking Elm Functions

Background: In Elm, functions are pure. Given the same input, they'll always return the same output. That isn't the case in JavaScript, where for example a function can return different values on successive calls, like Math.random() or Date.now().

Thus, a function that takes zero args is conceptually no different than a variable. A variable holds a value. A zero-arg function only returns one possible value. This is reflected in Elm syntax. Look at how you annotate and declare a variable in Elm:

Elm: Supplementary Notes

In which I get angry because some aspect of Elm seems... well, weird to me, and the docs aren't helping, so I jot down these notes in order to force myself to better grasp the topic, because writing forces me to think deeply about things in a way that I'm incapable of doing otherwise gaaasspp

Grokking Elm Components

The Elm architecture is cool because it's fractal. That is, you can break your program into components, each of which is a mini-expression of the overall Elm architecture. The Elm architecture is... turtles.

How is this done? I don't know! I'll figure it out and come back!

// created by greg reimer ("gregreimer" at gmail) http://obadger.com/
function createHistogram(rFunc) {
var arr = [];
var size = 30;
for (var i=0; i<size; i++){
arr[i] = 0;
}
for (var i=0; i<1000000; i++){
arr[Math.floor(rFunc() * size)]++;
@greim
greim / data-vacancies.js
Last active November 11, 2017 00:11
Data Vacancies
vacancies.listen('relatedProducts/:productId', async function(params, send) {
const id = params.productId;
const resp = await fetch(`/products/${id}/related`);
const products = await resp.json();
send({ type: 'RECEIVE_RELATED_PRODUCTS', id, products });
});
@greim
greim / fetch-logic-2.jsx
Last active November 11, 2017 16:47
Fetch Logic 2
function ProductDetailPage({ viewportWidth, relatedProductInfo }) {
const hasRealestate = viewportWidth > 800;
const hasData = relatedProductInfo === undefined;
return <div>
...
{ hasRealestate &&
( hasData
? <RelatedProducts products={relatedProducts}/>
: <div data-vacancy={`relatedProducts/${id}`}>Loading...</div>
)
@greim
greim / fetch-logic.jsx
Last active November 11, 2017 16:48
Fetch Logic
class ProductDetailPage extends React.Component {
componentDidMount() {
...
const hasRealestate = this.props.viewportWidth > 800;
const hasData = this.props.relatedProductInfo === undefined;
if (hasRealestate && !hasData) {
this.props.fetchRelatedProductInfo(this.props.productId);
}
}