hubFS: THE place for F#

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

An Idiom for Designing Functions

Last post 11-20-2009, 11:12 by Brian27. 3 replies.
Sort Posts: Previous Next
  •  01-24-2009, 15:34 8708

    An Idiom for Designing Functions

    While messing around with stuff, I noticed that a particular setup for functions is a good candidate for an idiom.  It's a compositional style.  First, an example of a function that doesn't use it.  This is a function that does 3 things.  It squares the input, multiplies it by 3, and then adds 7 to the result.

    let somefun k =
        let step1 = k*k

        let step2 = 3*k

        step2 + 7


    Pretend of course that the steps are more complex.  Here's a version of the function which uses the compositional idiom:

    let somefun =
        let step1 k = k*k

        let step2 k = 3*k

        let step3 k = k + 7

        step1 >> step2 >> step3


    (Note I haven't tested any of this code.)

    So somefun is a composition of the individual steps.  This is nice for a variety of reasons.  When you're writing the function all you have to do is focus on making the individual steps work, and then you just put them together.  It also makes the "big picture" more explicit in the composition, which will help you reason about the program.

    There is a "rule of thumb" about splitting code that is too long into multiple functions.  In other languages this has a significant cost of its own with regard to the complexity of the code.  But in languages with first class functions like F#, you can accomplish it while keeping all the functions in the relevant scope/indentation.  In fact it reads quite nicely in such languages.

    Keep in mind you can pass multiple values from one function to the next.
  •  01-26-2009, 0:02 8709 in reply to 8708

    Re: An Idiom for Designing Functions

    I like the idiom, but I doubt whether it is very practical. Many cases look like this:


    let somefun x =
    let step1a = f0 x
    let step1b = f1 x
    let step1c = f2 x
    f3 (step1a, step1b, step1c)



    This would require your code to be rewritten as:


    let somefun x =
    let step1 x = f0 x, f1 x, f2 x
    let step2 (a, b, c) = f3 (a,b,c)
    step1 >> step2



    This seems super in this simple example, but if f0, f1, f2 are complex, it is probably much less clear.

    Of course we could refactor and define f0, f1, f2 separately, but I'm not convinced that we are really winning with such an approach.

    JMTC
  •  09-03-2009, 20:44 11745 in reply to 8709

    Re: An Idiom for Designing Functions

    Normal 0 false false false MicrosoftInternetExplorer4 /* Style Definitions */ table.MsoNormalTable {mso-style-name:"Table Normal"; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-parent:""; mso-padding-alt:0in 5.4pt 0in 5.4pt; mso-para-margin:0in; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Times New Roman"; mso-ansi-language:#0400; mso-fareast-language:#0400; mso-bidi-language:#0400;}

    what a great info, thanks.
    Thanks so much for this. I appreciate the effort. It really helps a lot.

      Normal 0 false false false MicrosoftInternetExplorer4 /* Style Definitions */ table.MsoNormalTable {mso-style-name:"Table Normal"; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-parent:""; mso-padding-alt:0in 5.4pt 0in 5.4pt; mso-para-margin:0in; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Times New Roman"; mso-ansi-language:#0400; mso-fareast-language:#0400; mso-bidi-language:#0400;}

    http://tattooremovalcreamblog.com/" target="_blank" title="http://tattooremovalcreamblog.com/"> free tattoo removal cream before and after lazer prices this is the blog for tattoo removal cream prices and cost. the best cream to pruchasehttp://tattooremovalcreamblog.com/" target="_blank" title="http://tattooremovalcreamblog.com/"> free tattoo removal cream before and after lazer prices

     

  •  11-20-2009, 11:12 12329 in reply to 8708

    Re: An Idiom for Designing Functions

    Hi,

    There's something appealing in the form you suggest - also something familiar.  I haven't learned enough about F# yet to comment on the practicality of using this for more complicated scenarios (as the other response has).

    The familiar bit, relates to a language I was fond of called Forth (semi-compiled, trans-level, iterative, stack-based language, that sort of died on the vine in the 1990's).  As Forth built program/solutions though a vocabulary of words defined by iterating combinations of previously defined (more primitive), words, the structure you suggest would look something like:

    Step4:
    Step1
    Step2
    Step3 ;

    with the sub-steps mentioned in the other reply as:

    Step1:

    Step1a
    Step1b
    Step1c ;
    Of course, Step1a, etc..., would be defined before Step1 - but it would be fine to use the more abstract definition of Step1 to get the thing worked out conceptually; then go back and define the lower level words the high level word requires.

    The closer F# code could come to Forths' structure, the more I'd like it - but mine is not the typical programmer/developer mindset.

    Thanks for posting your idea,

    BRN..


View as RSS news feed in XML
Powered by Community Server, by Telligent Systems