www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 9926] New: Add the `let` function.

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9926

           Summary: Add the `let` function.
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Keywords: pull
          Severity: enhancement
          Priority: P2
         Component: Phobos
        AssignedTo: nobody puremagic.com
        ReportedBy: GenericNPC gmail.com



`let` is a very fundamental building block in functional programming. It's
similar to variable declaration+assignment in imperative programming, but the
name-binding is done mid-expression, and the scope of the binding is limited to
the part of the expression where it is used.

These properties make it beneficial for imperative languages as well, since it
exempts the programmer from having to split a calculation, allowing them to put
a complex calculation with mid-results inside an expression, and to limit the
scope of the mid-results to that calculation.

Example:

    functionWithManyArguments(
            //Many arguments
            (1 + 2 * 3).let!(x => x * x * x + 2 * x * x + 3 * x)(),
            //More arguments
            );

Instead of:

    int x = 1 + 2 * 3;
    functionWithManyArguments(
            //Many arguments
            x * x * x + 2 * x * x + 3 * x,
            //More arguments
            );


Without the `let` function, not only are we forced to split the calculation and
put some of it before the call to `functionWithManyArguments`, but we are also
exposing the mid-result `x` that can be used in later parts of the program -
even though it is only needed for this calculation.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Apr 12 2013
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9926




See https://github.com/D-Programming-Language/phobos/pull/1255

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Apr 12 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9926


bearophile_hugs eml.cc changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bearophile_hugs eml.cc



In Haskell I prefer to use "where". The top-down approach offered by "where"
feels more clear to me.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Apr 13 2013
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9926





 In Haskell I prefer to use "where". The top-down approach offered by "where"
 feels more clear to me.
Well, it's a matter of both taste and circumstances, but the problem with `where` is that the name is used *before* it is declared. Why is that a problem? Because in D(like in many other languages) you need to declare variables before you use them - unless you go out of the imperative part and into the declarative part, which is not the case here. So, lets say we want to bind VALUE to NAME for the duration of BODY. In Haskell, we could use `let`: let NAME = VALUE in BODY or we could use `where`: BODY where NAME = VALUE Both are elegant. Now, if we remove the keywords, we get either NAME VALUE BODY or BODY NAME VALUE. My original goal was to keep the order of NAME VALUE BODY, but I couldn't do it without changing the language itself. When you write a lambda, you need to write the NAME and then immediately the BODY, so it must be either VALUE NAME BODY(which I picked for my function) or NAME BODY VALUE, which would just look weird: where!(NAME => BODY)(VALUE) (which, BTW, you could also do with `let`, and it will still look weird). Ofcourse, you could use mixins to implement `where` in the correct order - but then you will loose the closure... -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Apr 14 2013