Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / deploy.rb
Created July 24, 2008 09:08
Using acts_as_solr with Monit and Capistrano
before :deploy, "solr:stop"
after :deploy, "solr:start"
namespace :solr do
task :stop, :roles => :app do
run "#{sudo} monit stop solr"
end
task :start, :roles => :app do
run "#{sudo} monit start solr"
@mudge
mudge / nameable.rb
Last active August 29, 2015 14:05
Add a "name" accessor to objects with first_name and last_name fields.
module Nameable
# Assuming first_name and last_name are accessors.
def name=(name)
self.first_name, *_, self.last_name = name.split
end
def name
[first_name, last_name].compact.join(" ")
end
@mudge
mudge / xml.rb
Created July 28, 2008 11:50
Fix for using acts_as_solr with libxml-ruby 0.8.1
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
@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:
@mudge
mudge / averageable.rb
Created July 29, 2008 10:49
Methods to calculate the mean, median and mode of an array.
module Averageable
def mean(&block)
sum(&block) / length.to_f
end
def count(elem)
length - (self - [elem]).length
end
def mode
@mudge
mudge / count.rb
Created July 29, 2008 10:37
A fast count method for Ruby 1.8 arrays.
class Array
def count(elem)
length - (self - [elem]).length
end
end
@mudge
mudge / count_benchmark.rb
Created July 29, 2008 11:10
Comparing two different implementations of a count method for arrays.
require 'benchmark'
class Array
def count_with_loop(elem)
enum = 0
each { |e| enum += 1 if elem == e }
enum
end
def count_without_loop(elem)
@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]