Overly Excessive
Overly Excessive

Reputation: 2125

Folding an array of arrays

I'm trying to fold an array of arrays of strings into a single string but I'm not having much luck. Unfortunately it seems Array.reduce expects my lambda to return an array of strings because it is an array of array of strings.

I'm getting :

Line error 37: The type 'string[]' does not match the type 'string'

This is the offending line

(fold state) + (fold item)

Because it's expecting the lambda to return a string[]

Here is the code:

let splitStr (seperator: string[]) (str: string) = str.Split(seperator, StringSplitOptions.None)
let convertFile fileName =
    let arrayToTransaction arr =
        let rec foldArray index (sb: StringBuilder) (arr:string[]) =
            if index > 5 then sb.ToString()
            else 
                let text = 
                    match index with
                    | 0 -> sb.Append(DateTime.Parse(arr.[1]).ToString("dd/MM/yy", CultureInfo.InvariantCulture))
                    | 1 -> sb.Append(arr.[0].Substring(0, arr.[0].IndexOf(',')).Trim())
                    | 2 -> sb.Append("Test")
                    | 3 -> sb.Append("Test")
                    | 4 -> sb.Append(Single.Parse(arr.[2].Substring(arr.[2].IndexOf('-') + 1)).ToString("F2", CultureInfo.InvariantCulture))
                    | _ -> sb.Append(String.Empty)
                foldArray (index + 1) (text.Append(",")) arr

        arr 
        |> Array.map (splitStr [|"\n"|])
        |> Array.reduce (fun state item -> let fold x = foldArray 0 (new StringBuilder()) x
                                           (fold state) + (fold item))

    File.ReadAllText(fileName)
    |> splitStr [|"\r\n\r\n"|]
    |> arrayToTransaction

Upvotes: 0

Views: 352

Answers (1)

Gus
Gus

Reputation: 26204

Your lambda in the Array.reduce must return a string[] since the signature of the lambda is 'T->'T->'T and the first 'T is already unified as string[] so the result should also be a string[]

Upvotes: 2

Related Questions