F#按索引数组切片数组 [英] F# slicing array by indices array
本文介绍了F#按索引数组切片数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何为F#数组重载.[]以基于任意索引数组对数组进行切片?
How can I overload .[] for F# array to slice an array based on an arbitrary index array?
例如:
let x = [|1..10|];
let indx = [|4;1|];
尽管
[| for i in indx ->x.[i]|]
可以,直接使用x.[indx]
会更好.
would work, it would be nicer to be able using x.[indx]
directly.
推荐答案
您始终可以编写一个F#扩展方法以接近语法
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.
这篇关于F#按索引数组切片数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文