Right associative operator in F# [duplicate]

心不动则不痛 提交于 2019-12-22 08:27:27

问题


Sometimes I have to write:

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

I would really like to avoid the parentheses. In Haskell there is an operator for this ($)

It would look like this

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

I created a custom operator

let inline (^!) f a = f a

and now I can write it like this

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

Is there something like this in F#?


回答1:


There is no way to define custom operator with an explicitly specified associativity in F# - the associativity is determined based on the symbols forming the operator (and you can find it in the MSDN documentation for operators).

In this case, F# does not have any built-in operator that would let you avoid the parentheses and the idiomatic way is to write the code as you write it originally, with parentheses:

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

This is difference in style if you are coming from Haskell, but I do not see any real disadvantage of writing the parentheses - it is just a matter of style that you'll get used to after writing F# for some time. If you want to avoid parentheses (e.g. to write a nice DSL), then you can always named function and write something like:

myList |> List.iter id

(I understand that your example is really just an example, so id would not work for your real use case, but you can always define your own functions if that makes the code more readable).




回答2:


No, there's nothing like this in a standard F# library. However, you have almost done creating your own operator (by figuring out its name must start with ^).

This snippet by Stephen Swensen demonstrates a high precedence, right associative backward pipe, (^<|).

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

This single-liner from the linked page demonstrates how to use it:

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

And here is an example how to use it for multi-line functions. I find it most useful for real-world multi-liners as you no longer need to keep closing parenthesis at the end:

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



回答3:


In F# it's <|

So it would look like:

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


来源:https://stackoverflow.com/questions/21080190/right-associative-operator-in-f

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!