Skip to content

Instantly share code, notes, and snippets.

@jwosty
Last active March 4, 2018 06:16
Show Gist options
  • Save jwosty/bc7e1723477ea4c300e1fe6a2581d687 to your computer and use it in GitHub Desktop.
Save jwosty/bc7e1723477ea4c300e1fe6a2581d687 to your computer and use it in GitHub Desktop.
open System
open System.IO
open System.Text
let tryReadFile path = async {
if File.Exists path then
use file = File.OpenRead path
let! bytes = file.AsyncRead (int file.Length)
return Some (Encoding.UTF8.GetString bytes)
else return None }
[<EntryPoint>]
let main args =
Async.RunSynchronously <| async {
match Array.tryItem 0 args with
| Some path ->
// old syntax
//let! text = tryReadFile path
//match text with ...
//| Some text -> Console.Write text
//| None -> Console.Error.WriteLine (sprintf "%s: No such file or directory" path)
// using match!
match! tryReadFile path with
| Some text -> Console.Write text
| None -> Console.Error.WriteLine (sprintf "%s: No such file or directory" path)
| None -> () }
0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment