Thread overview | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
July 17, 2004 Digital Mars Ate My Computer! | ||||
---|---|---|---|---|
| ||||
You know you're pushing the language to the limits when you cause the compiler to eat your machine. I just had an unhappy 10 minutes waiting for my virtual memory to be completely eaten up by dmd.exe, and another 5 while I rebooted the oh-so-stable OS that is WinXP. Pleuch! Anyway, I think I've found a serious flaw in D's templates architecture. Here's the issue: In DTL, there are several different ways of treating containers, supporting different paradigms. The first, and most important one of these, is based on ranges, usually in combination with foreach. "In D-world, foreach is King!" (That's the title of a section in the book ... <g>). Containers provide a number of standard methods (some template, some non-template) that allow one to provide a filtered (sub-)set of their contents. The one I've been banging on about in other threads is select(), so we'll stick with that. Container { <range> select(<some function / predicate / function-object); This allows us to write something such as the following extract from my test suites: printf("\nselect()-ing the numbers, with IsOdd's fn, as a delegate\n"); foreach(int i; cont.select(&IsOdd.fn)) { printf("%d ", i); } printf("\n"); printf("\nselect()-ing the numbers, with IsEven\n"); foreach(int i; cont.select0!(IsEven)()) { printf("%d ", i); } printf("\n"); printf("\nselect()-ing the numbers, with DivisibleBy\n"); foreach(int i; cont.select1!(DivisibleBy)(new DivisibleBy(3))) { printf("%d ", i); } printf("\n"); These give: select()-ing the numbers, with IsOdd's fn, as a delegate -9 -7 -5 -3 -1 1 3 5 7 9 select()-ing the numbers, with IsEven -10 -8 -6 -4 -2 0 2 4 6 8 select()-ing the numbers, with DivisibleBy -9 -6 -3 0 3 6 9 Ok, that's nice, but that's not the whole picture. Given the set of methods that filter output of a given container exist (or, rather, they will when I've got it all working) in a mixin, the obvious - and intended!! - step is to mix-in the mixin to the range types themselves. This is to give the far more powerful composition of filters, e.g. foreach( . . . , container.select(&IsEven).collect(&Multiply).reject0!(DivisibleBy)(new DivisibleBy(3))) { } or (the more accessible): container.select(&IsEven).max(); I trust you'll agree this a very powerful model, (almost) matching the declarative notation of some of the power powerful (albeit barely compilable) stuff in bleeding edge C++. Unfortunately, there's a fly in my ointment. Because D appears to evaluate the types generated ( / generatable), it gets in an infinite loop here. This does not happen in C++. I don't know what the answer is, other than this aspect of the language, or the current compiler's interpretation of it, should be changed. I'll try and boil this down to a simple example and post it soon. |
July 17, 2004 Re: Digital Mars Ate My Computer! | ||||
---|---|---|---|---|
| ||||
Posted in reply to Matthew Wilson | Matthew Wilson wrote:
> container.select(&IsEven).collect(&Multiply).reject0!(DivisibleBy)(new
> DivisibleBy(3)))
> {
> }
>
> or (the more accessible):
>
> container.select(&IsEven).max();
>
>
> I trust you'll agree this a very powerful model, (almost) matching the
> declarative notation of some of the power powerful (albeit barely
> compilable) stuff in bleeding edge C++.
It's not only powerful, it's easy and beautiful which is a very rare combination nowadays. Man, your DTL have earned a new fan today :) Only a question, what does collect do?
|
July 17, 2004 Re: Digital Mars Ate My Computer! | ||||
---|---|---|---|---|
| ||||
Posted in reply to Juanjo Álvarez | It does indeed look very cool, but why select, select0, select1, can they not fit under the same umbrella ? Charlie In article <cdbb5b$1b41$1@digitaldaemon.com>, Juanjo =?ISO-8859-15?Q?=C1lvarez?= says... > >Matthew Wilson wrote: > >> container.select(&IsEven).collect(&Multiply).reject0!(DivisibleBy)(new >> DivisibleBy(3))) >> { >> } >> >> or (the more accessible): >> >> container.select(&IsEven).max(); >> >> >> I trust you'll agree this a very powerful model, (almost) matching the >> declarative notation of some of the power powerful (albeit barely >> compilable) stuff in bleeding edge C++. > >It's not only powerful, it's easy and beautiful which is a very rare combination nowadays. Man, your DTL have earned a new fan today :) Only a question, what does collect do? |
July 17, 2004 Re: Digital Mars Ate My Computer! | ||||
---|---|---|---|---|
| ||||
Posted in reply to Juanjo Álvarez | "Juanjo Álvarez" <juanjuxNO@SPAMyahoo.es> wrote in message news:cdbb5b$1b41$1@digitaldaemon.com... > Matthew Wilson wrote: > > > container.select(&IsEven).collect(&Multiply).reject0!(DivisibleBy)(new > > DivisibleBy(3))) > > { > > } > > > > or (the more accessible): > > > > container.select(&IsEven).max(); > > > > > > I trust you'll agree this a very powerful model, (almost) matching the > > declarative notation of some of the power powerful (albeit barely > > compilable) stuff in bleeding edge C++. > > It's not only powerful, it's easy and beautiful which is a very rare combination nowadays. Man, your DTL have earned a new fan today :) Only a question, what does collect do? select() selects out those matching a given predicate, e.g. only selects those that are even collect() transforms all elements using a given transformation function, e.g. multiplies them by 3 reject() is the opposite of select(). It works by applying the Not!() template predicate and returns a Not'ed range otherwise identical to select(). The names for all these operations are borrowed from Ruby, and are open to change once it's released. It's the mechanisms that I'm interested in - and losing my hair over - at the moment. :) |
July 17, 2004 Re: Digital Mars Ate My Computer! | ||||
---|---|---|---|---|
| ||||
Posted in reply to Charlie | "Charlie" <Charlie_member@pathlink.com> wrote in message news:cdbpbp$1g1r$1@digitaldaemon.com... > It does indeed look very cool, but why select, select0, select1, can they not fit under the same umbrella ? Alas no. Walter has ruled out overloading of template and non-template member functions. There may be another way, of course. We're wandering out onto a new planet here, and people may be able to find paths I can't see. I very much want to get over the few language/compiiler hurdles that are holding me up, and then release 0.1, so all you smart(er) people can point out easier paths to me. :) > > Charlie > > In article <cdbb5b$1b41$1@digitaldaemon.com>, Juanjo =?ISO-8859-15?Q?=C1lvarez?= > says... > > > >Matthew Wilson wrote: > > > >> container.select(&IsEven).collect(&Multiply).reject0!(DivisibleBy)(new > >> DivisibleBy(3))) > >> { > >> } > >> > >> or (the more accessible): > >> > >> container.select(&IsEven).max(); > >> > >> > >> I trust you'll agree this a very powerful model, (almost) matching the > >> declarative notation of some of the power powerful (albeit barely > >> compilable) stuff in bleeding edge C++. > > > >It's not only powerful, it's easy and beautiful which is a very rare combination nowadays. Man, your DTL have earned a new fan today :) Only a question, what does collect do? > > |
July 18, 2004 Re: Digital Mars Ate My Computer! | ||||
---|---|---|---|---|
| ||||
Posted in reply to Matthew | Matthew wrote:
>
> "Juanjo Álvarez" <juanjuxNO@SPAMyahoo.es> wrote in message news:cdbb5b$1b41$1@digitaldaemon.com...
>> Matthew Wilson wrote:
>>
>> > container.select(&IsEven).collect(&Multiply).reject0!(DivisibleBy)(new
>> > DivisibleBy(3)))
>> > {
>> > }
>> >
>> > or (the more accessible):
>> >
>> > container.select(&IsEven).max();
>> >
>> >
>> > I trust you'll agree this a very powerful model, (almost) matching the
>> > declarative notation of some of the power powerful (albeit barely
>> > compilable) stuff in bleeding edge C++.
>>
>> It's not only powerful, it's easy and beautiful which is a very rare combination nowadays. Man, your DTL have earned a new fan today :) Only a question, what does collect do?
>
> select() selects out those matching a given predicate, e.g. only selects
> those that are even
> collect() transforms all elements using a given transformation function,
> e.g. multiplies them by 3
> reject() is the opposite of select(). It works by applying the Not!()
> template predicate and returns a Not'ed range otherwise identical to
> select().
>
> The names for all these operations are borrowed from Ruby, and are open to change once it's released. It's the mechanisms that I'm interested in - and losing my hair over - at the moment. :)
Maybe "transform" would be a better name than collect? When I first saw the
code I thought "collect(&Multiply)" would be multipliying all the values
and returning the outcome (collecting all the values on a single one).
Maybe it's because english is (obviously :) not my native language and in
my language (spanish) the word closer to "collect" (recolectar) means "to
join" or "to take together".
|
July 18, 2004 Re: Digital Mars Ate My Computer! | ||||
---|---|---|---|---|
| ||||
Posted in reply to Matthew | Matthew wrote:
>
> "Charlie" <Charlie_member@pathlink.com> wrote in message news:cdbpbp$1g1r$1@digitaldaemon.com...
>> It does indeed look very cool, but why select, select0, select1, can they not fit under the same umbrella ?
>
> Alas no. Walter has ruled out overloading of template and non-template member functions.
Yes, but select_dlg (from delegate), select_mix, etc, are more descriptive
names.
|
July 18, 2004 Re: Digital Mars Ate My Computer! | ||||
---|---|---|---|---|
| ||||
Posted in reply to Juanjo Álvarez | "Juanjo Álvarez" <juanjuxNO@SPAMyahoo.es> wrote in message news:cdcfpg$1pei$2@digitaldaemon.com... > Matthew wrote: > > > > > "Juanjo Álvarez" <juanjuxNO@SPAMyahoo.es> wrote in message news:cdbb5b$1b41$1@digitaldaemon.com... > >> Matthew Wilson wrote: > >> > >> > container.select(&IsEven).collect(&Multiply).reject0!(DivisibleBy)(new > >> > DivisibleBy(3))) > >> > { > >> > } > >> > > >> > or (the more accessible): > >> > > >> > container.select(&IsEven).max(); > >> > > >> > > >> > I trust you'll agree this a very powerful model, (almost) matching the > >> > declarative notation of some of the power powerful (albeit barely > >> > compilable) stuff in bleeding edge C++. > >> > >> It's not only powerful, it's easy and beautiful which is a very rare combination nowadays. Man, your DTL have earned a new fan today :) Only a question, what does collect do? > > > > select() selects out those matching a given predicate, e.g. only selects > > those that are even > > collect() transforms all elements using a given transformation function, > > e.g. multiplies them by 3 > > reject() is the opposite of select(). It works by applying the Not!() > > template predicate and returns a Not'ed range otherwise identical to > > select(). > > > > The names for all these operations are borrowed from Ruby, and are open to change once it's released. It's the mechanisms that I'm interested in - and losing my hair over - at the moment. :) > > Maybe "transform" would be a better name than collect? When I first saw the > code I thought "collect(&Multiply)" would be multipliying all the values > and returning the outcome (collecting all the values on a single one). > Maybe it's because english is (obviously :) not my native language and in > my language (spanish) the word closer to "collect" (recolectar) means "to > join" or "to take together". Sure. I've just borrowed the names from their Ruby counterparts - Ruby's my current fave language <g> - and they most certainly will be up for review/standardisation. We're just not at that stage of the game, and I wanted something established so as not to focus on that issue at the moment. Rest assured, I'm a *big* fan of unambiguous naming conventions. :-) |
July 18, 2004 Re: Digital Mars Ate My Computer! | ||||
---|---|---|---|---|
| ||||
Posted in reply to Juanjo Álvarez | "Juanjo Álvarez" <juanjuxNO@SPAMyahoo.es> wrote in message news:cdcfrh$1pei$3@digitaldaemon.com... > Matthew wrote: > > > > > "Charlie" <Charlie_member@pathlink.com> wrote in message news:cdbpbp$1g1r$1@digitaldaemon.com... > >> It does indeed look very cool, but why select, select0, select1, can they not fit under the same umbrella ? > > > > Alas no. Walter has ruled out overloading of template and non-template member functions. > > Yes, but select_dlg (from delegate), select_mix, etc, are more descriptive > names. Again, please don't worry about the names. All will be sorted in due course. :-) |
July 18, 2004 Re: Digital Mars Ate My Computer! | ||||
---|---|---|---|---|
| ||||
Posted in reply to Matthew | On Sun, 18 Jul 2004 06:37:30 +1000, Matthew wrote: <snip> > I very much want to get over the few language/compiiler hurdles that are > holding me up, and then release 0.1, so all you smart(er) people can > point out easier paths to me. :) > LOL! Smart answer! :-D |
Copyright © 1999-2021 by the D Language Foundation