0

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
2
  • I am pretty sure we don't have 37 lines here. Please show which line has the error Commented Nov 7, 2014 at 9:44
  • I've added a clarification on the offending line. Commented Nov 7, 2014 at 9:47

1 Answer 1

2

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[]

Sign up to request clarification or add additional context in comments.

2 Comments

Yeah, I managed to solve it by mutating the collection before reducing it with Array.map.
Great, the error message was a bit confusing because it was marking only the last term of the expression.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.