No nonsense JavaScript (bottom)

Source: Internet
Author: User

Five, functional type

This is not a JavaScript invention, its inventor is dead, and his invention is still bothering us ... Like Edison's lamp.
The bubbles are still shining on us.

In fact, the functional language is very simple, it is a language with the command-style language "complete" implementation of the program. Because of its basic ideas and commands
--If you don't want to use this hard-to-understand noun, change it to C, or Delphi--the language is completely different, so most
Cases, it is also incompatible with these traditional, generic, and commercially-oriented languages.

And in fact, you use it every day.

The following line of code is filled with the idea of a functional language:

[JavaScript]View Plaincopy
    1. A + b


Is it? Really, if you think of that "+" number as a function, it's exactly the same. In fact, the so-called function is a
Execution, an operation, a function entry ... That is, something in the code, either it is data, or it is an operation. And AS
If it is an operation, you can think of it as a "function". In this line of code-the expression, A and B are obviously data, and the + number is the
Operations, so it can naturally be seen as a "function, process, or operation." So...... The "function" that operates a sum of two numbers can be written
Into this:

[JavaScript]View Plaincopy
    1. Func_add (A, B)

or this:

[JavaScript]View Plaincopy
    1. (+ a B)

All of this is just a different notation on the text. It's like I said the symbol is "Jia", and you have to say it's "dark," and the other
But one must read "a-d-d." What's the difference? No.

All programmers write statements, write expressions, and arithmetic logic every day. We all know that these things can be written in a ... Well...... Incredibly big
In large functions, or in countless small, seemingly beautiful functions. When all of these functions are sequential, one after another, it
It becomes a "functional language". So Lisp, known as the "functional language of the grandparents", is the language of "Connecting function operations":

[JavaScript]View Plaincopy
    1. (Defun subst (x y z)
    2. (Cond (Atom Z)
    3. (Cond (eq z y) x)
    4. (' t z)))
    5. (' t (cons (subst x y (car z))
    6. (Subst x y (cdr z))))))

Some people think it is ugly spaghetti, and some people think it is the most concise language. Oh, as you know, that's what it looks like, Nikki.

Because the functional language has only a function that executes continuously--it does not have a "statement" and must achieve logical integrity. To put it simply, he
The three basic logic of "order, branching, and looping" needs to be implemented in a continuous execution process. The way to solve the function is to use ternary condition arithmetic to
Instead of If/then/else, it's the following code:

[JavaScript]View Plaincopy
    1. If a then B else C
    2. Equivalent
    3. A? B:c

In addition, recursion (function tuning function) is used to implement loops. Consider a recursive function call that causes a stack overflow (stack overflow at ...),
Therefore, the functional language puts forward "tail recursion", that is, in the writing code is "ensure" only in the function of the last operation recursive call.
In this way, the recursive call does not need to hold the stack-because there is no subsequent operation, it can be optimized to a line that does not need to be returned
JMP assembly instructions.

The world is so beautiful, the so-called function is just a bunch of arithmetic portals, as well as JMP, jmp, and JMP. But, but, isn't it--this CPU?

Six, more advanced function (1)

In fact, the world is not good.

If a piece of CPU is lying there, it will only be in order, branching, looping and the like. But when it runs, it has to have a
A clock is ticking ... Click ... If it is multi-core, there will be several such things at the same time are ticking.

This is the problem, the world is not a single time series, nor is it orderly. So there will be concurrency, there will be interrupts, there will be exceptions. Functional languages should be
It should be as infinitely stretched as spaghetti, so how many spaghetti would it be if there were multiple clicks? However, this sentiment
Do you still call spaghetti?

The solution in the functional formula is called continuation (continuation) and node. The continuation is to stop the current move to another place, and then return, and the node is
The code in one location is independent and unrelated to the other node. In functional languages, multiple nodes are like parallel universes, and most
They are transparent to each other, and if they are to be connected, they need extremely large energies and ... A wormhole.

We don't need to delve into the problems of multiple nodes, and the space-time problems caused by access in multiple spaces are the research by astrophysicists and space-time multidimensional theorists
Question, we just need to know: If you want multiple nodes to have cross-access relationships, then the world/universe will be destroyed. Such a class
It is written in a white paper, a reference manual, and an astronaut's daily guide to the languages of Erlang, which natively support multiple universes. If you want to be poor
The source, and think that they have been able to clearly understand more than 300 advanced digital, physical and celestial academic language, the ground below a process-oriented
The entry guide for the sequencer, start here:
Http://www.blogjava.net/canonical/archive/2007/12/05/165664.html

