Skip to content

Added practical example to std.algorithm.iteration.group#5177

Merged
dlang-bot merged 1 commit intodlang:masterfrom
JackStouffer:group-docs
Feb 21, 2017
Merged

Added practical example to std.algorithm.iteration.group#5177
dlang-bot merged 1 commit intodlang:masterfrom
JackStouffer:group-docs

Conversation

@JackStouffer
Copy link
Contributor

Just discovered this useful idiom. I was doing this manually using foreach over ranges and incrementing the count in an AA for my stats plotting code.

All manual foreach loops in user code are bugs!

Copy link
Contributor

@wilzbach wilzbach left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!
Definitely worth to be shown to the user as an example :)

@quickfur
Copy link
Member

Awesome example!


uint[string] result;
auto range = ["a", "b", "a", "c", "b", "c", "c", "d", "e"];
result = range.sort!("a < b")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be a regular lambda.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's also the default, so it could be omitted here as well..

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately it can't be omitted, because then the compiler assumes you're using array.sort rather than std.algorithm.sort().

And for some reason, unlike Phobos, DMD does not stick to actually deprecating things when it says it will. array.sort has been a warning for almost three years now.

@wilzbach
Copy link
Contributor

Unfortunately it can't be omitted, because then the compiler assumes you're using array.sort rather than std.algorithm.sort().

Just use empty parentheses (array.sort()) ;-)

array.sort has been a warning for almost three years now.

Well, I guess nobody has bothered to make a PR so far :/

@dlang-bot dlang-bot merged commit 60b557f into dlang:master Feb 21, 2017
@quickfur
Copy link
Member

Hmm. According to the git log, this warning was turned into a deprecation last year (Apr 29, see dmd commit bc1f04ba. It has been a warning since before the C++ to D switchover in Aug 2015, though, so it has been a while. I think I'll submit a PR for this.

@quickfur
Copy link
Member

The deed is done: dlang/dmd#6554

@JackStouffer JackStouffer deleted the group-docs branch May 22, 2017 17:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants