Skip to content

Instantly share code, notes, and snippets.

View glenjamin's full-sized avatar

Glen Mailer glenjamin

View GitHub Profile
@glenjamin
glenjamin / apply_merge.clj
Created March 19, 2013 21:55
Helpful function for dealing with ring request maps?
(defn ->merge [m & fns]
"Merge the recursive result of calling f on m into m
Useful when dealing with operations on ring request maps"
(loop [m m
fns fns]
(if-not (seq fns)
m
(recur (merge m ((first fns) m)) (rest fns)))))
(assert (= {:a 1 :b 2}
@glenjamin
glenjamin / semi-colon.js
Created August 10, 2013 10:27
Semi-colons I consider extraneous noise in JavaScript
// When assigning a function to a variable
var times = function(n, f) {
for (var i=0; i<n; ++i) {
f();
}
}/*here*/
// When there's only one expression in a function
function(callback) {
doSomething(function(err, n) { callback(err, n * 2)/*here*/ })/*and here*/
@glenjamin
glenjamin / Iterable.php
Last active December 24, 2015 03:59
Shared behaviours in PHPUnit with Traits
<?php
namespace Example;
interface Iterable {
/**
* @return Iterator iterator over all items
*/
public function items();
@glenjamin
glenjamin / slowish.js
Last active December 27, 2015 12:29
How to optimise callbacks that need state
var app = express();
database.connect(function(err, db) {
app.db = db;
app.listen(1337);
})
app.get('/stuff/:id', stuffHandler);
function stuffHandler(req, res) {
app.db.fetch(req.params.id, function datastoreCallback(err, data) {
@glenjamin
glenjamin / all-buffered-io-ever.js
Created November 6, 2013 08:37
What's the "fast" way to read all the data from a socket?
// Basically every data read ever looks something like this
// Can this be optimised using the callback advice from http://blog.trevnorris.com/2013/08/long-live-callbacks.html
function getData(callback) {
http.get('http://localhost/whatever', function(err, res) {
if (err) return callback(err);
res.on('error', callback);
var buffers = [], length = 0;
res.on('data', function(chunk) {
@glenjamin
glenjamin / water.clj
Last active December 28, 2015 04:59
*That* interview question, explored
(ns water
(require [clojure.string :as s]))
(defn right-reductions [f l]
"just like (reductions) but form the right"
(reverse (reductions f (reverse l))))
(defn transpose [m]
"Transpose a list of lists"
(apply mapv vector m))
@glenjamin
glenjamin / pgk.sh
Last active December 30, 2015 17:09
Portable shell function to neatly wrap the pgrep/pkill combo
# List processes via pgrep, then prompt to pkill
pgk() {
[ -z "$*" ] && echo 'Usage: pgk <pattern>' && return 1
pgrep -fl $*
[ "$?" = "1" ] && echo 'No processes match' && return 1
echo 'Hit [Enter] to pkill, [Ctrl+C] to abort'
read && pkill -f $*
}
@glenjamin
glenjamin / objectZip.js
Created December 10, 2013 17:36
objectZip iterator for javascript
var _ = require('underscore');
/**
* functional zip() for JS objects
*
* Skips keys not present in both sides
*
* @param a left object
* @param b right object
* @param f(key, aval, bval) iterator
@glenjamin
glenjamin / test.php
Created December 12, 2013 17:38
PHPUnit data providers can have test docs too!
<?php
public function providerGetEventId() {
return array(
'wrong way around' => array(
'Premier League',
array('id' => 5,'name' => 'Hull', 'shortname' => 'HUL'),
array('id' => 6,'name' => 'Manchester City', 'shortname' => 'MNC'),
null
),
'different event type' => array(
@glenjamin
glenjamin / pool-transaction.js
Last active October 22, 2021 00:46
DB transaction from a connection pool in node-mysql
var mysql = require('mysql');
var pool = mysql.createPool('mysql://localhost');
inTransaction(pool, function(db, next) {
db.query("DELETE * FROM stuff", function(err) {
if (err) return next(err);
db.query("INSERT INTO stuff VALUES (1,2,3)", function(err) {
return next(err);