Lambda expressions implement finite state machines and lambda expressions

Source: Internet
Author: User

Lambda expressions implement finite state machines and lambda expressions

There are multiple modes to implement the state machine. The most flexible and powerful method is to migrate tables. One of the disadvantages of this method is that you need to write a large number of small pieces of code to support the migration table. In C #3.0, it can be implemented in a very elegant way.
In addition to finite state machines, finite state machines are also used to analyze characters.
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;

Namespace StateMachine
{
Class Program
{
Static void Main (string [] args)
{
Var door = new Door (State. Open );

While (true)
{
String s = Console. ReadLine ();
Operation op = string. IsNullOrEmpty (s )? Operation. Push: Operation. Pull;
Door. Process (op );
}
}
}

Enum Operation
{
Push, Pull
}

Enum State
{
Open, Closed
}

Class Door
{
Public State {get; set ;}

Dictionary <State, Dictionary <Operation, Action> rule;
Public Door (State state)
{
This. State = state;

Rule = new Dictionary <State, Dictionary <Operation, Action> ();
Foreach (var e in Enum. GetValues (typeof (State )))
{
Rule [(State) e] = new Dictionary <Operation, Action> ();
}

InitOperationRule ();
}

Void InitOperationRule ()
{
// Normal operation
Rule [State. Closed] [Operation. Push] = () => {Console. WriteLine ("door opened"); State = State. Open ;};
Rule [State. Open] [Operation. Pull] = () => {Console. WriteLine ("the door has been pulled"); State = State. Closed ;};

//// Handling of several special cases
// Rule [State. Closed] [Operation. Pull] = () => Console. WriteLine ("the door is Closed and it is also pulled ");
// Rule [State. Open] [Operation. Push] = () => Console. WriteLine ("the door is Open. You don't need to Push it. Go in directly ");
}

Public void Process (Operation op)
{
Try
{
Rule [State] [op] ();
}
Catch (KeyNotFoundException)
{

Console. WriteLine (string. Format ("the {1} operation is not allowed for the door in the {0} State", State, op ));
}

}
}
}

From the code, we can see that using lambda expressions can simplify the structure of the migration table and make it more intuitive.
One disadvantage of constructing a state machine through a migration table is the query speed. In this example, two queries are required for each operation to perform the state conversion operation. If the status is large, it is very time-consuming. Here I have improved it so that each operation only needs to be queried once.

Class DoorPlus
{
State state;
Public State
{
Get {return state ;}
Set
{
If (state! = Value)
CurrentOpRule = rule [value];
State = value;
}
}

Dictionary <Operation, Action> currentOpRule;
Dictionary <State, Dictionary <Operation, Action> rule;
Public DoorPlus (State state)
{
This. State = state;

Rule = new Dictionary <State, Dictionary <Operation, Action> ();
Foreach (var e in Enum. GetValues (typeof (State )))
{
Rule [(State) e] = new Dictionary <Operation, Action> ();
}

CurrentOpRule = rule [State];

InitOperationRule ();
}

Void InitOperationRule ()
{
// Normal operation
Rule [State. Closed] [Operation. Push] = () => {Console. WriteLine ("door opened"); State = State. Open ;};
Rule [State. Open] [Operation. Pull] = () => {Console. WriteLine ("the door has been pulled"); State = State. Closed ;};

//// Handling of several special cases
// Rule [State. Closed] [Operation. Pull] = () => Console. WriteLine ("the door is Closed and it is also pulled ");
// Rule [State. Open] [Operation. Push] = () => Console. WriteLine ("the door is Open. You don't need to Push it. Go in directly ");
}

Public void Process (Operation op)
{
Try
{
CurrentOpRule [op] ();
}
Catch (KeyNotFoundException)
{

Console. WriteLine (string. Format ("the {1} operation is not allowed for the door in the {0} State", State, op ));
}
}
}


Where does the lambda expression come from?

Lambda Expressions (Lambda Expressions) 16: 33 Lambda Expressions (Lambda Expressions) and anonymous methods are actually one thing. The only difference is that their syntax format is different. Lambda expressions are further evolved in terms of syntax. In essence, they are one thing. Their role is to generate methods. That is, the inline method.

Reference from C # head Architect Anders Hejlsberg:

Www.ondotnet.com/..page%2

Lambda expressions and anonymous methods are really just two words for the same thing. The only thing that differs is, "What does the syntax look like? "And the lambda expressions are a further evolution of the syntax. But underneath, they do the same thing. They generate methods. You know, they're in-line methods.

Therefore, we should understand both the anonymous method and Lambda expression. Next, let's take a look at a simple code example, using anonymous methods and Lambda expressions to search for Arrays:

Use the. net 2.0 anonymous method to search for the string array containing

Static void Main (string [] args)
{
String [] list = new string [] {"abc", "12", "java "};
String [] ll = Array. FindAll (list,
Delegate (string s)
{
Return s. IndexOf ("a")> = 0;
}
);
Foreach (string var in ll)
{
Console. WriteLine (var );
}
Console. ReadLine ();
}

Use a. net 3.5 Lambda expression to search for a string array containing

Static void Main (string [] args)
{
String [] list = new string [] {"abc", "12", "java "};

String [] ll = Array. FindAll (list, s => (s. IndexOf ("a")> = 0 ));
Foreach (string var in ll) ...... remaining full text>

Relationship between Lambda expressions and regular expressions in C #

Links that cannot be reached
Lambda expressions are a feature of functional languages. c # uses its dynamic compilation feature to achieve this effect. It can be used to write anonymous functions. no such thing as C and C ++.
Regular Expressions are implemented in various languages and used for query and matching.

Related Article

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.