F# convert Array2 into a list

前端 未结 1 712
南方客
南方客 2021-01-13 03:06

I\'m still new to functional programming so if I can\'t figure out how to do something I revert back to procedural style. I found a way to get around having to convert to a

1条回答
  •  清歌不尽
    2021-01-13 03:52

    Apparently in .Net, multi-dimensional arrays are IEnumerable (non-generic), and thus this works:

    let a2 = Array2.init 2 3 (fun x y -> (x+1)*(y+1))
    let l = a2 |> Seq.cast |> Seq.fold (fun l n -> n :: l) []
    printfn "%A" l
    

    EDIT: As Noldorin points out in a comment, this is even better:

    let l = a2 |> Seq.cast |> Seq.toList
    

    0 讨论(0)
提交回复
热议问题