Skip to content

Instantly share code, notes, and snippets.

@joseanpg
joseanpg / crearSaludos.js
Created August 21, 2011 19:41
GEJS-4 crearSaludos.js Soluciones
/*********************************************************************
Solución funcional
La más elegante, y ahora todos sabemos ;)
*********************************************************************/
function crearSaludos1(array) {
return array.map(function(nombre){
@joseanpg
joseanpg / crearSaludo_version_incorrecta.js
Created August 21, 2011 18:42
GEJS-4 crearSaludos.js Versión (deliberadamente) incorrecta
function crearSaludo(array) {
var resultado = [];
for (var j=0, len = array.length; j<len; j++) {
var nombre = array[j];
resultado.push(function(){
console.log("Hola " + nombre);
});
}
return resultado;
}
//Imperative
var checkAccessor1 = function(accesor) {
var steps = accesor.split('.');
var cursor = steps[0];
var acumulator = cursor;
for (var j=1, len = steps.length;j<len;j++) {
cursor += '.' + steps[j];
acumulator += ' && ' + cursor;
}
@joseanpg
joseanpg / memorizaParametro_partial.js
Created August 21, 2011 09:35
GEJS-4 memorizaParametro (aplicación parcial)
Function.prototype.memorizaParametro = function(parametro) {
var funcionSobreLaQueSeEjecuto_memorizaParametro = this;
return function() {
var argsArray = Array.prototype.slice.call(arguments,0);//Convertimos en array
argsArray.unshift(parametro);
return funcionSobreLaQueSeEjecuto_memorizaParametro.apply(this,argsArray);
}
}
//Primer ejemplo: uso puramente funcional
@joseanpg
joseanpg / memorizaParametro.js
Created August 21, 2011 09:22
GEJS-4 memorizaParametro
Function.prototype.memorizaParametro = function(parametro) {
var funcionSobreLaQueSeEjecuto_memorizaParametro = this;
return function() {
var argsArray = Array.prototype.slice.call(arguments,1); //Menos el primero
argsArray.unshift(parametro);
return funcionSobreLaQueSeEjecuto_memorizaParametro.apply(this,argsArray);
}
}
//Primer ejemplo: uso puramente funcional
@joseanpg
joseanpg / dangerZone.js
Created August 19, 2011 09:21
Asignación por defecto mediante || (GEJS tercera reunión)
/*
* El siguiente patrón
*
* function foo(param) {
* param = param || valor_por_defecto
* ...
*
* no representa ningún riesgo de cara al rendimiento.
*
* Con ECMAScript 5 tenemos la posibilidad
@joseanpg
joseanpg / regex_vocalizeitor.js
Created August 16, 2011 16:51
GEJS Vocales (Con RegEx)
// FUNCIONES AUXILIARES
/*
* La siguiente función cuenta las propiedades
* (tanto propias como hereradas) de un objeto
*/
function contarPropiedades(obj) {
var recuento = 0;
for (propiedad in obj) recuento ++;
document.documentElement.childNodes[0].innerHTML = '';
var pres = document.getElementsByTagName('PRE');
for(var j = 0, len = pres.length; j<len;j++) {
var sty= pres[j].style;
sty.border='1px solid black';
sty.padding='8px';
sty.marginLeft='32px';
}
var df = document.createDocumentFragment();
-- Estrategias para aplanar arboles
-- ATENCIÓN: Estas nuevas funciones no han sido probadas
-- (No tengo GHC disponible en esta maquina)
module Aplaneitor where
data Tree a = Leaf a | Trees [Tree a] deriving(Show)
cow = Trees [Leaf 1, Trees [ Leaf 2, Leaf 3], Leaf 4]
-- Estrategias para aplanar arboles
@joseanpg
joseanpg / gist:1146822
Created August 15, 2011 14:06 — forked from juandopazo/gist:1146157
Concatenar arrays anidados sin recursividad
function esArray(array) {
return Object.prototype.toString.call(array) == '[object Array]');
}
function desempaqueta(array,index) {
Array.prototype.splice.apply(array, [index, 1].concat(array[index]));
}
function flatten(arr) {
arr = arr.concat();