Created
August 13, 2011 17:34
-
-
Save joseanpg/1144064 to your computer and use it in GitHub Desktop.
This file contains 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
/* Abstracción!!! | |
* | |
* En la función aplaneitor de https://gist.github.com/1143902 | |
* podemos apreciar el esquema foldl (reduce) | |
* En esta versión se ha generalizado dicha función | |
* obteniendo una versión de foldl valido para "árboles de arrays" | |
*/ | |
function foldl(f,acu,soa) { | |
if (soa instanceof Array) { | |
for (var j=0, len = soa.length;j<len;j++) { | |
acu = foldl(f,acu,soa[j]); | |
} | |
} | |
else { | |
acu = f(acu,soa); | |
} | |
return acu; | |
} | |
function phineas(soa,sep) { | |
//"Aplastamos los arrays" | |
var aplastaneited = foldl(function(x,y){x.push(y);return x},[],soa); | |
//Unimos los strings del array aplastado | |
var text = foldl(function(x,y){x=x+sep+y;return x;},'',aplastaneited); | |
//Devolvemos el texto salvo el espurio separador inicial | |
return text.slice(sep.length); | |
} | |
console.log(phineas(['a',['b',['c',['d']]]],'*')); | |
console.log(phineas(["hola", ["soy", ["juan", "fernandez"] ], "y", ["no", "tengo", ["dinero"] ] ],'+')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment