map, filter y reduce
Tres formas de procesar una lista sin escribir un solo bucle for.
map, filter and reduce
Three ways to process a list without writing a single for loop.
map() recorre un array y devuelve uno nuevo, del mismo
tamaño, con cada elemento transformado por la función que le pases.
map() loops through an array and returns a new one, the same
size, with each item transformed by the function you pass it.
filter() recorre el array y devuelve uno nuevo, solo con los elementos donde
la función devuelve true.
filter() loops through the array and returns a new one, keeping only the items
where the function returns true.
Y como map y filter devuelven arrays,
se pueden encadenar uno detrás del otro.
And since map and filter return arrays,
they can be chained one after another.
reduce() es el más raro de los tres al principio, pero el más potente:
recorre el array acumulando un resultado, y al final entrega un solo valor.
reduce() is the strangest of the three at first, but the most powerful: it
loops through the array building up a result, and at the end hands back
a single value.
Un ranking típico: filtrar quiénes clasifican, calcular su bono, y sumar el total a repartir. Los tres métodos, encadenados.
A typical ranking: filter who qualifies, calculate their bonus, and add up the total to hand out. All three methods, chained.
map() para transformar, filter() para elegir,
reduce() para resumir en un valor, y cómo encadenarlos para reemplazar bucles
enteros con una sola línea legible.
Ahora, cómo separar tu código en varios archivos en vez de amontonarlo todo en uno.
map() to transform, filter() to choose,
reduce() to summarise into a value, and how to chain them to replace whole loops
with one readable line.
Now, how to split your code across several files instead of piling it all into one.