foreach and filter
Russel Winder
russel at winder.org.uk
Mon Apr 16 04:20:02 PDT 2012
On Wed, 2012-04-11 at 18:32 +0200, Jakob Ovrum wrote:
[...]
> Ranges are iterated in-place. You can't mutate a const range,
> hence you cannot advance it by one (`popFront`), which is
> required by the lowering of foreach using the range interface.
[...]
I think I am still trying to recover from the shock of this; it is very
likely to turn anyone interested in any form of declarative expression
away from D. Also the internal activity is being exposed at the API
level. Not to mention the history of iteration being one of working
with mutable object on immutable structures.
Trying to create the smallest possible example:
auto r = iota ( 0 , 10 ) ;
writeln ( map ! ( i => i * i ) ( r ) ) ;
works fine. Let us try:
const r = iota ( 0 , 10 ) ;
writeln ( map ! ( i => i * i ) ( r ) ) ;
works fine. Great. Now
auto r = iota ( 0 , 10 ) ;
writeln ( reduce ! ( ( a, b ) => a + b ) ( 10 , r ) ) ;
works fine. Let's try:
const r = iota ( 0 , 10 ) ;
writeln ( reduce ! ( ( a, b ) => a + b ) ( 10 , r ) ) ;
Oh dear:
/home/users/russel/lib.Linux.x86_64/DMD2/bin64/../../src/phobos/std/algorithm.d(725): Error: function std.range.iota!(int,int).iota.Result.popFront () is not callable using argument types ()
/home/users/russel/lib.Linux.x86_64/DMD2/bin64/../../src/phobos/std/algorithm.d(725): Error: function std.range.iota!(int,int).iota.Result.front () is not callable using argument types ()
example.d(7): Error: template instance example.main.reduce!(__lambda3).reduce!(int,const(Result)) error instantiating
Not only is the error message incomprehensible to someone who doesn't
know the details of the internal workings (OK not a killer blow), but
the very inconsistency of behaviour between map and reduce is a
violation of the Principle of Least Surprise, and seems like a killer
blow.
--
Russel.
=============================================================================
Dr Russel Winder t: +44 20 7585 2200 voip: sip:russel.winder at ekiga.net
41 Buckmaster Road m: +44 7770 465 077 xmpp: russel at winder.org.uk
London SW11 1EN, UK w: www.russel.org.uk skype: russel_winder
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 198 bytes
Desc: This is a digitally signed message part
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20120416/6a80b4f8/attachment-0001.pgp>
More information about the Digitalmars-d
mailing list