Last active
August 29, 2015 14:08
-
-
Save devboy/3b094b2a23c79f045ce9 to your computer and use it in GitHub Desktop.
F# Async Workflows Run Like A Charm On Unity3D
This file contains 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
using UnityEngine; | |
using System.Collections; | |
public class NewBehaviourScript : MonoBehaviour { | |
// Use this for initialization | |
void Start () { | |
Debug.Log ("Start"); | |
Playground.AsyncTango(); | |
Debug.Log ("Dancing The Tango"); | |
} | |
int count = 0; | |
// Update is called once per frame | |
void Update () { | |
Debug.Log ("Update" + (++count).ToString()); | |
} | |
} |
This file contains 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
module Playground | |
open FSharp.Data | |
open UnityCompat | |
let urlList = [ "http://www.microsoft.com/" | |
"http://msdn.microsoft.com/" | |
"http://www.bing.com" | |
"http://www.google.com" | |
"http://www.apple.com" | |
"http://www.yahoo.com" | |
"http://www.wooga.com" | |
"http://www.facebook.com" | |
] | |
let fetchAsync(url:string) = | |
async { | |
try | |
Logf "%s on %i" url Threading.Thread.CurrentThread.ManagedThreadId | |
let! html = Http.AsyncRequestString (new System.Uri(url)).AbsoluteUri | |
Logf "Read %d characters for %s" html.Length url | |
with | |
| ex -> printfn "%s" (ex.Message); | |
} | |
let AsyncTango ()= | |
let rnd = System.Random() | |
urlList | |
|> Seq.map (fun url -> | |
let delay = rnd.Next(1000, 3000).ToString() | |
"http://deelay.me/"+delay+"/"+url) | |
|> Seq.map fetchAsync | |
|> Async.Parallel | |
|> Async.Ignore | |
|> Async.Start |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment