bangjunyoung/SpiralMatrixSharp

Function `ofArray` not used.

Idrek opened this issue · 1 comments

Idrek commented

It seems that function ofArray in src/SpiralMatrixSharp/FSharp.fs is not used. Regardless of that, a rectangular array does not spread elements in correct order.

// It fails with expected output: [| [|3;4|]; [|7;8|]; [|0;0|]; [|0;0|] |]
[<Fact>]
let ``Test ofArray function`` () =
    let output = [| [|1;2|]; [|3;4|]; [|5;6|]; [|7;8|] |] |> array2D
    Assert.Equal(output, Array2D.ofArray 4 2 [|1..8|])

I think array should be a square one, so better to accept an argument and check it, like:

let ofArray (side: int) source =
    if Array.length source <> pown side 2 then
        invalidArg "source" "must be a square array"

    let array2D = Array2D.zeroCreate side side
    source
    |> Array.iteri (fun i elem -> array2D.[i / side, i % side] <- elem)

    array2D

with test:

[<Fact>]
let ``Test ofArray function`` () =
    let expected = [| [|1;2;3|]; [|4;5;6|]; [|7;8;9|] |] |> array2D   
    Assert.Equal(expected, Array2D.ofArray 3 [|1..9|])

    Assert.Throws<System.ArgumentException>(
        fun () -> Array2D.ofArray 3 [|1..7|] |> ignore) 
    |> ignore

Thanks for noticing this.

I'm not sure why this unused and buggy code was committed in the first place. I'll fix a bug and remove it from the source.