Is there a good reason for a different argument order in functions getting N-th element of Array, List or Seq:
Array.get source index
List .nth source index
Seq .nth index source
I would like to use pipe operator and it seems possible only with Seq:
s |> Seq.nth n
Is there a way to have the same notation with Array or List?
I don't think of any good reason to define
Array.get
andList.nth
this way. Given that pipeplining is very common in F#, they should have been defined so that thesource
argument came last.In case of
List.nth
, it doesn't change much because you can useSeq.nth
and time complexity is stillO(n)
wheren
is length of the list:It's not a good idea to use
Seq.nth
on arrays because you lose random access. To keepO(1)
running time ofArray.get
, you can define:In general, different argument order can be alleviated by using
flip
function:You can use it directly on the functions above:
Since Pad and bytebuster answered your last question I will focus on the why part.
This is based my current knowledge and not historical facts.
Since F# derived from OCaml and OCaml has Array and List but not Seq and F# uses |> for natural pipelining and type checking and OCaml lacks the pipleline operator, the authors of F# made the switch for Seq. But obviously to be backward compatablie with OCaml they did not switch everything.
Just use backward pipe operator:
Since both operators are left associative,
x |> f <| y
is parsed as(x |> f) <| y
, and this does the trick.Backward pipe operator is also useful if you want to remove parentheses:
f (very long expression)
can be replaced withf <| very long expression
.