How does Javascript's ability to use undeclared functions as objects help in sharing tasks between computers?

I recently read in this article to explain how JavaScript's ability to manipulate functions can be used so that every computer in the world can do a small part of processing all the information on the Internet. I understand that this is:

function map(fn, a) { for (i = 0; i < a.length; i++) { a[i] = fn(a[i]); } } 

map function allows you to quickly call a function for each element of the array

 map( function(x){return x*2;}, a ); 

and JS allows you to call a function without declaring it. The premise is that if all the data on the Internet was saved as an array, you can (somehow using map ) share the task of making certain changes to each element of the array between several processors or all computers in the world.

This is the part that I do not understand - why do you need to manipulate the t21 or JS array? Could you just send each computer a section of the array, send them a function to run on each element of the array and force them to convert the array without having to execute map or any number of wacky uses of the function?

Of course, using a function as an object seems convenient, but why is it generally inseparable from the task of separating tasks between the CPUs?

+5
source share
2 answers

No, you are not mistaken here. Joel does not advocate using JavaScript to "allow every computer in the world to play a small role in processing all the information on the Internet." It uses JavaScript as a language of choice to demonstrate the functionality of map and reduce functions (which, by the way, can be defined much more general than only for arrays). He then completely leaves the realm of JavaScript, believing that programming languages ​​need a certain level of abstraction ( first-class functions ) to help

First-class programming languages ​​let you find more abstraction options, which means your code is smaller, denser, more reusable, and more scalable.

That map and reduce so useful as a concept (without any specific language implementation), because they are absolutely universal, being able to express any set of data, simply passing different functions. While they are pure , they are trivially parallel and can be implemented on multi-core machines or even clusters of Internet scale without changing the algorithm or result.

+4
source

MapReduce is how google searched in the early years using multiple computers.

What I don't think is clearly conveyed - if you do not iterate yourself for loops and use map , then you can give it a function that takes a value and creates a new value, then the map function itself can decide how to work in parallel.

cannot work for loops, you have to manually execute your own parallel implementation. You can do parallel things in both directions, nothing stops it. But this is more a question of what was simpler / easier / less error prone

for a useful introduction to functional programming in js, you might want to take a look at https://drboolean.gitbooks.io/mostly-adequate-guide/content/

+1
source

All Articles