-
-
Notifications
You must be signed in to change notification settings - Fork 706
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Removed string predicates from std.algorithm.iteration documentation #3800
Conversation
|
I don't agree. Strings are perfectly acceptable here, and offer another benefit (besides brevity) that some template with a string parameter is a singleton, while templates based on lambdas are unique. For instance, the following code creates 2 instances of template filter: auto foo(R)(R r) if(isInputRange!R)
{
return filter!(a => a==5)(r);
}
auto bar(R)(R r, int x) if(isInputRange!R)
{
return sum(r.filter!(a => a==5), x)
}while using string lambdas only produces one. Saves on code bloat, and other things. Until we can factor those into one instance, I think string lambdas are here to stay. What can be done, however, is to mix strings and lambdas in the docs so it's obvious both are possible. However, I'd say in the cheat sheet, go ahead and change everything to lambdas, it should be consistent (right now, it looks like some functions only take strings, while others take lambdas). |
|
IMO exe size is a small price to pay for syntax highlighting, better errors from the compiler, the ability to forget about special cases like the fact that "a++" modifies in place, and the ability to name parameters whatever you want. Also, my point still stands that changing these makes the docs clearer for new comers. From these examples you can infer you can pass function literals or function pointers logically from the fact that you can pass lambdas. Where as the string examples, you can be forgiven for believing that you can only use strings. I mean, we can't expect every newcomer to read Ali's or Andrei's book for the full picture; a lot of people are just going to be reading the Phobos docs. |
It's a small price, but it's a price. I'm just saying that we shouldn't remove string lambdas because they offer benefits (for now), and showing that both strings and lambdas work in the examples identifies the capabilities phobos supports. I don't want to remove all string lambdas from the docs or from phobos. |
The problem is that it doesn't make much sense to have two lines for every example with a predicate defined, one with a string and one with a lambda. How about this: because of the problem above, we keep lambdas in the docs, as I think that they are the best option most of the time. But, we modify the docs of these functions so fact that the predicate is passed to |
|
@schveiguy ping. |
|
Can you modify one doc and show me what you mean? |
346e56f to
4f6226c
Compare
|
@schveiguy I modified the docs for |
| @@ -2864,6 +2864,9 @@ Two adjacent separators are considered to surround an empty element in | |||
| the split range. Use $(D filter!(a => !a.empty)) on the result to compress | |||
| empty elements. | |||
|
|
|||
| The predicate is passed to $(XREF functional,binaryFun), and can accept a string, | |||
| function pointer, lambda, or function literal. | |||
|
|
|||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are many things that work here, including delegates, functors, functions, templates, etc.
I think it's probably best to identify that strings here are a special case. Everything else is an alias to a "callable" entity. Perhaps: "The predicate can be either a string lambda (see
a591749 to
8a62e77
Compare
|
@schveiguy fixed. |
|
Looks better. On the second splitter overload, it looks like the predicate must accept an element of r and an element of s, so the blurb about what predicates are accepted may need to be reworded, or maybe we can come up with a more "portable" way of saying it. |
|
@schveiguy Ok. But on the whole is this a good compromise? Can I start applying this to the other functions? |
|
It's fine with me, I cannot speak for others who haven't looked at it yet. |
|
Updated the rest of the docs. |
|
ping |
|
This has been brought up before. Some people support this, some don't. Maybe @andralex should make a judgment call here and settle this issue once and for all, instead of letting this PR bit-rot? |
|
Yah, we need to fix function equality in the future but string lambdas should be slowly and surely phased out. @JackStouffer thx for the work, when modifying comments in the future please take the opportunity to replace |
|
Auto-merge toggled on |
|
Function equality is, in the general case, undecidable. However, for our purposes, equality for most of the function literals we'll need to compare in normal use cases can probably be sufficiently approximated by some kind of AST structure matching. It won't catch all the cases, but it will certainly catch the cases that string lambdas can catch, and many more besides. (E.g., currently |
Removed string predicates from std.algorithm.iteration documentation
|
Thanks! |
|
@quickfur yah, alpha renaming too. Someone should write a DIP for this... |
The two most common things that I hear when introducing D to people are
writelnactually a property ofstring!?" (misunderstanding of UFCS)Nothing much can be done for the first one, idiosyncrasies are part of learning a new language. But the second one is mostly because the docs use string predicates more than lambdas. String predicates are bad practice compared to lambdas IMO and should be discouraged by not including them in docs. Yes, there are legitimate uses of string predicates, but 99% of the time, you should use lambdas.