Right associative operator in F #

Sometimes I have to write:

myList |> List.iter (fun x -> x)

I would really like to avoid parentheses. Haskell has an operator for this ($)

It will look like

myList |> List.iter $ fun x -> x

I created a custom statement

let inline (^!) f a = f a

and now I can write it like this:

myList |> List.iter ^! fun x -> x

Is there something similar in F #?

+4
source share
3 answers

It is not possible to define a custom operator with explicitly defined associativity in F # - associativity is determined based on the characters that make up the operator (and you can find it in the MSDN documentation for operators ).

F # , , :

myList |> List.iter (fun x -> x)

, Haskell, - , F # . (, DSL), - :

myList |> List.iter id

( , - , id , , ).

+6

, F # . , (, ^).

, , (^<|).

let inline (^<|) f a = f a

, :

{1..10} |> Seq.map ^<| fun x -> x + 3

, . , , :

myList
|> List.map
    ^<| fun x ->
        let ...
        returnValue
+8

In F#it<|

So it will look like this:

myList |> List.iter <| fun x -> x
0
source

All Articles