Skip to content

Instantly share code, notes, and snippets.

@yvan-sraka
Created October 26, 2016 13:54
Show Gist options
  • Save yvan-sraka/aeaa4af71d81f1420901eb6a4c7f0157 to your computer and use it in GitHub Desktop.
Save yvan-sraka/aeaa4af71d81f1420901eb6a4c7f0157 to your computer and use it in GitHub Desktop.
// 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