Shout out to D at cppcon, when talkign about ranges.

H. S. Teoh via Digitalmars-d digitalmars-d at puremagic.com
Wed Sep 30 15:47:33 PDT 2015


On Wed, Sep 30, 2015 at 10:32:16AM -0400, Andrei Alexandrescu via Digitalmars-d wrote:
> On 09/30/2015 09:31 AM, Steven Schveighoffer wrote:
> >On 9/29/15 9:45 PM, deadalnix wrote:
> >>https://www.youtube.com/watch?v=mFUXNMfaciE
> >>
> >>From
> >>http://wiki.dlang.org/Component_programming_with_ranges
> >>
> >>Congrat H. S. Teoh
> >
> >Ditto!
> >
> >Here is the link to the video at the time where D is credited, very
> >nice compliment:
> >
> >https://youtu.be/mFUXNMfaciE?t=115
[...]

I watched most of the video (mainly the first half where he goes through
the C++ version of the code), and I have to confess I couldn't help
noticing just how ugly the C++ syntax is.

I mean, overloading operator|() for chaining, seriously?  UFCS is
superior any day, IMNSHO.

And overloading operator%() for string interpolation, ugh. (Though this
one seems to be a Boost thing, not specific to the ranges proposal.)

And this is on top of the usual ugliness of C++ template syntax. Not to
mention the RANGE_FOREACH macro (I assume it's a macro), where D's
foreach has supported natively for years now.  (D has seriously ruined
my life; I simply can't bring myself to go back to C++ anymore. At least
not voluntarily.)

Also, this seems to confirm that C++ is gradually falling to the
position where it's playing catch-up with respect to innovations in
newer languages like D and Rust. The fact that ranges are being proposed
for the C++ standard library is a big endorsement of D, IMO.

It's also clear that the imperative crowd is slowly but surely moving
towards a more functional approach to programming. As the presenter
said, having mutable state and branching/looping is what makes code
explode in combinatorial complexity, making it hard to analyse, hard to
prove correct, and more prone to bugs.

I feel like he didn't hit on the central point of my article, though.
The whole thrust behind that article was the idea of impedance mismatch
between code structure and data structure, an idea I picked up from the
Jackson Structured Programming paradigm in my college years.  When code
structure and data structure don't map 1-to-1, there's a structure
conflict, and the usual response is to invent ad hoc hacks to compensate
for the mismatch, usually in the form of boolean flags or other such
band-aids. JSP, however, proposes that the *right* way to resolving the
conflict is to change the program structure so that it *does* match the
structure of the data.  I have applied this in various forms throughout
my career, and have found that it works very well not only in
solidifying the way I write loops, but it also gives me a tangible
handle on how to better shape the large-scale of my programs.

Where ranges come in, is that ranges, due to their modular, encapsulated
nature and their standardized API, essentially guides the programmer
into the correct data-matching code structure in an unobtrusive, even
spontaneous manner.  In order to make range-based code work at all, you
pretty much have to write your code in a way that exactly matches your
data -- hacks like boolean flags and other such ugliness are
spontaneously excluded (unless you deliberately warp your code to bring
them back in -- and who in their right mind would want to do that?).  To
me, this is what makes ranges such a great innovation: you get the best
of JSP (which I suspect almost nobody these days has even heard about!),
many of the benefits of pure functional programming, code readability,
testability, etc., all in one package. And it's not even hard to use.


> One great action item is to backport some of Eric's ideas to the D
> example.  Currently Eric's final code looks nicer than the D code. --
> Andrei

Do you have a list of ideas that should be backported?

One that I can think of is using transpose() to do the columnar pasting,
instead of the more specific function that I wrote specifically for that
purpose. That was very clever, since by making all the formatted months
exactly the same dimensions, it avoided the need to write a specific
function, but can simply use transpose(). Great idea indeed!


T

-- 
VI = Visual Irritation


More information about the Digitalmars-d mailing list