Next, every value is also an object and object-oriented paradigm (with messages call) can be used inside F#
programs.
As an illustration, the System.String
class offers many operations to extract or transform texts as shown below:
"1,2,3" |> fun str -> str.Split [| ',' |]
val it : string array = [| "1"; "2"; "3"|]
Arrays are fixed size list and have similar operations to the ones available on lists:
let split (sep:char) (str:string) = str.Split [| sep |]
"1,2,3" |> split ',' |> Array.map int |> Array.filter (fun v->v%2=0)
|> Array.map string |> Array.reduce (fun s v->s+","+v)
As a remark, the preceding code integrates an "unsplit" function that can be defined as follow:
let unsplit sep = Array.reduce (fun s v->s+(string sep)+v)
"1,2,3" |> split ',' |> unsplit ','
As an exercise, how to read/write CSV data ?