Last active
August 29, 2015 14:05
-
-
Save nicohvi/fe856380c847283c5297 to your computer and use it in GitHub Desktop.
Functional programming
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
function whosTopDog(dogs, comparison) { | |
for (var i=0; i < dogs.length; i++) comparison(dogs[i]); | |
} | |
whosTopDog(['ice cube', '50 cent', 'snoop'], function(dog) { | |
if(dog === 'snoop dog') { | |
console.log(dog+ ' is top deg.'); | |
} | |
else { | |
console.log(dog+ ' is a bitch.'); | |
} | |
}); |
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
array = ['ice cube', '50 cent', 'snoop'] | |
map(array, function(value, index) { | |
return value+ ' has index: ' +index+ ' in the array.'; | |
}) |
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
var map = function(items, fn) { | |
var newArray = []; | |
for(var i=0; i < items.length; i++) newArray.push(fn(items[i], i)); | |
return newArray; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment