Created
January 25, 2023 19:02
-
-
Save leandromoh/4b88b934683103ee4574d315a28c246d to your computer and use it in GitHub Desktop.
demostration how task works, C# and F#
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
// https://sharplab.io/#v2:C4LgTgrgdgNAJiA1AHwAICYCMBYAUKgBgAJVMA6AGQEsoBHAbj0JMwFZHcmAOEgNgB4awAHxEA4gFNgAMQD2sgBRCiVAJR4A3niI6SATj5kAIhIA2AQwCeCzAVUddLPUvvbdqAOwqOAXzx4AN3MwIgAPAGciAF4iAFEoCABbCTBzACNTCTIAJXMoAHMJGxgiW1UyAGUzCQBjYAVJGXlXXEc2vAAzWTAJcxqACwUgkNCVKDDwlqYDVF5jMysbAH0CVan8TGcAIkAeDcAQfa2WoA== | |
using System; | |
using System.Linq; | |
using System.Threading.Tasks; | |
async Task<int> GetFoo(int i) | |
{ | |
await Task.Delay(10); | |
Console.WriteLine(i); | |
return i; | |
} | |
var xs = Enumerable.Range(1, 10).Select(GetFoo); | |
foreach(var x in xs); | |
await Task.Delay(1_000); | |
Console.WriteLine("🌄"); |
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
// https://sharplab.io/#v2:DYLgZgzgNAJiDUAfA9gBwKYDsAEBlAnhAC7oC2A3ALABQaWehJpAdACoAWATugIYwCWmAOZseEANYQqtDDgLEybLrwHDy2GjWDoi2IToBiyZNn7YAvNiJjx2AN41sT7DGQBCbKxvMAIumA8+NgAjAAMjs6onIJEYDgARACk/PGmGtTO2NxEAK6cOPw0AL6a1Nq6AB4QFtgA2sHMzGEAutiIAHx46ACOzKQ8qHqGxqVgyJzYFaY4VS7IjgAUAJSONBzcfMy42uioC8EA+qHHKxnpUTFx2PGAPBuAIPvxNEA== | |
open System; | |
open System.Threading.Tasks; | |
open System.Threading; | |
let getFoo i = task { | |
do! Task.Delay 10 | |
printfn "%i" i | |
return i | |
} | |
let xs = [1..10] |> Seq.map getFoo | |
for x in xs do | |
() | |
Thread.Sleep(1_000) | |
printfn "🌄" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment