Exercise 1: skips :: [a] -> [[a]]
The nth list in the output should contain every nth element from the input list. For example, skips "hello!" == ["hello!", "el!", "l!", "l", "o", "!"].
The question UPenn Homework 3: skips function and it's associated answers inspired me to write a solution in Javascript. I used the opportunity to (finally) better understand high order array functions. If you would be so kind to critique:
- Code Layout, specifically if nesting
function skips()
makes any sense and why. - Do higher order functions provide more than just readability, are they more efficient?
- Can I make the nested
function skips()
better (readable/faster)? - Anything at all.
function skip(str, cb) {
var input = str.split('');
cb(input.map(skips));
function skips(val, index, arr) {
var res = [];
var i = index;
var iterator = i + 1;
for (i; i < arr.length; i += iterator){
res.push(arr[i]);
}
return res;
}
}
skip('hello!', console.log);