Skip to content

Instantly share code, notes, and snippets.

@xphere
xphere / robot.js
Created December 21, 2012 17:57
js[CODE].is('Fun');
// SimpleRand as part of C implementation
// Usage:
// s = new SimpleRand(seed);
// v = s.rand(5, 10);
function SimpleRand(randState) {
var u;
function next() {
if (randState === u) {
randState = 54819;
}
// Based on idea of http://millermedeiros.github.com/js-signals/
function Signal(context, remember) {
this._bindings = [];
this._sorted = true;
this._erased = false;
this._remember = !!remember;
this._params = null;
this._propagate = false;
this._context = context || this;
// Binds a method to its owner, optionally adding more arguments
function bindMethod(_this, method) {
method = _this[method];
if (arguments.length <= 2) {
return method.bind(_this);
}
var args = Array.prototype.slice.call(arguments, 2);
return args.unshift(_this), method.bind.apply(method, args);
}
@xphere
xphere / extend.js
Created December 12, 2012 02:05
Extend JS
function extend(object) {
if (arguments.length > 1) {
for (var i = 1; i < arguments.length; ++i) {
for (j in arguments[i]) {
object[j] = arguments[i][j];
}
}
}
return object;
}
@xphere
xphere / Event.js
Created December 11, 2012 16:35
EventDispatcher JS
function Event() {
}
extend(Event.prototype, {
propagationStopped: false,
stopPropagation: function() {
this.propagationStopped = true;
return this;
},
isPropagationStopped: function() {
@xphere
xphere / inherit.js
Created December 11, 2012 16:30
Inherit JS
// Makes fn inherit from parent class
// Allows "super" calls ONLY in the constructor via Class.parent(this, param1, param2, ...)
// Also allows to extend the prototype giving more arguments
function inherit(parent, fn) {
fn || (fn = function() { });
fn.prototype = Object.create(parent.prototype);
for (var i = 2; i < arguments.length; ++i) {
extend(fn.prototype, arguments[i]);
}
fn.prototype.constructor = fn;
$observableUniverse = new Location('Universo observable');
$virgoSuperCluster = new Location('Supercúmulo de Virgo', $observableUniverse);
$localGroup = new Location('Grupo Local', $virgoSuperCluster);
$milkyWay = new Location('Via Lactea', $localGroup);
$orionArm = new Location('Brazo de Orión', $milkyWay);
$solarSystem = new Location('Sistema Solar', $orionArm);
$earth = new Location('La Tierra', $solarSystem);
$europe = new Location('Europa', $earth);
$spain = new Location('España', $europe);
$catalonia = new Location('Catalunya', $spain);
@xphere
xphere / google-maps-geocode.php
Last active October 12, 2015 12:18
Usage of Google\Maps\Geocode
<?php
$geocode = new \Google\Maps\Geocode\GeocodeService();
$cachePath = $this->container->getParameter('kernel.root_dir') . '/../cache';
$cache = new \Doctrine\Common\Cache\FilesystemCache($cachePath);
$cacheAdapter = new \Guzzle\Cache\DoctrineCacheAdapter($cache);
$cachePlugin = new \Guzzle\Plugin\Cache\CachePlugin($cacheAdapter);
$geocode->getClient()->addSubscriber($cachePlugin);
@xphere
xphere / MenuPlannerAPI.php
Last active October 12, 2015 03:38
Menu Planner API
$planner = new Planner();
$planner
->menu('menu')
->avoid('ingredient:sugar')
->avoid('type:fish')
->day('monday')
->meal('breakfast')
->dish('snack')->end()
->end()
->meal('dinner')
@xphere
xphere / gist:3907873
Created October 17, 2012 20:15
How to start a new Symfony2 project easily with Composer
# Download composer as explained in http://getcomposer.org/download
> curl -s https://getcomposer.org/installer | php
# Create a new Symfony2 project
> composer.phar create-project symfony/framework-standard-edition directory