D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 5530 - std.algorithm.len()
Summary: std.algorithm.len()
Status: RESOLVED WONTFIX
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: All All
: P2 enhancement
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-02-05 17:22 UTC by bearophile_hugs
Modified: 2014-01-25 15:45 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description bearophile_hugs 2011-02-05 17:22:30 UTC
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.
Comment 1 Peter Alexander 2014-01-25 14:00:32 UTC
What's wrong with `walkLength`? If the range has `.length` (and isn't a narrow string) then walkLength just returns .length
Comment 2 bearophile_hugs 2014-01-25 15:10:22 UTC
(In reply to comment #1)
> What's wrong with `walkLength`?

Its semantics and performance are different for narrow strings, that are common use case. len is O(1), walkLength is sometimes O(n).
Comment 3 Peter Alexander 2014-01-25 15:31:05 UTC
(In reply to comment #2)
> (In reply to comment #1)
> > What's wrong with `walkLength`?
> 
> Its semantics and performance are different for narrow strings, that are common
> use case. len is O(1), walkLength is sometimes O(n).

So am I right in saying that your proposed 'len' function would return .length for narrow strings?

If that's the case then it would be a glaring inconsistency with the rest of Phobos, which has been very careful to treat strings as ranges of code points, and not code units. Having a single function break consistency for the sake of a few characters of typing convenience doesn't seem like a sensible thing to do.
Comment 4 Andrei Alexandrescu 2014-01-25 15:44:52 UTC
I understad the motivatio, but it's not nearly enough to justify a synonim for length.
Comment 5 Andrei Alexandrescu 2014-01-25 15:45:31 UTC
Sorry for the spelling errors - keyboard is acting up.