Before you can write out your learning experience, write about expert F # About AsynchronizationProgramOfArticleExamples of outdated syntax and corresponding improvements
Code
# Light
Open System
Open System. Io
Open Microsoft. fsharp. Control
Open Microsoft. fsharp. Control. commonextensions
LetNumimages =200
LetSize =512
LetNumpixels = size * size
LetTransfromimage (pixels, imagenum) =
Printfn"Transformimage % d"Imagenum
//Frequent CPU computing operations
Pixels|> (Array. Map (FunB->B + 1uy ))
Let Processimageasync (I) =
Async {
Use instream = file. openread (sprintf " Image % d. tmp " I)
Let ! Pixels = instream. readasync (numpixels) // Change to asyncread
Let Pixels ' = Transfromimage (pixels, I)
Use outstream = file. openwrite (sprintf " Image % d. Done " I)
Do ! Outstream. writeasync (pixels ' ) // Change to asyncwrite
}
Let Processimagesasync () =
Printfn " Processimagesasync... "
Let Tasks = [ For I In 1 .. Numimages -> Processimageasync (I)]
Async. Run (async. parallel tasks) | > Ignore // Change to runsynchronously.
Printfn " Processimagesasync finished " AnotherCodeYes Code
Let Processimagesrepeats = 20
Let Repeatn pixels =
Pixels | > Array. Map ( Fun B -> B + 1uy)
Let Transformimage (pixels, imagenum) =
Printfn " Transformimage % d " Imagenum
// Frequent CPU computing operations
[ For I In 1 .. Processimagesrepeats -> Repeatn pixels] // The syntax here is pixels |> func. repeatn processimagesrepeats (array. map (fun B-> B + 1uy) due to func. the repeatn has been removed by the F # library, so it can only be replaced by this syntax
Let Processimagesync (I) =
Use instream = file. openread (sprintf " Image % d. tmp " I)
Let Pixels = array. zerocreate numpixels
Let Npixels = instream. Read (pixels, 0 , Numpixels)
Let Pixels ' = Transformimage (pixels, I)
Use outstream = file. openwrite (sprintf " Image % d. Done " I)
Outstream. Write (pixels ' . [0], 0, numpixels)