问题
How can I overload .[] for F# array to slice an array based on an arbitrary index array?
For example:
let x = [|1..10|];
let indx = [|4;1|];
although
[| for i in indx ->x.[i]|]
would work, it would be nicer to be able using x.[indx]
directly.
回答1:
You can always write an F# extension method to get close on the syntax
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)
but since arrays already have an indexer it doesn't appear there's a way to overload/change it.
来源:https://stackoverflow.com/questions/2357006/f-slicing-array-by-indices-array