-
-
Notifications
You must be signed in to change notification settings - Fork 744
Issue 9699 - strip functions should allow setting custom match character. #1201
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
Conversation
These should probably use predicates instead of hard-coded values. @andralex, any word on such inlining? |
I've added predicate overloads. |
LGTM |
And what does this buy us over simply using |
You'll have to demonstrate how |
Code like: auto stripped = "\n test\t".strip!isWhite(); Is not at all pretty with |
Having the |
Those return a lazy range, it's completely different. And calling array on that is expensive. |
Nope. They return |
These may be worth adding from the standpoint of user friendliness, but they don't add any functionality, which is why I never implemented them when someone brought them up in the past. |
I think the predicate should also accept strings in addition to delegates. The functions in std.algorithm / std.range use some fancy wrapper that turns mixins to delegates or something like that.
How do you define what "adds functionality"? |
... somewhat? It obviously doesn't add anything that can't be done with |
Is there a good reason to do this? We're largely working towards phasing string lambdas out and using proper lambda expressions instead. I don't think there's a good reason to proliferate string lambdas any longer. |
No idea. You're probably right. I thought it was the convention to use them. |
However, I'm not sure if supporting the string syntax is a good idea. It was born when the lambda syntax The one benefit of the string syntax is that it is slightly shorter - but it's only shorter because |
That auto inference tricked me. Anyway how can you be advocating this: string str = "xx abc xx";
string both = str.retro().find!(a => a != 'x')().retro().find!(a => a != 'x')(); over this: string str = "xx abc xx";
string both = str.strip!(a => a == x); |
My point is that you can do what you're trying to do here just fine with what we already have. New functions aren't necessary. They may ultimately be desirable on the grounds that the equivalent of So, if we decide that it's worth adding on the grounds of user-friendliness, fine. But it's not adding anything that we can't already do in std.algorithm. And Andrei at least has typically shot down stuff on those grounds (though I don't think that I feel anywhere near as strongly about that as he does, and I have no idea what he'd think in this case). There's also the question of whether this should be made more general (e.g. put in std.array to work on all arrays, since these have nothing to do with whitespace), but as soon as you start going down that route, pretty soon, the question becomes whether they should work on all ranges, in which case, As for the predicates, I think that all predicates should support the string syntax. It's often cleaner than lambda literals, and there's no reason not to support it as far as I can see, unless you're trying to get rid of string lambdas, which I would be very much opposed to. Not supporting them is inconsistent, and it's trivial to add support. Both types of lambdas should work, and then the user can use whichever is best for their code. |
That point is completely lost to anyone who used strip functions and wanted to pass a character. So now you're saying the user has to write half a dozen expressions and reverse the predicate. If you really want to advocate writing excessively long and unreadable code then go ahead and remove half the stuff in Every time we try to improve the API this pointless discussion comes up, as if someone said at some point "Phobos development is now frozen, user-convenience is a non-issue". It's absurd that adding breaking language changes and language enhancements is now easier to do than adding convenient functions like these to Phobos.
See Walter's last two comments here. There's no need to bring it back from the dead. |
Walter and Andrei both seem to be big on not making Phobos shallow, which tends to mean not adding functions which do what other functions already do. To some extent, I agree, and to some extent I disagree - usability does trump the goal of making the library deep at least some of the time. But any function which just adds convenience needs to be discussed, or we'll just end up with a pile of convenience functions which do what other functions already do but slightly differently. I brought up the fact that
There's a big difference between not using it in examples and killing it off, and not supporting it here just makes it inconsistent with the rest of Phobos. I'd strongly argue that we either get rid of string lambdas completely (which I'm opposed to) or that all predicates in Phobos support them. |
Here are my thoughts on the subject:
|
I think the string predicates are just plain harmful.
The new lambda syntax has none of the above problems. Let's not proliferate string predicates unnecessarily. |
Oh, regarding lambdas - I think we should slowly phase out string lambdas. |
Right, but people expect code which uses |
std.string should import from std.algorithm appropriately. |
I think there should be only one implementation, possibly with some satellite alias definition or (worst case) forwarding functions. |
@andralex Honestly, I wouldn't think that stripping a specific element from either side of a range is likely to be used much outside of strings. For other ranges, I would expect it to be frequent to strip elements which match a predicate, but I'd also think that strip is a weird name for that. A more appropriate name for that would be It won't be the end of the world if we end up with generic |
@jmdavis If it's not much extra work and is not conducive to unexpected errors and bugs it's probably worth just letting |
@andralex: That's a cute thing, striping silence in audio. So we want the equivalent of this pull request except the strip function would take a range and its element type as arguments? |
@AndrejMitrovic yah, and they should go in std.algorithm. The strip function with no element and no predicate is in std.string and strips whitespace. |
But I guess they should be called stripFront and stripBack if they work with ranges? And perhaps introduce stripLeft/stripRight as aliases of them into std.array? |
@andralex @AndrejMitrovic I'd probably just keep them as |
@AndrejMitrovic do you plan to finish this? Please advise, thanks. |
@andralex: Yes, today. |
http://d.puremagic.com/issues/show_bug.cgi?id=9699