3

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.

1 Answer 1

3

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#.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.