2
\$\begingroup\$

I've played with F# for over a year, but I've only recently begun tentatively using it for professional work. I'm concerned that my c-style OOP principles are blinding me to the advantages of a functional language. I wrote the following code and it works just fine, but I feel like I'm just not quite using the language to its full potential. It still feels too C#-ish to me.

open System
open System.Configuration
open System.DirectoryServices
open System.DirectoryServices.ActiveDirectory
type adEntry =
 static member prop (name:string) (entry:SearchResult) =
 ( entry.Properties.Item( name ).Item( 0 ).ToString() )
 static member formatUserName (format:string) (entry:SearchResult) =
 let displayName = adEntry.prop "displayName" entry
 let first = displayName.Substring ( 2 + displayName.IndexOf ", " )
 let last = displayName.Substring ( 0, displayName.IndexOf ", " )
 let ntid = adEntry.prop "cn" entry
 format.Replace( "%f", first )
 .Replace( "%l", last )
 .Replace( "%i", ntid )
type ad =
 static member query (filter:string) = 
 use searcher = new DirectorySearcher( filter )
 Seq.cast<SearchResult> ( searcher.FindAll() )
[<EntryPoint>]
let main argv = 
 let groupQuery = "memberof=CN=" + ( ConfigurationManager.AppSettings.Item "ADGroup" ) + ",OU=XYZ Groups,DC=xyz,DC=com"
 let results = 
 Seq.map
 ( adEntry.formatUserName "%f %l-%i" )
 ( ad.query groupQuery )
 Seq.iter
 ( printf "%s\n" )
 results
 ignore ( Console.ReadKey true )
 0

Am I missing out on F#'s power or maintainability by using types in the code above?

asked Dec 17, 2013 at 15:01
\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

I think that functional programming shows itself most when you're manipulating data. You almost don't have that in your code, so there isn't much to change.

Though there are some small things that you could change:

  1. You could create a type for user name. This would make sense if you wanted to do other things that formatting with them, otherwise, it would probably just complicate the code for no good reason.
  2. You have just three methods, I don't see much reason to put them into classes, you don't have to do that in F#. If you still think that it's worth it, you should use the F# equivalent of static classes, which are modules, since all your functions are static.
  3. Similarly, you don't need main, you could just put all that code at the top level.
  4. To create groupQuery, I would use sptrintf, I think it makes the code more readable.
  5. Instead of Seq.iter, you could use for..in.
  6. ignore is usually written using |>, e.g. Console.ReadKey true |> ignore.
answered Dec 17, 2013 at 17:41
\$\endgroup\$
1
  • \$\begingroup\$ I originally had my methods as "global" functions, but organized them into "static classes" in anticipation of this code growing in size as I add new features. How are functional programs typically organized? \$\endgroup\$ Commented Dec 17, 2013 at 17:46

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.