📎 Webclip
Introduction to Functional Programming in F# - Part 6
The post walks through reading external data in F# with Seq, parsing a pipe-delimited customers file, and separating file access from parsing and output. It then refactors the import flow so the file reader can be replaced with a fake implementation for testing.
Reading notes#
readFileusesSystem.IO,StreamReader, and a sequence expression to return the file contents line by line.Seq.iteris used inmainto print the loaded lines to the terminal.readFileis changed to returnResult<seq<string>, exn>so file loading errors can be handled.importmatches on theResult, printing either the data or the error message.- A
Customerrecord is introduced with fields for the six columns in the CSV file. parseskips the header row, splits each line on|, and keeps only lines that match the expected shape.Seq.choose idremovesNonevalues and unwrapsSomevalues into a sequence ofCustomerrecords.parseLineis extracted from the mapping step to make the parsing logic separate and reusable.importis refactored to accept afileReaderfunction so the code can work with different data sources.- A
FileReadertype alias is introduced for the function signaturestring -> Result<string seq, exn>. readFileis rewritten to fit theFileReadersignature.- Partial application is used to create
importWithFileReaderfromimport readFile. - A
fakeFileReaderis shown as a test double that returns fixed CSV data without touching the file system. - The final code combines file reading, parsing, output, and error handling, and the post says the next step will be data validation.
