Array.get source index List .nth source index Seq .nth index source
我想使用管道运算符,似乎只有Seq:
s |> Seq.nth n
有没有办法使用与Array或List相同的符号?
Array.get source index List .nth source index Seq .nth index source
我想使用管道运算符,似乎只有Seq:
s |> Seq.nth n
有没有办法使用与Array或List相同的符号?
在List.nth的情况下,它不会改变太多,因为您可以使用Seq.nth,时间复杂度仍然是O(n),其中n是列表的长度:
[1..100] |> Seq.nth 10
在数组上使用Seq.nth并不是一个好主意,因为您丢失了随机访问.要保持O(1)运行Array.get的时间,可以定义:
[<RequireQualifiedAccess>] module Array = /// Get n-th element of an array in O(1) running time let inline nth index source = Array.get source index
一般来说,使用翻转功能可以减轻不同的参数顺序:
let inline flip f x y = f y x
您可以直接使用它上面的功能:
[1..100] |> flip List.nth 10 [|1..100|] |> flip Array.get 10