I've hacked together some code to read the content from a sequence of files:
let fileContents =
[ "filename1"; "filename2"; "etc" ]
|> Seq.map(fun file -> async {
use fs = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read)
let data = Array.create(int fs.Length) 0uy
let! _ = fs.AsyncRead(data, 0, data.Length)
return data
})
|> Async.Parallel
|> Async.RunSynchronously
|> Seq.map System.Text.Encoding.ASCII.GetString
It works, but I'm not really sure about it. For one thing, is async
actually giving me any benefit here? Also inside the async
function I need to wait for AsyncRead
to finish, but I don't need the returned value from it. Is there a better way of doing this than let! _ = ...
?