This page is a love letter to the functional methods waiting patiently inside of Javascript. They are all ES5 array methods. All are stateless; they give predictable output. Functional programming makes the code more like math. And just as math is proveable, functional code is proveable.
I got onto this angle while using Elm, an immutable language that compiles into Javascript, providing both an immutable environment for state, as well as a pleasant encounter with your errors, via very friendly compiler error messages.
"Pure functions are easier to reason about. Testing is easier, and pure functions lend themselves well to techniques like property-based testing. Debugging is easier. Programs are more bulletproof. " - Alvin Alexander.
For each three letter prompt, can you guess the Functional javascript method? (Mouse over each for explanations.)
Remove any occurance of "never" from Lear's lament.
let sansNever =(w)=>{
if (w ==='never'){
return false;
} else if (w!=='never'){
return true;
} else {
console.log('ERROR');
}
}
lear_tears = bardWords.filter(sansNever);
Colors from Detroit. Data from Baseball Reference.
youngPitchersArrayOfObjects = rotators.filter((o)=>{return o.Age < 30 });
youngPitchersArrayOfNames = youngPitchersArrayOfObjects.map((o)=>{return o.Name} );
Join an array of words from King Lear to commentary about those words.
cont = cliffWords.concat(bardWords);
Pluck several elements from the middle of an array.
cont = bardWords.slice(5, 9);
Convert an array to a string, optionally inserting some sort of string within its joints.
cont = lear_tears.join('****');
This is a mnemonic mnemonic device for remembering array functions. From the left: join, concat, slice, some(sum), every, filter, map, forEach.