...Вона блює і співає (bytebuster463) wrote in ru_fsharp,
...Вона блює і співає
bytebuster463
ru_fsharp

Point-free function application in F#

В коммуне по-прежнему тихо, потому подумалось, что эта мини-статья будет кому-то интересна. Не все знают, как использовать duck-typing для применения не-последнего аргумента.
Модератору: не знаю, какие метки ставить. :( И не знаю: а ссылки на SO вообще можно, да? :-)

Originally posted by bytebuster463 at Point-free function application in F#
Pointless notation in F# is a powerful mechanism for coding, allowing the source be free of burdening arguments. Combined with strong typing it makes F# code very expressive.
Say you have (+) operator which has type of int -> int -> int.
You may define your custom function:

let add2_version1 x = x + 2

This function will have type of int -> int which means it takes an int and returns an int.
This syntax is equal to:

let add2_version2 x = (+) 2 x

or

let add2_version3 x = ((+) 2) x

As we see, ((+) 2) is a function itself, and x is applied to it. So we can simplify our function:

let add2_version4 = (+) 2

But what if our operator is not symmetric, and we need to apply the first argument? Look at this question on StackOverflow:

How can I create a partial function application for a non-symmetric operator such as the modulus operator with regards to the first argument without any argument names in F#? My first attempt was let mod10 = (%) 10 which of course translates to mod10(x) = 10 mod x instead of the desired mod10(x) = x mod 10.

Certainly, this is possible!

let mod10 = (%) >> (|>) 10

How did we get it? Simple! The following expressions are equal:

let m1 x = x % 10                    
let m2 x = (%) x 10                  // x (op) y = (op) x y
let m3 x = ((%) x) 10                // f x y = (f x) y
let m4 x = 10 |>         ((%) x)     // f x = x |> f
let m5 x = ((|>) 10)     ((%) x)     // x |> f = (|>) x f
let m6 x = ((%) x)    |> ((|>) 10)   // f x = x |> f
let m7 x = (x |> (%)) |> ((|>) 10)   // (op) x = x |> (op)
let m8 x = x |> ((%)  >> ((|>) 10))  // f(x) |> g = x |> (f >> g)
let m9   =       (%)  >> ((|>) 10)   // remove formal argument
let m10  =       (%)  >>  (|>) 10    // remove unnecessary parenthesis

ЗЫ. Если Вам понравилась эта статья, можно лайкнуть по ссылке :-)
  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your IP address will be recorded 

  • 10 comments