Mam plik CSV z dwiema kolumnami, tekstem i liczbą. Celem jest przekształcenie plików z tego:W języku F #, Jak korzystać z Seq.unfold w kontekście większego potoku?
some text once,1
some text twice,2
some text thrice,3
do tego:
some text once,1
some text twice,1
some text twice,1
some text thrice,1
some text thrice,1
some text thrice,1
razy powtarzając każdy Liczba linii i rozprzestrzeniania licznik nad tym wielu liniach.
To wydaje mi się dobrym kandydatem do Seq.unfold, generując dodatkowe linie, gdy czytamy plik. Mam następującą funkcję generatora:
let expandRows (text:string, number:int32) =
if number = 0
then None
else
let element = text // "element" will be in the generated sequence
let nextState = (element, number-1) // threaded state replacing looping
Some (element, nextState)
FSI daje w wyniku funkcją następujący podpis:
val expandRows : text:string * number:int32 -> (string * (string * int32)) option
Wykonywanie następujących czynności w FSI:
let expandedRows = Seq.unfold expandRows ("some text thrice", 3)
daje oczekiwany:
val it : seq<string> = seq ["some text thrice"; "some text thrice"; "some text thrice"]
The Pytanie brzmi: jak podłączyć to do kontekstu większego potoku ETL? Na przykład:
File.ReadLines(inFile)
|> Seq.map createTupleWithCount
|> Seq.unfold expandRows // type mismatch here
|> Seq.iter outFile.WriteLine
Poniższy błąd występuje w rozwinięciu w kontekście potoku.
Type mismatch.
Expecting a 'seq<string * int32> -> ('a * seq<string * int32>) option'
but given a 'string * int32 -> (string * (string * int32)) option'
The type 'seq<string * int 32>' does not match the type 'string * int32'
Spodziewałem się, że program expandRows zwraca ciąg seq, jak w moim izolowanym teście. Ponieważ nie jest to ani "oczekiwanie", ani "dane", jestem zdezorientowany. Czy ktoś może wskazać mi właściwy kierunek?
GIST kodu jest tutaj: https://gist.github.com/akucheck/e0ff316e516063e6db224ab116501498
Bardzo pomocne wyjaśnienie; dokładnie to, czego potrzebowałem. dzięki! – akucheck
Cieszę się, że mogę pomóc. –