Skip to content

Instantly share code, notes, and snippets.

@mudge
mudge / uri.rb
Created February 27, 2015 12:30
A helper class to parse MongoDB connection strings
module MongoDB
class URI
attr_reader :uri
def initialize(uri)
@uri = uri
end
def username
matches[:username]
@mudge
mudge / hash.rb
Last active August 29, 2015 14:10
Copying Clojure's IFn interface for Set#to_proc and Hash#to_proc. More details at http://mudge.name/2014/11/26/data-structures-as-functions.html
class Hash
def to_proc
method(:[]).to_proc
end
end
%w(a b c d).map(&{"a" => 1, "b" => 2, "c" => 3})
# => [1, 2, 3, nil]
# Or, a more readable example:

Keybase proof

I hereby claim:

  • I am mudge on github.
  • I am mudge (https://keybase.io/mudge) on keybase.
  • I have a public key ASBDGnU-C3RqiI8Qhc2PUEsdVcNTtf5avLN5urIUcEbzOwo

To claim this, I am signing this object:

@mudge
mudge / EachCons.php
Created July 17, 2014 14:32
Iterate over an array as a sliding window of element pairs.
<?php
$a = array(1, 2, 3);
$pairs = array_map(null, $a, array_slice($a, 1));
/**
* Prints:
* (1,2)(2, 3)(3, )
*/
foreach ($pairs as list($x, $y)) {
echo "({$x}, {$y})";
@mudge
mudge / FlatMapIterator.php
Last active August 29, 2015 14:04
Fun with PHP's Iterator classes to implement a lazy flat map.
<?php
/* From https://github.com/guzzle/iterator/blob/master/MapIterator.php */
class MapIterator extends \IteratorIterator
{
/** @var mixed Callback */
protected $callback;
/**
* @param \Traversable $iterator Traversable iterator
@mudge
mudge / gist:8832334
Last active August 29, 2015 13:56
A functional conundrum (that isn't PHP specific).
<?php
/* Given an anonymous function with a previously unspecified number of arguments like so: */
$f = function ($x, $y, $z) {
/* Do something interesting with $x, $y and $z. */
return;
};
/* How can I convert it into the following nested function (each function yielding one argument),
* finally calling $f when there are enough arguments?
*/
@mudge
mudge / canalis.js
Created August 7, 2013 18:16
A work in progress.
/* go(function () {
* console.log('Foo');
* });
*/
var go = function (f) {
if (typeof process.nextTick === 'function') {
process.nextTick(f);
} else if (typeof setImmediate === 'function') {
setImmediate(f);
} else {
@mudge
mudge / cidr.php
Last active December 20, 2015 12:29
A simple function to determine whether an IP is within a CIDR range or not.
<?php
/* A simple function to determine whether an IP is within a range
* or not.
*
* Examples:
*
* withinCIDR('1.2.3.4', '1.2.3.0/24');
* => 1
*
* withinCIDR('1.2.3.4', '1.2.3.4');
@mudge
mudge / gist:6062586
Last active June 5, 2016 15:10
Convert Elasticsearch search output into bulk format.
paste -d '\n' <(curl 127.0.0.1:9200/collection/_search | jq -c '.hits.hits[] | {"index": {_index, _type, _id}}') <(curl 127.0.0.1:9200/collection/_search | jq -c '.hits.hits[]._source')
@mudge
mudge / echo.py
Created July 15, 2013 09:06
An extremely simple HTTP echo server in Python for testing requests.
import socket
port = 3001
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('localhost', port))
s.listen(5)
while 1:
client, address = s.accept()
data = client.recv(1024)
if data: