Asynchronous and parallel patterns in F # (3

Source: Internet
Author: User

In part 3rd of this series, we will explore lightweight, interactive proxies in F #, as well as proxy-related patterns, including the internal state of isolation. As the original content is more, the translation is split into two paragraphs. The basic use of proxies is discussed in the previous paragraph, while the second half discusses the further patterns in proxy usage. )

Message and Union type

Many times we use union type as the type of message. For example, I'm going to show a proxy based DirectX example, where we'll use the following message in the simulation engine:

type Message =
   | PleaseTakeOneStep
   | PleaseAddOneBall of Ball

Agents in the impersonation engine:

let simulationEngine =
   Agent.Start(fun inbox ->
     async { while true do
           // Wait for a message
           let! msg = inbox.Receive()

           // Process a message
           match msg with
           | PleaseTakeOneStep -> state.Transform moveBalls
           | PleaseAddOneBall ball -> state.AddObject ball })

In many cases, it is a good practice to use strongly typed messages. However, when you need to collaborate with other messaging mechanisms, there is no need to worry about using a generic message type such as "obj" and "string", at which point the agent only needs to type-judge or convert at run time.

Parameterized agents and abstract proxies

Proxies are only one design pattern in F # encoding. This means that you can use the various techniques commonly used in F #, such as parameterization, abstraction, or code fragment reuse with agents. For example, you can parameterize the previous Servequotestream function to specify the time interval in each stock message transmission:

open System.Net.Sockets

/// serve up a stream of quotes
let serveQuoteStream (client: TcpClient, periodMilliseconds: int) = async {
   let stream = client.GetStream()
   while true do
     do! stream.AsyncWrite( "AAPL 439.2"B )
     do! Async.Sleep periodMilliseconds
}

This means that different requests in your stock server can have different lengths of spacing.

Similarly, you can use function arguments to abstract the functionality of the entire proxy class:

let iteratingAgent job =
   Agent.Start(fun inbox ->
    async { while true do
         let! msg = inbox.Receive()
         do! job msg })

let foldingAgent job initialState =
   Agent.Start(fun inbox ->
    let rec loop state = async {
      let! msg = inbox.Receive()
      let! state = job state msg
      return! loop state
     }
    loop initialState)

You can use the first function in this way:

let agent1 = iteratingAgent (fun msg -> async { do printfn "got message '%s'" msg })

and a second:

let agent2 =
   foldingAgent (fun state msg ->
     async { if state % 1000 = 0 then printfn "count = '%d'" msg;
         return state + 1 }) 0

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.