Created
October 26, 2016 13:54
-
-
Save yvan-sraka/aeaa4af71d81f1420901eb6a4c7f0157 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// Definition of Map function | |
function Map(arr, func) { | |
m_arr = []; | |
for (var i = 0; i < arr.length; ++i) | |
m_arr.push(func(arr[i])); | |
return m_arr; | |
} | |
// Definition of Filter function | |
function Filter(arr, func) { | |
f_arr = []; | |
for (var i = 0; i < arr.length; ++i) | |
if (func(arr[i])) | |
f_arr.push(arr[i]); | |
return f_arr; | |
} | |
/** EXAMPLES (with tasty juicy fruits) **/ | |
var T = ["banana", "apple", "strawberry", "melon", "pineapple", "bob"]; | |
console.log(T); | |
T = Filter(T, function (x) { | |
return x.indexOf("a") > - 1; | |
}); | |
console.log(T); | |
T = Map(T, function (x) { | |
return x + x; | |
}); | |
console.log(T); | |
T = Filter(T, function (x) { | |
return x.length > 15; | |
}); | |
console.log(T); | |
T = Map(T, function (x) { | |
return x + "^^"; | |
}); | |
console.log(T); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment