Reputation: 31
I have this code:
public async Task CreateFileAsync(string filePath, byte[] bytes)
{
using (var sourceStream = System.IO.File.Open(filePath, FileMode.OpenOrCreate))
{
sourceStream.Seek(0, SeekOrigin.End);
await sourceStream.WriteAsync(bytes, 0, bytes.Length).ConfigureAwait(false);
}
}
I want to write it on F#, I get as far as this before I can't figure out what to do:
module myModule
open System.IO;
let CreateFileAsync (filePath: string, bytes : byte[]) =
use sourceStream = File.Open(filePath, FileMode.OpenOrCreate)
|> sourceStream.Seek(0, SeekOrigin.End);
I have searched around but there are a couple of concepts here and I can't put them all together.
Upvotes: 3
Views: 137
Reputation: 243126
You can use the async { .. }
workflow:
let createFileAsync (filePath, bytes) = async {
use sourceStream = System.IO.File.Open(filePath, FileMode.OpenOrCreate)
sourceStream.Seek(0, SeekOrigin.End)
do! sourceStream.AsyncWrite(bytes, 0, bytes.Length) }
This is fairly imperative code, so it does not look very different in F#.
Upvotes: 3