Skip to content

Instantly share code, notes, and snippets.

@joseanpg
Created August 21, 2011 15:10
Show Gist options
  • Save joseanpg/1160724 to your computer and use it in GitHub Desktop.
Save joseanpg/1160724 to your computer and use it in GitHub Desktop.
//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;
}
return acumulator;
}
console.log(checkAccessor1('obj.uno.dos.tres.prop'));
//With foldl1
var checkAccessor2 = function(accesor) {
var steps = accesor.split('.');
var cursor = steps[0];
return steps.slice(1).reduce(function(acumulator,current,index){
cursor += '.' + current;
return acumulator + ' && ' + cursor;
},cursor);
}
console.log(checkAccessor2('obj.uno.dos.tres.prop'));
//With foldl
var checkAccessor3 = function(accesor) {
var steps = accesor.split('.');
var cursor = '';
return steps.reduce(function(acumulator,current,index){
if (index === 0) {
cursor = current;
acumulator = current;
}
else {
cursor += '.' + current;
acumulator += ' && ' + cursor;
}
return acumulator;
},'');
}
console.log(checkAccessor3('obj.uno.dos.tres.prop'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment