Returning arrays of different dimensions from one function; is it possible in F#?

后端 未结 2 1831
别跟我提以往
别跟我提以往 2020-12-19 17:21

I am trying to convert some Python to F#, specifically numpy.random.randn.

The function takes a variable number of int arguments and returns arrays of different dim

2条回答
  •  执念已碎
    2020-12-19 17:54

    Although Tomas' suggestion to use overloading is probably best, .NET arrays do share a common sub-type: System.Array. So what you want is possible.

    member self.differntarrays ([] dimensions: Object[]) : Array =
        match dimensions with
        | [| dim1 |] ->      
             [| 
                1 
             |] :> _
        | [| dim1; dim2 |] -> 
            [|
               [| 2 |], 
               [| 3 |] 
            |] :> _
        | _ -> failwith "error"
    

提交回复
热议问题