Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

An example Analysis of F# language

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "F# language case Analysis". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "F# language case Analysis".

Write a function that adds two numbers in F# and enter it in F# Interactive:

one

Let add num1 num2=num1*num2

F# Interactive infers this function type for us: val add: num1:int-> num2:int-> int, which means that add has two arguments of type int and gets a type of int.

Function as an argument:

one

two

three

four

five

six

/ / C#

Private int Twice (int input,Func f)

{

Return f (f (input))

}

Var result = Twice (2, n = > n)

Using F# requires only a very concise function declaration:

one

two

three

four

five

six

> let twice (input:int) f (input)

Val twice: input:int-> f: (int-> int)-> int

> twice 2 (fun n-> n)

Val it: int = 16

Val twice: input:int-> f: (int- > int)-> int is the inference given by F # Interactive: the twice function requires an int parameter and a (int- > int) function as arguments and returns an int.

These two examples are just a warm-up and are not the focus of this blog, so if you think the first two examples are boring or not clear, please continue to read the summary below.

Scenario: an event will have a Schedule, which can be divided into three types: Once (once), Daily (once a day) and Weekly (once a week). Events will have different promotional content and different postponement strategies according to the type of Schedule.

How do you think about such a scene?

I. process type-oriented coding

Coding for process types is a literal translation of requirements, and the code is written as follows:

1. Display the promotional content of the event:

one

two

three

four

five

six

seven

eight

nine

ten

eleven

twelve

thirteen

fourteen

fifteen

sixteen

seventeen

Public void ShowScheduleDescriptions ()

{

Switch (ScheduleType)

{

Case ScheduleType.Once:

Console.WriteLine ("this is once activity")

Break

Case ScheduleType.Daily:

Console.WriteLine ("this is daily activity")

Break

Case ScheduleType.Weekly:

Console.WriteLine ("this is weekly activity")

Break

Default:

Throw new InvalidOperationException ("unsupported schedule")

}

}

Such code looks fine at first, but there are actually two red flags:

In violation of the OCP principle, if you need to add a Monthly type one day, you no doubt need to modify this method.

This code style will allow the following developers to continue without thinking, for example, the need to postpone activities according to different types of activities.

two。 Postponement of activities:

one

two

three

four

five

six

seven

eight

nine

ten

eleven

twelve

thirteen

fourteen

fifteen

sixteen

seventeen

Public void DelaySchedule ()

{

Switch (ScheduleType)

{

Case ScheduleType.Once:

Console.WriteLine ("Delay one hour")

Break

Case ScheduleType.Daily:

Console.WriteLine ("Delay one day")

Break

Case ScheduleType.Weekly:

Console.WriteLine ("Delay one week")

Break

Default:

Throw new InvalidOperationException ("unsupported schedule")

}

}

Such a nominee violates the DRY principle, but the same code framework cannot be reused.

Second, object-oriented coding.

For an experienced OO developer, once you see switch,if (type=typeof (…)) Code like this will immediately be on the alert. Are there some abstract types that haven't been found? In this example, the following abstractions are found:

one

two

three

four

five

six

seven

eight

nine

ten

eleven

twelve

thirteen

fourteen

fifteen

sixteen

seventeen

eighteen

nineteen

twenty

twenty-one

twenty-two

twenty-three

twenty-four

twenty-five

twenty-six

twenty-seven

twenty-eight

twenty-nine

thirty

thirty-one

thirty-two

thirty-three

thirty-four

thirty-five

thirty-six

thirty-seven

thirty-eight

Public class Schedule

{

Public virtual void ShowShowScheduleDescriptions ()

{

}

Public virtual void DelaySchedule ()

{

}

}

Public class OnceSchedule: Schedule

{

Public override void ShowShowScheduleDescriptions ()

{

Console.WriteLine ("this is once activity")

}

Public override void DelaySchedule ()

{

Console.WriteLine ("Delay one hour")

}

}

Public class DailySchedule: Schedule

{

Public override void ShowShowScheduleDescriptions ()

{

Console.WriteLine ("this is daily activity")

}

Public override void DelaySchedule ()

{

Console.WriteLine ("Delay daily day")

}

}

/ /... Other schedule

This kind of code solves two problems of process-oriented code and looks more extensible. With the introduction of new types of Schedule, the old code does not need to be changed at all.

Of course, things are not absolute, under what circumstances do you need to change the old code? When we need to extend the behavior of Schedule, such as requirements upgrade, different Schedule have different ways of hosting, we have to add a void Hold () method to each Schedule.

III. Functional solution

Functional languages use distinguishable unions and pattern matching to deal with such problems.

Define a Schedule distinguishable federation:

one

two

three

four

Type Schedule=

| | Once of DateTime |

| | Daily of DateTime*int |

| | Weekly of DateTime*int |

This type illustrates that there are three different types of Schedule and defines the data structures that each of the three types have. It's like a combination of Enum and classes, but it's very delicate.

1. Display the promotional content of the event, using pattern matching:

one

two

three

four

five

Let ShowShowScheduleDescriptions schedule=

Match schedule with

| | Once (DateTime)-> printfn "this is once activity" |

| | Daily (DateTime,int)-> printfn "this is daily activity" |

| | Weekly (DateTime,int)-> printfn "this is weekly activity" |

This method is similar to switch. Case, but it is achieved by matching distinguishable unions, rather than by a displayed Enum.

two。 Postponement of activities:

one

two

three

four

five

Let DelaySchedule schedule=

Match schedule with

| | Once (DateTime)-> printfn "Delay one hour" |

| | Daily (DateTime,int)-> printfn "Delay one day" |

| | Weekly (DateTime,int)-> printfn "Delay one week" |

Functional programming solutions find it easy to add new behaviors, such as Hold (). Coding is accomplished by defining a distinction between federation and pattern matching, and the whole solution is like a combination of process-oriented and object-oriented, but the focus is different and the implemented code is more refined.

Thank you for your reading. The above is the content of "F# language example Analysis". After the study of this article, I believe you have a deeper understanding of the problem of F# language example analysis, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report