13

Let's say I have this code

let identifier = spaces_surrounded (many1Satisfy isLetter)

I was wondering if it there was any native F# function that allowed me to refactor it to

let identifier = spaces_surrounded $ many1Satisfy isLetter

that is, something such as

let ($) f1 f2 = f1 (f2)

(that is if I am not mistaken, my Haskell skills are not too sharp..).

1 Answer 1

20

The standard F# idiom for this is the forward pipe operator |> were you would rewrite

let identifier = spaces_surrounded (many1Satisfy isLetter)

as

let identifier = many1Satisfy isLetter |> spaces_surrounded 

you can also use the backward pipe operator <| if you want to maintain the original order, but this tends to be a little less common

let identifier = spaces_surrounded <| many1Satisfy isLetter
9
  • Hmm so it seems <| is what I was looking for. What is its definition? Aug 25, 2011 at 0:48
  • 6
    It is let inline (<|) f x = f x as seen here: github.com/fsharp/fsharp/blob/master/src/fsharp/FSharp.Core/…
    – Brian
    Aug 25, 2011 at 0:52
  • 3
    Note that defining your own operators a la Haskell is subject to F# rules of associativity/precedence, e.g. see research.microsoft.com/en-us/um/cambridge/projects/fsharp/…
    – Brian
    Aug 25, 2011 at 0:53
  • The (<|) operator associates the other way from ($), and the type-checker seems better at inferring what I went when I use (|>), so watch out for both of those. Aug 25, 2011 at 19:49
  • 2
    well "ignore <| stuff" reads nicer than the other way round but I guess it's just a preference (and looking at your greps I might be a weirdo ;) ). The only think I don't like is mixing <| and |>.
    – Random Dev
    Aug 31, 2011 at 6:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.