This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 $* | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Example; | |
interface Iterable { | |
/** | |
* @return Iterator iterator over all items | |
*/ | |
public function items(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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*/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$error = new My_API_Application_Response(My_API_Application_Response::FOOLISH_HUMAN_ERROR); | |
return $error; | |
// Oh no! I've typed My_API_Application_Response twice. This feels wrong! | |
// How about this? | |
return My_API_Application_Response::FOOLISH_HUMAN_ERROR(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var root = template: "something", data: {}, children: {} | |
function render(obj) | |
partials = {} | |
for each name, child in obj.children | |
partials[name] = render(child) | |
return mustache(obj.template, obj.data, partials) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app.use(require('connect-flash')()); | |
// Expose the flash function to the view layer | |
app.use(function(req, res, next) { | |
res.locals.flash = function() { return req.flash() }; | |
next(); | |
}) |