Python's features, which requires D

Dennis Ritchie via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat May 23 13:25:17 PDT 2015


On Saturday, 23 May 2015 at 19:22:40 UTC, Alex Parrill wrote:
> import std.algorithm;
> import std.range;
> import std.stdio;
> import std.conv;
>
> void main() {
> 	enum n1 = 5;
> 	writeln(stdin.byLine
> 		.map!(line => line.split(" ").map!(x => to!int(x)))
> 	);
> 	
> 	writeln("------");
> 	
> 	enum n2 = 6;
> 	writeln(iota(n2)
> 		.map!(i => chain(
> 			repeat("2", i),
> 			only("1"),
> 			repeat("0", n2 - i - 1),
> 			only("\n")
> 		).joiner(" ")).joiner
> 	);
> }
>
>
> (I omitted evens/odds because it's been addressed and fizzbuzz 
> because there's probably dozens of them floating around)
>
> You seem to be focusing on D's arrays only, but the real meat 
> is in ranges, which are more generic. Also note that the above 
> solution doesn't allocate any of the ranges in the heap; 
> they're all on the stack (as opposed to Python, where you have 
> to allocate lists or use iterators+itertools).

This does not work!

enum n1 = 5;
writeln(stdin.byLine
  	.map!(line => line.split(" ").map!(x => to!int(x)))
);
-----
http://rextester.com/VGHZF81178

Even if you wanted to write this:

enum n = 5;
writeln(stdin.byLine
	.map!(line => line.split(" ").map!(x => to!int(x))).take(n)
);
-----
http://rextester.com/COWE75794

That it's still not working. In my opinion, and should not work :)

> You seem to be focusing on D's arrays only, but the real meat 
> is in ranges, which are more generic.

Not sure what kind of meat you mean, but I really don't see much 
meat in ranges. Of course, this is 10 times better and easier to 
use than STL iterators C++. For me the most important feature D 
are mixins, which I, unfortunately, rarely use. I'm waiting for 
new features from D: for new designs, not simply the expansion of 
Phobos and fix bugs in DMD :) Should I wait for these new 
features? It seems to me that everyone is not enough to simply 
correct C++ — they all want a language in which many different 
sugar. In my opinion, sugar you can try to shake out of Lisp, if 
possible :)

> Also note that the above solution doesn't allocate any of the 
> ranges in the heap; they're all on the stack (as opposed to 
> Python, where you have to allocate lists or use 
> iterators+itertools).

And yet I do not like how the function byLine. It seems to me 
that we need analogues that will not work so damp as byLine.

All right. Next time I will try to combine features that are not 
available in D, and of the faster languages: Erlang, Perl, Lisp, 
Nim, Rust, Scala etc. :)


More information about the Digitalmars-d-learn mailing list