tl;dr
Should I implement each helper function like below?
let splitToTuple2 str =
match str.Split() with
| [| a; b |] -> a, b
| _ -> failwith "size must be 2"
let splitToTuple3 str =
match str.Split() with
| [| a; b; c |] -> a, b, c
| _ -> failwith "size must be 3"
let splitToTuple4 str =
match str.Split() with
| [| a; b; c; d |] -> a, b, c, d
| _ -> failwith "size must be 4"
motivation
The number of words is known in advance.
let str = "first second third" // each word is separeted by space
This below is enough to do things I need.
let arr = str.Split()
let a = arr.[0] // "first"
let b = arr.[1] // "second"
let c = arr.[2] // "third"
However, I'd like to do those by one-liner.
let [| a; b; c |] = str.Split() // WARNING: Incomplete pattern matches on this expression.
#nowarn "25"
is unfavorable because it affects whole file.
In this case, I need to split a string to string * string * string
to avoid warning.
let a, b, c = str |> splitToTuple3
Should I implement each helper function as mentioned at the beginning?
let words = str.Split()
?sscanf
so you can dolet (t1, t2, t3) = sscanf "%s %s %s" "first second third"
and so on with any tuple size. For handling failures you have thetry
version. You can also code the function yourself but it's not trivial.