Skip to content

Instantly share code, notes, and snippets.

@isaacabraham
Forked from chgeuer/clean_arch.fs
Last active April 23, 2022 11:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save isaacabraham/78fe32f350dcacac2d7fd86144789258 to your computer and use it in GitHub Desktop.
Save isaacabraham/78fe32f350dcacac2d7fd86144789258 to your computer and use it in GitHub Desktop.
// F# version of https://twitter.com/gsferreira/status/1516827091127394309/
open System
open System.IO
type PotentialProcessingError = StringMissing | StringTooShort
let application (str: string) =
match str with
| str when str.Length < 5 -> Error StringTooShort
| str -> Ok str
let infrastructure text =
let path = Path.Combine(Path.GetTempPath(), "my-text.txt")
printfn $"Storing at {path}"
File.WriteAllText(path, text)
let console () =
printfn "What message do you want to store?"
match Console.ReadLine() with
| null | "" -> None
| str -> Some str
console ()
|> Option.map application
|> Option.defaultValue (Error StringMissing)
|> Result.map infrastructure
|> printfn "Stored successfully? %A"
@chgeuer
Copy link

chgeuer commented Apr 22, 2022

Actually, figured that Ctrl-Z on Windows (EOF) makes the string null, so console would return Some null...

let console () =
    printfn "What message do you want to store?"
    match Console.ReadLine() with
    | null -> None
    | "" -> None
    | str -> Some str

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment