FSharp function parameter order

Since functions in FSharp with several parameters become integral to functions with only one parameter, should the signature Seq.filterbe

Seq.filter predicate source

? How different is it from

Seq.filter source predicate

thanks

+4
source share
4 answers

The first order (predicate, sequence) is more suitable for combinators of a sequence of chains using an operator |>. As a rule, you have one sequence to which you apply several operations / transformations, consider something like

xs |> Seq.map ... |> Seq.filter ... |> Seq. ...

... (, ) (, , ). ( , ) , () Seq , .

+10

:

Seq.filter predicate source 

Seq.filter soure predicate 

source
|> Seq.filter predicate

Seq.filter predicate

let isEven = Seq.filter (fun x -> x % 2 = 0)

source |> isEven

F # , , - OCaml. : N- , Seq

+6

Yes, it Seq.filtertakes a predicate, followed by a sequence for filtering. If you want to provide them in a different order, you can write a function to change the arguments:

let flip f a b = f b a

then you can write

(flip Seq.filter) [1..10] (fun i -> i > 3)

The existing order is more convenient because it makes a partial application more useful, for example.

 [1..3] |> Seq.map ((*)2) |> Seq.filter (fun i -> i > 2)
+5
source

and you also have || > for pipeline functions that take two arguments, or partially apply 2 arguments to a wider signature. :)

0
source

All Articles