F# passing an operator with arguments to a function

后端 未结 3 1122
小蘑菇
小蘑菇 2021-01-04 11:02

Can you pass in an operation like \"divide by 2\" or \"subtract 1\" using just a partially applied operator, where \"add 1\" looks like this:

List.map ((+) 1         


        
3条回答
  •  星月不相逢
    2021-01-04 11:28

    You could write a flip function, something like:

    let flip f x y = f y x
    
    List.map (flip (-) 1) [2;4;6]
    

    I may have the syntax wrong, I'm not terribly fluent in F#.

提交回复
热议问题