Finora abbiamo imparato come utilizzare funzioni pure per evitare effetti collaterali in un programma. Inoltre, abbiamo visto i vantaggi di avere una funzione che dipende solo dai suoi argomenti di input.
Questo è solo l'inizio. Come suggerisce il nome, la programmazione funzionale è incentrata su una teoria delle funzioni.
Sarebbe sensato poterle trasmettere come argomenti ad altre funzioni, e restituire una funzione da un'altra funzione. Le funzioni sono considerate <dfn>oggetti di prima classe</dfn> in JavaScript, il che significa che possono essere utilizzati come qualsiasi altro oggetto. Possono essere salvate in variabili, memorizzate in un oggetto, o passate come argomenti di un'altra funzione.
Cominciamo con alcune semplici funzioni che operano sugli array, che sono metodi del prototipo dell'oggetto array. In questo esercizio stiamo guardando `Array.prototype.map()`, o più semplicemente `map`.
Il metodo `map` itera attraverso ogni elemento di un array e restituisce un nuovo array contenente i risultati della chiamata della funzione di callback su ogni elemento. Lo fa senza mutare l'array originale.
Quando viene utilizzata la callback, le sono passati tre argomenti. Il primo argomento è l'elemento che è correntemente in fase di elaborazione. Il secondo è l'indice di quell'elemento e il terzo è l'array su cui è stato chiamato il metodo `map`.
Qui sotto trovi un esempio d'uso del metodo `map` sull'array `users` che restituisce un nuovo array contenente come elementi solo i nomi degli utenti. Per semplicità, l'esempio utilizza solo il primo argomento della callback.
```js
const users = [
{ name: 'John', age: 34 },
{ name: 'Amy', age: 20 },
{ name: 'camperCat', age: 10 }
];
const names = users.map(user => user.name);
console.log(names);
```
La console visualizzerà il valore `[ 'John', 'Amy', 'camperCat' ]`.
L'array `watchList` contiene oggetti con informazioni su diversi film. Usa `map` su `watchList` per assegnare un nuovo array di oggetti alla variabile `ratings`. Ogni film nel nuovo array dovrebbe avere una key `title` con il nome del fil, e una key `rating` con il voto da IMDB. Il codice nell'editor al momento utilizza un ciclo `for` per farlo, quindi dovresti sostituire il ciclo con la tua espressione `map`.
"Actors": "Leonardo DiCaprio, Joseph Gordon-Levitt, Elliot Page, Tom Hardy",
"Plot": "A thief, who steals corporate secrets through use of dream-sharing technology, is given the inverse task of planting an idea into the mind of a CEO.",
"Language": "English, Japanese, French",
"Country": "USA, UK",
"Awards": "Won 4 Oscars. Another 143 wins & 198 nominations.",
"Writer": "Jonathan Nolan (screenplay), Christopher Nolan (screenplay), Christopher Nolan (story), David S. Goyer (story), Bob Kane (characters)",
"Actors": "Christian Bale, Heath Ledger, Aaron Eckhart, Michael Caine",
"Plot": "When the menace known as the Joker wreaks havoc and chaos on the people of Gotham, the caped crusader must come to terms with one of the greatest psychological tests of his ability to fight injustice.",
"Language": "English, Mandarin",
"Country": "USA, UK",
"Awards": "Won 2 Oscars. Another 146 wins & 142 nominations.",
"Writer": "Bob Kane (characters), David S. Goyer (story), Christopher Nolan (screenplay), David S. Goyer (screenplay)",
"Actors": "Christian Bale, Michael Caine, Liam Neeson, Katie Holmes",
"Plot": "After training with his mentor, Batman begins his fight to free crime-ridden Gotham City from the corruption that Scarecrow and the League of Shadows have cast upon it.",
"Language": "English, Urdu, Mandarin",
"Country": "USA, UK",
"Awards": "Nominated for 1 Oscar. Another 15 wins & 66 nominations.",
"Actors": "Sam Worthington, Zoe Saldana, Sigourney Weaver, Stephen Lang",
"Plot": "A paraplegic marine dispatched to the moon Pandora on a unique mission becomes torn between following his orders and protecting the world he feels is his home.",
"Language": "English, Spanish",
"Country": "USA, UK",
"Awards": "Won 3 Oscars. Another 80 wins & 121 nominations.",
"Actors": "Leonardo DiCaprio, Joseph Gordon-Levitt, Elliot Page, Tom Hardy",
"Plot": "A thief, who steals corporate secrets through use of dream-sharing technology, is given the inverse task of planting an idea into the mind of a CEO.",
"Language": "English, Japanese, French",
"Country": "USA, UK",
"Awards": "Won 4 Oscars. Another 143 wins & 198 nominations.",
"Writer": "Jonathan Nolan (screenplay), Christopher Nolan (screenplay), Christopher Nolan (story), David S. Goyer (story), Bob Kane (characters)",
"Actors": "Christian Bale, Heath Ledger, Aaron Eckhart, Michael Caine",
"Plot": "When the menace known as the Joker wreaks havoc and chaos on the people of Gotham, the caped crusader must come to terms with one of the greatest psychological tests of his ability to fight injustice.",
"Language": "English, Mandarin",
"Country": "USA, UK",
"Awards": "Won 2 Oscars. Another 146 wins & 142 nominations.",
"Writer": "Bob Kane (characters), David S. Goyer (story), Christopher Nolan (screenplay), David S. Goyer (screenplay)",
"Actors": "Christian Bale, Michael Caine, Liam Neeson, Katie Holmes",
"Plot": "After training with his mentor, Batman begins his fight to free crime-ridden Gotham City from the corruption that Scarecrow and the League of Shadows have cast upon it.",
"Language": "English, Urdu, Mandarin",
"Country": "USA, UK",
"Awards": "Nominated for 1 Oscar. Another 15 wins & 66 nominations.",
"Actors": "Sam Worthington, Zoe Saldana, Sigourney Weaver, Stephen Lang",
"Plot": "A paraplegic marine dispatched to the moon Pandora on a unique mission becomes torn between following his orders and protecting the world he feels is his home.",
"Language": "English, Spanish",
"Country": "USA, UK",
"Awards": "Won 3 Oscars. Another 80 wins & 121 nominations.",