digitalmars.D.bugs - [Issue 5530] New: std.algorithm.len()
- d-bugmail puremagic.com (43/43) Feb 05 2011 http://d.puremagic.com/issues/show_bug.cgi?id=5530
http://d.puremagic.com/issues/show_bug.cgi?id=5530 Summary: std.algorithm.len() Product: D Version: D2 Platform: All OS/Version: All Status: NEW Severity: enhancement Priority: P2 Component: Phobos AssignedTo: nobody puremagic.com ReportedBy: bearophile_hugs eml.cc A simple task asks to sort an array according to the length of its items. This is a D2 solution: import std.stdio, std.algorithm; void main() { auto l = [['a','b','c'],['d','e'],['f','g','h'],['i','j','k','l'],['m','n'],['o']]; schwartzSort!((s){return s.length; })(l); writeln(l); } It's supposed to print: [['o'], ['d', 'e'], ['m', 'n'], ['a', 'b', 'c'], ['f', 'g', 'h'], ['i', 'j', 'k', 'l']] I suggest to add a simple len() function to std.algorithm, that allows to shorten that very common code (mapping lengths is a very common operation): import std.stdio, std.algorithm; size_t len(Range)(Range r) if (is(typeof(r.length))) { return r.length; } void main() { auto l = [['a','b','c'],['d','e'],['f','g','h'],['i','j','k','l'],['m','n'],['o']]; schwartzSort!len(l); writeln(l); } In Python the "len()" function is a free function to allow it to be used as mapping function, sorting function for a Schwartz sort, etc. In Ruby there is a "size" standard attribute, and blocks are more used. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Feb 05 2011