4 ms·
the D language supports that kind of syntax nums.filter!(i => i%2 == 0).map!(i => i/2);
by darkstalker 14y ago
the D language supports that kind of syntax
nums.filter!(i => i%2 == 0).map!(i => i/2);
- fferen 14y agoTo elaborate, this is because of D's Uniform Function Call Syntax (http://www.drdobbs.com/blogs/cpp/232700394 http://www.drdobbs.com/blogs/cpp/232700394) - any function that takes an object as first argument can be called as though it were a method of that object; "map" and "filter" are actually functions in the std.algorithm module. It's a pretty neat trick and although in principle it could make things harder to reason about I haven't had any problems with it so far.
- lihaoyi 14y agoso does Scala stuff.filter(_ % 2 == 0).map(_ / 2) and C#, demonstrating that this isn't some obscure feature that only language geeks care about: Stuff.Where(x => x % 2 == 0).Select(x => x / 2) My point isn't that this sort of syntax is new and novel, it's just that in Python it's annoyingly inconsistent. There are reasons where you would want to use type-class style modules to structure your code in a certain way, but I do not think python's map() filter() reduce() and len() qualify as these cases