Can your programming language do this? _javascript skills
One day, you are browsing your code and find that there are two large pieces of code that are almost the same. In fact, they are exactly the same - except one is about Spaghetti and the other is about Chocolate Moose.
// A small example:
alert("I want to eat pasta!");
alert("I want to eat chocolate mousse!");
Hmm , this example happens to be written in JavaScript, but even if you don’t understand JavaScript, you should be able to understand what it is doing.
Copying code is not good. So, you create a function
function SwedishChef(food){
alert("I want to eat" food "!");
}
SwedishChef("pasta");
SwedishChef("Chocolate Mousse");
Ok, this is just a very small example, I believe you can imagine a more practical example. This code has many advantages, all of which you have heard tens of thousands of times: maintainability, readability, abstraction = good!
Now you notice that there are two other pieces of code that are almost identical to them, except that one repeatedly calls a function called BoomBoom, and the other repeatedly calls a function called PutInPot. Other than that, the two pieces of code are almost identical:
alert("Get the lobster");
PutInPot("lobster");
PutInPot("water");
alert("Get the chicken");
BoomBoom("Chicken");
BoomBoom("Coconut Sauce");
Now we need to find a way so that you can use one function as a function of another parameter. This is an important capability, because it is easier for you to write the framework code as a function (emu note: remember the template method pattern, right?).
function Cook( i1, i2, f ){
alert("get" i1);
f(i1);
f(i2);
}
Cook( "Lobster", "Water", PutInPot );
Cook( "Chicken", "Coconut Sauce", BoomBoom );
Look, we actually passed the function as a calling parameter!
Can your programming language do it?
Wait... If we already have the specific implementation code of functions such as PutInPot and BoomBoom (and do not need to reuse them elsewhere), then writing them into function calls using inline syntax is not Is it prettier than explicitly declaring these two functions?
Cook( "lobster",
"water",
function(x) { alert("pot " x); } );
Cook( "chicken",
"Coconut Sauce",
function(x) { alert("boom " x); } );
Yeah, so convenient! Please note that I just created a function out of the blue, I didn’t even think about naming it, I just took it by the ear and dropped it into a function.
When you think of anonymous functions as parameters, you probably think of code that performs the same operation on every element in an array.
var a = [1,2,3];
for (i=0; i
}
for (i=0; i
}
It is often necessary to do the same for all elements in the array thing, so you can write a function like this to help:
function map(fn, a){
for (i = 0; i a [i] = fn(a[i]);
}
}
Now you can change the above to:
map( function(x){return x*2 ;}, a );
map( alert, a );
Another common task is to sum up all the elements in an array in some general way:
function sum(a){
var s = 0;
for (i = 0; i s = a[i];
return s;
}
function join(a){
var s = "";
for (i = 0; i s = a[i];
return s;
}
alert(sum([1,2,3]));
alert(join(["a","b","c"]));
sum They look very similar to join. You may want to abstract them into a generic function that sums up all the elements in the array according to some algorithm:
function reduce(fn, a, init){
var s = init;
for (i = 0; i s = fn( s, a[i] );
return s;
}
function sum(a){
return reduce( function(a, b){ return a b; }, a, 0);
}
function join(a){
return reduce( function(a, b){ return a b; }, a, "" );
}
Many early programming languages were unable to do this. Some languages allow you to do it, but make it difficult (e.g. C has function pointers, but you have to declare and define the function elsewhere). Object-oriented languages also do not guarantee what you can do with functions (treat functions as objects?).
If you want to treat a function as a class of objects, Java requires you to create an object with a single method, called an operator object. Many object-oriented languages require you to create a complete file for each class, and development like this is really fast. If your programming language requires you to use operator objects to wrap methods (rather than treating methods themselves as objects), you are not getting the full benefits of a modern (dynamic) programming language. Why not give it a try and see if you can return it and get some money back?
What’s the benefit of not having to write functions that do nothing but do something with each element through an array?
Let’s look back at the map function. When you want to do something with each element in an array, you probably don't care which element is done first. Whether you start execution from the first element or the last element, your result will be the same, right? If you have 2 CPUs on hand, you can write code so that they each work on half of the elements, so map is twice as fast.
Or, use your imagination. Imagine that you have tens of millions of servers distributed in several data centers around the world. You have a really big array. Well, let’s use it a little more. Use your imagination and imagine that this array records the contents of the entire Internet. Also, now you can execute map on thousands of servers simultaneously, allowing each server to solve a small part of the same problem.
So in this example, writing a very fast code to search the entire Internet is as simple as calling the map function with a simple string searcher (operator) as a parameter.
I hope you noticed a really interesting point. If you want to turn the map/reduce pattern into a technology that is useful to everyone and can be used by everyone immediately, you It only takes a super genius to write the most important part of the code so that map/reduce can run on a huge array of parallel computers, and then the rest of the old code that has always worked well in a single loop can still remain correct Running, the only difference is that it is n times faster than the original single-machine running. This means that they all suddenly turn into code that can be used to solve a huge problem.
Let me say it again. By abstracting the concept of "loop", you can implement the "loop" process in any way you like, including making the loop iteration speed scale with the hardware computing power. Maintain satisfactory synchronized growth.
You should now be able to understand why the recent dissatisfaction with those computer science students who have not been taught anything but Java: ( http://www.joelonsoftware.com/articles/ThePerilsofJavaSchools.html ):
Without understanding functional programming, you can't invent MapReduce, the algorithm that makes Google so massively scalable. The terms Map and Reduce come from Lisp and functional programming. MapReduce is, in retrospect, obvious to anyone who remembers from their 6.001-equivalent programming class that purely functional programs have no side effects and are thus trivially parallelizable. The very fact that Google invented MapReduce, and Microsoft didn't, says something about why Microsoft is still playing catch up trying to get basic search features to work, while Google has moved on to the next problem: building Skynet^H^H^H^H^H^H the world's largest massively parallel supercomputer. I don't think Microsoft completely understands just how far behind them are on that wave.
If you don’t understand functional programming, you can’t invent MapReduce, the algorithm that makes Google’s computing power so scalable. The terms Map and Reduce are derived from Lisp language and functional programming... (This is the content of another article, emu does not fully understand the ins and outs of the various terms in it, so I will not translate it)
I hope you understand now that (dynamic) programming languages that treat functions as basic types allow you to better abstract during the programming process, which means making the code leaner, more functional, more reusable, and more efficient. Extensible. Many Google applications use the Map/Reduce model, so they can all benefit from it when someone optimizes it or fixes defects.
I’m going to be a little more verbose. I think the most productive programming language is one that allows you to abstract at different levels. The old FORTRAN language used to not let you write functions. C has function pointers, but they are very ugly, ugly, ugly, ugly, do not allow anonymous declarations, and cannot be implemented when using them and must be implemented elsewhere. Java lets you use operator objects, something even uglier. As Steve Yegge said, Java is a kingdom of nouns (http://steveyegge.blogspot.com/2006/03/execution-in-kingdom-of-nouns.html).
Author's Note: FORTRAN is mentioned here, but the last time I used FORTRAN was 27 years ago. FORTRAN has functions, and what I was thinking about when I was coding was probably the GW-BASIC language. (emu note, basic does only have so-called subroutines and go-sub statements, which only reorganize the code structure. There are no parameters and call stacks, so there are no real function calls)
Translator's Note: Original author The title "Can your programming language do this?" is actually not the real value of this article. I repost this article not because the original author can master the basic skills of the language, but because this is A demonstration of the map/reduce model.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

Both Python and JavaScript's choices in development environments are important. 1) Python's development environment includes PyCharm, JupyterNotebook and Anaconda, which are suitable for data science and rapid prototyping. 2) The development environment of JavaScript includes Node.js, VSCode and Webpack, which are suitable for front-end and back-end development. Choosing the right tools according to project needs can improve development efficiency and project success rate.
