hubFS: THE place for F#

. . . are you on The Hub?
Welcome to hubFS: THE place for F# Sign in | Join | Help
in Search

Factoring out constant expressions from functions

Last post 07-09-2008, 5:13 by Stephan. 4 replies.
Sort Posts: Previous Next
  •  07-08-2008, 4:58 6329

    Factoring out constant expressions from functions

    Hi,

    What pattern do you guys use to factor out constant expressions from functions, like regex in the following definition?

    let escape s =
        let regex = System.Text.RegularExpressions.Regex("[^a-zA-Z0-9_]")
        regex.Replace(s, "_")


    I've always been using

    let escape1 =
        let regex = System.Text.RegularExpressions.Regex("[^a-zA-Z0-9_]")
        fun s ->
            regex.Replace(s, "_")


    because it keeps related things together and doesn't clutter the global namespace. The problem is that this version is less efficient than

    let escape2regex = System.Text.RegularExpressions.Regex("[^a-zA-Z0-9_]")
    let escape2 s =
        escape2regex.Replace(s, "_")


    because escape1 is not compiled into a static function but a function object (even if it is never used as a function object).

    What's the usual "F#-way" to do this? Define an extra module with implementation details?

    Best regards,
      Stephan

  •  07-08-2008, 5:43 6330 in reply to 6329

    Re: Factoring out constant expressions from functions

    Hi,

    you can use the second method, and then don't include it in the signature file. Or you may declare escape2regex as private as in let private escape2regex = System.Text.RegularExpressions.Regex("[^a-zA-Z0-9_]") which ensures this definition is only callable from within the module.

    Making sure it is both declared private and missing from the signature file makes it easy to see which definition won't appear in the signature or is not callable from outside when you read the implementation file, and it avoids cluttering the signature file...

  •  07-09-2008, 2:31 6333 in reply to 6330

    Re: Factoring out constant expressions from functions

    Thanks Julien. That's probably the cleanest solution. Unfortunately, I'm a bit lazy with regard to writing and keeping up-to-date signature files...

    Stephan

  •  07-09-2008, 4:05 6334 in reply to 6333

    Re: Factoring out constant expressions from functions

    A fairly recent addation to the language is that you can now use the "private" keyword instead of signature files:

    let private x = "toto"

    It's not that well documented but there is a section in the F# manual about it:
    http://research.microsoft.com/fsharp/manual/spec2.aspx#_Toc202383865

    Not sure if this is the prefered way of doing this or just an alterative to signature files.

    Robert Pickering
    http://strangelights.com
  •  07-09-2008, 5:13 6336 in reply to 6334

    Re: Factoring out constant expressions from functions

    The problem with "private" declarations is that code completion doesn't yet filter them out.
View as RSS news feed in XML
Powered by Community Server, by Telligent Systems