Object and stream

Source: Internet
Author: User

People often look at the pictures and recall the past, clearly feeling the various changes that have taken place along childhood, youth, middle-aged, and old age. From this change, we can abstract three keywords: object, time, and state. The object has a state and an identifier. The state changes over time when the identifier remains unchanged. This represents a dynamic world view: Time itself does not belong to the world, and the world is evolving in the time dimension. Under the guidance of this world view, when a computer program is used to simulate real-world problems, we use the object state in the computer to represent the state of the world, changes in the state of an object in a computer represent changes in the state of the world and time processes.

Unlike the above dynamic world view, another world view holds that the world is static in nature. How can this problem be solved? For example, a historical character changes every day of his life. From this perspective, the state changes. But if you look at his life, its life trajectory is static as a whole. We can take this whole as a research object. Here, we actually include the time dimension into the object attribute so that the object moving in the third dimension becomes a static object in the fourth dimension. Physically, the trajectory of an object in a four-dimensional space is the World Line of the object ). In addition, even if it is not a final object, if the object is regular, for example, the motion state of an object is completely determined by a certain formula, that is to say, the position of an object at a certain time in the future is completely determined. As long as we master this formula, we can grasp the world line of the object.

If x (t) represents the relationship between object states over time, x (t) can be expressed as a Sequence in discrete time ). Unlike the computed object model, the state change is used to implicitly represent the time process. Here, the time process is explicitly expressed by discrete sequence, while the calculation is from the input sequence input (t) output (t) transformation to the output sequence. Our program actually plays the role of a signal processor. But the disadvantage of finite sequence is that it requires the input to be complete and thus cannot process interaction. For example, the input (t) on the user's keyboard cannot be expressed in a finite sequence. Stream needs to be introduced to process interactions ). A stream can be regarded as an infinite sequence of inert values. In this way, the program becomes an infinite signal processor. The input is a stream, and the output is also a stream. For better understanding, the following is an example of a Stack implemented by C:

// C # stream-based Stack

Public enum OperationType {PUSH, POP}

Public struct Input {
Public OperationType Operation;
Public int Data;
}

Class Stack {
Public static IEnumerable <int> Transform (IEnumerable <Input> aSourceStream ){
Vertex list <int> list = new vertex list <int> ();
Foreach (Input command in aSourceStream ){
If (OperationType. PUSH = command. Operation ){
List. AddLast (command. Data );
}
Else if (OperationType. POP = command. Operation ){
Int data = list. Last. Value;
List. RemoveLast ();
Yield return data;
}
}
}
}

Public class InputGenerator {
Public static IEnumerable <Input> Generate (){
While (true ){
String line = Console. ReadLine ();
If (line. StartsWith ("push ")){
Yield return new Input {Operation = OperationType. PUSH, Data = int. Parse (line. Split ('') [1])};
}
Else if (line = "pop "){
Yield return new Input {Operation = OperationType. POP };
}
}
}
}

Public static void Main (string [] args ){
Foreach (int output in Stack. Transform (InputGenerator. Generate ())){
Console. WriteLine (output );
}
}

Running example:

> Push 1

> Push 2

> Pop

2

> Push 3

> Pop

3

In the preceding example, user Input is encapsulated into an infinite Input stream. Stack is a signal processor that flows from Input to int output stream.

 

Everything flows -- Heraclitus 

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.