2010-03-01 39 views
2

如何重載。[]爲F#數組根據任意索引數組切片數組?F#按指數排列數組切片數組

例如:

let x = [|1..10|]; 
let indx = [|4;1|]; 

雖然

[| for i in indx ->x.[i]|] 

會的工作,這將是更好的使用直接向x.[indx]能。

+2

您的示例不執行*切片* - 它只檢索數組的不同元素。切片會f.ex.是'x。[2..6]' – Frank 2010-03-01 16:04:45

回答

5

你總是可以編寫一個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) 

但因爲數組已經有了它不會出現在索引有辦法過載/更改。