Continuation is one of the ways to solve state problems. In short, there is a state of time: The past, the present and the future. For the past, we have history,
It will also be because of what happened in the past to decide what is happening now, for example, because yesterday drank a little wine, so today can only eat gruel, and now Also
It is tomorrow's yesterday, so to record today's state for what can be done tomorrow, for example, today has eaten gruel; As for tomorrow, of course,
There are a lot of things going on and we have to be prepared one by one.

Oh, the important thing is this "ready". This is called in the computer system: event. or call it a plan, or call it a burst. Know why I
Can I run our pc? En... is because there is an instruction pipeline that executes instructions in a sufficiently small time slice. For computing Systems
, this timed command-access behavior is the interrupt. So one day a friend asked me: If the execution logic is only sequential, branching and
Loops, what are the triggers in the process system? I think for a long time, no solution. And now to answer this question, you have to take the "Time" dimension
In addition, the so-called burst, concurrency and similar things, is the logic of the concept of time series.

Well, when we're "ready" for the future to trigger something--simply put, it's a clock handler.
(in Windows It is called OnTimer, in the browser it is called settimeout/setinterval), in order to this time we can in the functional
A good Italian noodle in the language, we explain: "We are ready", and "how we prepared". And according to
The basic principle of function is that a function is independent of state, and it has nothing to do with a time series such as "Future". This has become a contradiction.

In fact, we have already clashed with this contradiction in the first time, and that is the "cycle". Because the loop requires a state amount to indicate the loop progress,
This "progress" is timing-related. And the function uses "recursion" to solve it, that is, through the parameters of the recursive function to pass the input
Degree. In the "Recursive parameters"--both sides of the interface, the function is time-dependent. The problem is stack overflow, and the workaround
is the tail recursion, and the problem is programming complexity, and can prove that the tail recursion can replace all recursion; the solution is ... Weinberg
Rightly so, all the ways to solve the problem will bring new problems. Oh... It's Weinberg again.

Now, we clearly need "more state", because we have the system running in one or more of the time series--also on the CPU. Even
We have nodes, and we guarantee "no wormhole", then we also need to solve the past, present and future problems in a CPU.

The function of the Immortals said two words: continuous. Simple is to pass the arguments for the past, the present state, and the preparation for the future as a function.
To. It looks like the "Now" is going to explode immediately, because it involves both the past, the present state, and the changes, including future operations. In
Is the new solution is: to not explode now, the "continuous" interface does not have to operate.

Operations are made by "future" data based on "past", which is the continuation. The functional feature that supports it is the lazy evaluation. To put it simply,

[JavaScript]View Plaincopy
    1. function f (x, C) {
    2. ...
    3. C (x);
    4. }
    5. F (data, f);

Because the C itself is not evaluated on the Transport Interface F (), the explosion occurs/does not occur at the time of the future operation of C (x). If
The entropy of the universe has its limits, so this limit is also in the last known future. And, in the last known future, it is possible to distort back to the present. This is
The two principles that continue:
--------------------
After a successful continuation, is a new continuation (or process)
A failure continues to go back to the previous selection point
--------------------
It is because there is a continuous state, and this state and persistence itself are passed through the function parameters, so, "back to the selection point" is only the future itself
A choice that is irrelevant to the present state.

Seven, more advanced function (2)

In any case, the complexity of the various functional forms is indeed the existence of a kind of self-purity in the programming paradigm. such as generators (generator)
This problem occurs because there is a correlation (such as an increment) between the generation of a batch of data, and the resulting process is time-dependent (not always in a single call
Get all the data), the functional language defines a function concept such as a "generator". We can define a yield in the generator,
This return is the "future" to return to this "now" a wormhole. Through this wormhole, we can get a timing-related data. Below
This is one example of the Fibonacci sequence on Mozilla:

[XHTML]View Plaincopy
  1. <!--for Firefox 3--
  2. <mce:script type="application/javascript;version=1.7"><!--
  3. function fib () {
  4. var i = 0, j = 1;
  5. while (true) {
  6. Yield I; <-wormholes are here
  7. var t = i;
  8. i = J;
  9. j + = T;
  10. }
  11. }
  12. var g = fib ();
  13. A = g.next ();
  14. b = G.next ();
  15. c = G.next ();
  16. // ...
  17. // .. After three days (or after a cycle), a time traveler wants to go back to the FIB and see
  18. z = g.next ();
  19. Well, it turns out to be the value "2."
  20. alert (z);
  21. --></mce:script>


Viii. Conclusion

It seems as if this article "no nonsense" has a lot of nonsense ... Haha, joke, really all no nonsense, or the living can see the wood?

I really want to read nonsense, you should go to college textbooks.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.