F#按索引数组切片数组

aha*_*ala 2 f#

我如何重载.[] F#数组基于任意索引数组切片数组?

例如:

let x = [|1..10|]; 
let indx = [|4;1|];
Run Code Online (Sandbox Code Playgroud)

虽然

[| for i in indx ->x.[i]|] 
Run Code Online (Sandbox Code Playgroud)

会工作,能x.[indx]直接使用会更好.

Bri*_*ian 6

您始终可以编写F#扩展方法来接近语法

let a = [| 1..10 |]
let idx = [|4;1|]

type 'T ``[]`` with   //'
    member this.Slice(indices:int array) =
        [| for i in indices -> this.[i] |]

printfn "%A" (a.Slice idx)
Run Code Online (Sandbox Code Playgroud)

但由于数组已经有一个索引器,它似乎没有出现过载/更改它的方法.