Skip to content

Instantly share code, notes, and snippets.

View nathggns's full-sized avatar

Nate Higgins nathggns

View GitHub Profile
import {inject} from 'aurelia-framework';
import {HttpClient} from 'aurelia-http-client';
@inject(HttpClient)
export class ClusterList {
heading = 'Clusters';
url_base = 'https://foobar:443/v1';
fabrics = [];
constructor(http){
module.exports = React.createClass({
displayName : 'LazyRender',
getInitialState() { return { props : this.props } },
componentWillReceiveProps(newProps) { setTimeout(() => this.setState(newProps), 10) },
shouldComponentUpdate(newProps, newState) { return this.state !== newState; },
render() { return React.cloneElement(React.Children.only(this.props.children), this.state); }
});
@nathggns
nathggns / brainfuck.ts
Created June 7, 2015 04:24
brainfuck in typescript
declare function require(module:string):any;
declare var process:any;
var readline = require('readline');
var fs = require('fs');
type brainfuckDone = (char : string) => void;
type brainfuckInput = (done : brainfuckDone) => void;
function brainfuck(tokens : string[], inp : brainfuckInput) {
@nathggns
nathggns / curry.es6.js
Created June 7, 2015 02:46
Recursive curry functions in ES6
function curry(fn, ...args) {
if (args.length === fn.length) {
return fn(...args);
}
return curry.bind(this, fn, ...args);
}
function add(a, b) {
return a + b;
@nathggns
nathggns / README.md
Last active August 29, 2015 14:22
Class-based injection in typescript

Requires building the master branch of TypeScript, and the reflect-metadata polyfill.

Compile with

node built/local/tsc.js --target ES5 --experimentalDecorators --emitDecoratorMetadata --module commonjs inject.ts
@nathggns
nathggns / await.es6.js
Last active August 29, 2015 14:22
Await implemented in ES6.
function await(generatorFunction) {
let gen = generatorFunction();
/**
* @param {any?} err The error to throw in the generator, where yield was last called.
* @param {any?} result The result to pass to the genarator for the last call to yield
*/
function next(err, result) {
// If the last promise that was yielded was rejected,
// trigger an error inside the generator where yield was last called
@nathggns
nathggns / results2010.json
Last active August 29, 2015 14:21
2010 Election Results
{
"Aberavon": [
{
"name": "(LAB)",
"votes": 16073,
"share": 0.52
},
{
"name": "(LD)",
"votes": 5034,
@nathggns
nathggns / random.es7.js
Last active August 29, 2015 14:21
Demonstration of some cool es7 features.
require('babel/polyfill');
/**
* Imagine this is some complex async process
*/
async function random() {
return new Promise(r => r(Math.random()));
}
async function main() {
@nathggns
nathggns / majority_seats.php
Last active August 29, 2015 14:21
Calculate how many majority seats each party gained
<?php
$json = file_get_contents('https://gist.githubusercontent.com/nathggns/051203c3d7e72159de0d/raw/9e368d3a99a219a7fc487d327b86ebd4f6c8ed53/results.json') or die('Cannot fetch data');
$consts = json_decode($json, true) or die('Cannot decode data');
var_dump(array_reduce(array_map(function($const_name) use ($consts) {
return [$consts[$const_name][0]['name'], $const_name];
}, array_filter(array_keys($consts), function($const_name) use($consts) {
return $consts[$const_name][0]['share'] >= 0.5;
})), function($data, $pair) {
<?php
$json = file_get_contents('https://gist.githubusercontent.com/nathggns/051203c3d7e72159de0d/raw/9e368d3a99a219a7fc487d327b86ebd4f6c8ed53/results.json') or die('Cannot fetch data');
$consts = json_decode($json, true) or die('Cannot decode data');
echo array_reduce(array_values($consts), function($amount, $parties) {
return $amount + ($parties[0]['share'] < 0.5 ? 1 : 0);
}, 0);