Created
December 29, 2016 06:26
-
-
Save akucheck/e0ff316e516063e6db224ab116501498 to your computer and use it in GitHub Desktop.
Expand rows, given count, using Seq.unfold
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// usage: unfoldTest1 inFile outFile | |
(* | |
expected input is below | |
some text once,1 | |
some text twice,2 | |
some text thrice,3 | |
intended output: | |
some text once,1 | |
some text twice,1 | |
some text twice,1 | |
some text thrice,1 | |
some text thrice,1 | |
some text thrice,1 | |
*) | |
open System | |
open System.IO | |
// ======================================================== | |
// define any constants, values, etc | |
// ======================================================== | |
// ======================================================== | |
// define all types, functions | |
// ======================================================== | |
let createTupleWithCount (line:string) = | |
let lineArray = line.Split(',') | |
let count = int32 lineArray.[1] | |
lineArray.[1] <- string 1 | |
let updatedLine = lineArray |> String.concat "," | |
// printfn "%A %d" updatedLine count | |
(updatedLine, count) | |
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) | |
// cleanup | |
let flushAndClose (strWriter : StreamWriter) = | |
strWriter.Flush |> ignore | |
strWriter.Close |> ignore | |
// ======================================================== | |
// ** begin main ** | |
// ======================================================== | |
[<EntryPoint>] | |
let main argv = | |
// TODO: robustify handling of command line args | |
// ensure correct # of arguments | |
// ensure input file exists | |
if argv.Length <> 2 then | |
printfn "%A" "Dude! I need an Inputfile, an Outputfile" | |
// setup ========================================== | |
let inFile = argv.[0] | |
use outFile = new StreamWriter(argv.[1]) | |
// let barTarget = int32 argv.[2] | |
File.ReadLines(inFile) | |
|> Seq.map createTupleWithCount | |
|> Seq.unfold expandRows | |
|> Seq.iter outFile.WriteLine | |
// cleanup ======================================== | |
flushAndClose outFile | |
0 // return an integer exit code | |
// ======================================================== | |
// ** end main ** | |
// ======================================================== | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment