The quintessential F# style question

109 views
Skip to first unread message

Thomas Clarke

unread,
Sep 25, 2015, 10:38:01 AM9/25/15
to F# Discussions
Which? Why?

not( File.Exists( wDir+specFile))

not <| File.Exists( wDir+specFile)

File.Exists( wDir+specFile) |> not

not <| ( File.Exists <| wDir+specFile)

wDir+specFile |> File.Exists |> not

Isaac Abraham

unread,
Sep 25, 2015, 11:04:20 AM9/25/15
to F# Discussions

I hate nested ( ) and tend to avoid them where possible. I’d probably have picked the second one from your examples (although I know some people frown upon using <| as well), but I’d also consider separating this into two (particularly if you’ll be doing this elsewhere too): -

 

let fileDoesNotExist = not << File.Exists

fileDoesNotExist(wDir + specFile)

--
--
To post, send email to fsharp-o...@googlegroups.com
To unsubscribe, send email to
fsharp-opensou...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/fsharp-opensource
---
You received this message because you are subscribed to the Google Groups "F# Discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to fsharp-opensou...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

 

 

Adam Granicz

unread,
Sep 25, 2015, 11:26:31 AM9/25/15
to fsharp-o...@googlegroups.com
Guideline-wise: the first one, but without inner space in parentheses.  Also, negation is like an operator, so I'd normally use a space afterwards, so:

not (File.Exist(wDir+specFile))

(But use Path.Combine instead of "+")

In general, the forward pipe is an overkill for short (textually or time-wise) and related operations.  The back pipe is claimed to be hard to get for newcomers.  Mixing the two in the same expression is evil.

I, personally, prefer the second variant.  The reason is that:

1) I like to avoid parentheses
2) doing negation as last step seems to be easy to miss when reading code

Cheers,
Adam.

--
--
To post, send email to fsharp-o...@googlegroups.com
To unsubscribe, send email to
fsharp-opensou...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/fsharp-opensource
---
You received this message because you are subscribed to the Google Groups "F# Discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to fsharp-opensou...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Adam Granicz, IntelliFactory
www.intellifactory.com

Matthew Moloney

unread,
Sep 25, 2015, 11:28:24 AM9/25/15
to fsharp-o...@googlegroups.com
not(File.Exists(fn))

I know lots of ppl who hate parens. I don’t mind them.

I don’t like trailing ‘not’ as it sounds sarcastic in my head. Plus the reader may not see it.

In if then else conditions I typically go general to specific. not, the general existence of a file, the specific file name. 

I tend to use parens when accessing APIs targeting C# as you have to for overloaded methods anyway so I try to keep that consistent. 

I also try to write in a way that’s a bit more approachable to beginners. 

I dislike naming variables.

I would consider swapping the expressions in ‘then' and ‘else’ to get rid of the not.

You can also do

fn |> exists |> function | false -> …. | true -> ...
 

NOTE: File.Exists uses exceptions internally so it’s way slower than it has to be. If it’s a problem you can use an extern function which will be way faster.

Jared Hester

unread,
Sep 27, 2015, 9:11:55 AM9/27/15
to F# Discussions

not<<File.Exists<| wDir+specFile

is my favorite for the specific situation you presented, but a word of caution about <|
Unfortunately due to the associativity rules for operators being determined by their first ascii character instead of being set on a per operator basis the backpipe doesn’t work the way it really should
e.g.

not <| File.Exists <| wDir+specFile

will not work because < is left associative

However if you define a similar operator with the proper associativity you can do

let (^) (fn:'a->'b) x = fn x
let ( ** ) (fn:'a->'b) x = fn x

let x  = not ^ File.Exists ^ wDir+specFile
let x  = not ** File.Exists ** (wDir+specFile)

^ aka hat is the best option as it is right associative and has the highest precedence
** is also right associative but has a lower precedence than +, thus the need for parens.

not(File.Exists<| wDir+specFile)

is another fine option, but not my personal preference

File.Exists>>not<| wDir+specFile 

I find this last one amusing, but I wouldn’t use it

Casper Bollen

unread,
Nov 20, 2015, 5:47:11 AM11/20/15
to F# Discussions
My plus 1 for option 5:

// file string -> check exists -> negate
wDir
+specFile |> File.Exists |> not


Why, first I am a parens hater, second and related to the first, it makes the code more readable, not in a natural language sense, but in a code sense. It tells you exactly what's going on reading left to right.
Reply all
Reply to author
Forward
0 new messages