Created
          July 15, 2009 11:24 
        
      - 
      
- 
        Save JeffreyZhao/147655 to your computer and use it in GitHub Desktop. 
    Selective Receive with F# MailboxProcessor
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | #light | |
| open Microsoft.FSharp.Control | |
| let counter = | |
| MailboxProcessor.Start(fun inbox -> | |
| let rec loop() = | |
| async { | |
| let! msg = inbox.Scan(fun x -> | |
| match x with | |
| | 1 -> Some(async { return x }) | |
| | _ -> None) | |
| do printf "%d " msg | |
| let! msg = inbox.Scan(fun x -> | |
| match x with | |
| | 2 -> Some(async { return x }) | |
| | _ -> None) | |
| do printf "%d " msg | |
| return! loop() } | |
| loop()); | |
| for t = 1 to 5 do counter.Post(1) | |
| for t = 1 to 5 do counter.Post(2) | |
| Console.ReadLine() |> ignore | |
| // output: | |
| // 1 2 1 2 1 2 1 2 1 2 | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment