Last active
March 4, 2018 06:16
-
-
Save jwosty/bc7e1723477ea4c300e1fe6a2581d687 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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