F #: Sequencing sequences in one Seq

I am trying to create a single sequence containing the contents of several files so that it can be sorted and then transferred to a graphic component. However, I am stuck trying to put the contents of each file together. The pseudo-code below does not compile, but I hope it will show the intention of what I'm trying to achieve.

Any help is very valuable.

open System.IO let FileEnumerator filename = seq { use sr = System.IO.File.OpenText(filename) while not sr.EndOfStream do let line = sr.ReadLine() yield line } let files = Directory.EnumerateFiles(@"D:\test_Data\","*.csv",SearchOption.AllDirectories) let res = files |> Seq.fold(fun x item -> let lines = FileEnumerator(item) let sq = Seq.concat x ; lines sq ) seq<string> printfn "%A" res 
+4
source share
2 answers

Basically you are trying to override Files.Readlines , which returns the contents of the file as seq<string> . This can then be combined with Seq.concat:

 let res = Directory.EnumerateFiles(@"D:\test_Data","*.csv",SearchOption.AllDirectories) |> Seq.map File.ReadLines |> Seq.concat 
+12
source

To fix the problem in the original approach, you need to use Seq.append instead of Seq.concat . The initial value for fold should be an empty sequence, which can be written as Seq.empty :

 let res = files |> Seq.fold(fun x item -> let lines = FileEnumerator(item) let sq = Seq.append x lines sq ) Seq.empty 

If you want to use Seq.concat , you need to write Seq.concat [x; lines] Seq.concat [x; lines] because concat expects a sequence of concatenated sequences. On the other hand, append just takes two sequences, so it's easier to use.

Another (simpler) way to concatenate all strings is to use yield! in sequence expressions:

 let res = seq { for item in files do yield! FileEnumerator(item) } 

This creates a sequence by repeating all the files and adding all the lines from the files (in order) to the resulting sequence. yield! construct yield! Adds all elements of a sequence to the result.

+4
source

Source: https://habr.com/ru/post/1402276/


All Articles