You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@groovy.apache.org by "Daniel.Sun" <su...@apache.org> on 2017/12/31 12:24:44 UTC

About adding DGM startsWith(String...) and endsWith(String...)

Hi all,

      I am going to add startsWith(String...) and endsWith(String...) for
java.lang.String(i.e. if and only if the string starts with/ends with any
specified strings, return true), because many senarios require them. For
example:

The following code
   
https://github.com/apache/groovy/blob/master/gradle/assemble.gradle#L352-L355
can be simplified as
    it.file.name.startsWith('asm-', 'antlr-', 'antlr4-')

    The similar senario appears at:
https://github.com/apache/groovy/blob/master/gradle/assemble.gradle#L366-L369

    Any thoughts?

Cheers,
Daniel.Sun



--
Sent from: http://groovy.329449.n5.nabble.com/Groovy-Dev-f372993.html

Re: About adding DGM startsWith(String...) and endsWith(String...)

Posted by Guillaume Laforge <gl...@gmail.com>.
Sounds useful!
I would have used it a few times already.

On Sun, Dec 31, 2017 at 3:35 PM, mg <mg...@arscreat.com> wrote:

> All for adding that: Very helpful, does not break any existing code,
> should be faster than any regex based solution (and be honest: Who wouldn't
> wrap the regex solution in a helper method if used more than 2x in the
> code, using a substring based solution along the way... ?-) ).
>
> -------- Ursprüngliche Nachricht --------
> Von: "Daniel.Sun" <su...@apache.org>
> Datum: 31.12.17 13:59 (GMT+01:00)
> An: dev@groovy.incubator.apache.org
> Betreff: Re: About adding DGM startsWith(String...) and
> endsWith(String...)
>
> Yeah, you are right, but `it.file.name =~ /^(?:asm|antlr|antlr4)-/` is not
> very intuitive IMHO.
>
> Cheers,
> Daniel.Sun
>
>
>
> --
> Sent from: http://groovy.329449.n5.nabble.com/Groovy-Dev-f372993.html
>



-- 
Guillaume Laforge
Apache Groovy committer & PMC Vice-President
Developer Advocate @ Google Cloud Platform

Blog: http://glaforge.appspot.com/
Social: @glaforge <http://twitter.com/glaforge> / Google+
<https://plus.google.com/u/0/114130972232398734985/posts>

Re: About adding DGM startsWith(String...) and endsWith(String...)

Posted by mg <mg...@arscreat.com>.
All for adding that: Very helpful, does not break any existing code, should be faster than any regex based solution (and be honest: Who wouldn't wrap the regex solution in a helper method if used more than 2x in the code, using a substring based solution along the way... ?-) ).
-------- Ursprüngliche Nachricht --------Von: "Daniel.Sun" <su...@apache.org> Datum: 31.12.17  13:59  (GMT+01:00) An: dev@groovy.incubator.apache.org Betreff: Re: About adding DGM startsWith(String...) and endsWith(String...) 
Yeah, you are right, but `it.file.name =~ /^(?:asm|antlr|antlr4)-/` is not
very intuitive IMHO.

Cheers,
Daniel.Sun



--
Sent from: http://groovy.329449.n5.nabble.com/Groovy-Dev-f372993.html

Re: About adding DGM startsWith(String...) and endsWith(String...)

Posted by "Daniel.Sun" <su...@apache.org>.
Yeah, you are right, but `it.file.name =~ /^(?:asm|antlr|antlr4)-/` is not
very intuitive IMHO.

Cheers,
Daniel.Sun



--
Sent from: http://groovy.329449.n5.nabble.com/Groovy-Dev-f372993.html

Re: About adding DGM startsWith(String...) and endsWith(String...)

Posted by Marcin Erdmann <ma...@proxerd.pl>.
My only thought is that it can also be achieved using =~ and a regular
expression,  it.file.name =~ /^(?:asm|antlr|antlr4)-/

On Sun, Dec 31, 2017 at 12:24 PM, Daniel.Sun <su...@apache.org> wrote:

> Hi all,
>
>       I am going to add startsWith(String...) and endsWith(String...) for
> java.lang.String(i.e. if and only if the string starts with/ends with any
> specified strings, return true), because many senarios require them. For
> example:
>
> The following code
>
> https://github.com/apache/groovy/blob/master/gradle/
> assemble.gradle#L352-L355
> can be simplified as
>     it.file.name.startsWith('asm-', 'antlr-', 'antlr4-')
>
>     The similar senario appears at:
> https://github.com/apache/groovy/blob/master/gradle/
> assemble.gradle#L366-L369
>
>     Any thoughts?
>
> Cheers,
> Daniel.Sun
>
>
>
> --
> Sent from: http://groovy.329449.n5.nabble.com/Groovy-Dev-f372993.html
>

Re: About adding DGM startsWith(String...) and endsWith(String...)

Posted by Daniel Sun <re...@hotmail.com>.
Hi Jochen,

      I've renamed the two DGM according to you suggestion[1] :-)

Cheers,
Daniel.Sun

[1]
https://github.com/apache/groovy/commit/3c91bc1fd5651a15a0435a31fac2d6b98246016c




--
Sent from: http://groovy.329449.n5.nabble.com/Groovy-Dev-f372993.html

Re: About adding DGM startsWith(String...) and endsWith(String...)

Posted by MG <mg...@arscreat.com>.
+1 on startsWithAny or startsWithAnyOf (was thinking along the same line 
after my reply). Better expresses the use and keeps startsWith free to 
be extended with other arguments in the future.

@Offset parameter: I tend to be for offering more functionality - though 
I must admit I do not see the applications here, and "startsWith with an 
offset" (even if Java already uses it) is a bit weird, since it is more 
of a "containsAt".
Detail question: Would the offset parameter be the first (imho better to 
read) or the last (as in Java) parameter ?


On 31.12.2017 16:16, Jochen Theodorou wrote:
> On 31.12.2017 13:24, Daniel.Sun wrote:
>> Hi all,
>>
>>        I am going to add startsWith(String...) and 
>> endsWith(String...) for
>> java.lang.String(i.e. if and only if the string starts with/ends with 
>> any
>> specified strings, return true), because many senarios require them. For
>> example:
>>
>> The following code
>> https://github.com/apache/groovy/blob/master/gradle/assemble.gradle#L352-L355
>> can be simplified as
>>      it.file.name.startsWith('asm-', 'antlr-', 'antlr4-')
>>
>>      The similar senario appears at:
>> https://github.com/apache/groovy/blob/master/gradle/assemble.gradle#L366-L369 
>>
>>
>>      Any thoughts?
>
> I think the name should express the iterative aspect something like 
> startsWithAny of findStartsWith. And then there is also the variant 
> with an offset in java-lang.String. Would we have that as well?
>
> But frankly my feeling is that it would be better to find a generic 
> solution for this kind of construct, instead of having more methods in 
> DGM. But that not stop us in the past either and I do not have an idea 
> to present here as well.
>
> bye Jochen
>


Re: About adding DGM startsWith(String...) and endsWith(String...)

Posted by Jochen Theodorou <bl...@gmx.org>.
On 31.12.2017 13:24, Daniel.Sun wrote:
> Hi all,
> 
>        I am going to add startsWith(String...) and endsWith(String...) for
> java.lang.String(i.e. if and only if the string starts with/ends with any
> specified strings, return true), because many senarios require them. For
> example:
> 
> The following code
>     
> https://github.com/apache/groovy/blob/master/gradle/assemble.gradle#L352-L355
> can be simplified as
>      it.file.name.startsWith('asm-', 'antlr-', 'antlr4-')
> 
>      The similar senario appears at:
> https://github.com/apache/groovy/blob/master/gradle/assemble.gradle#L366-L369
> 
>      Any thoughts?

I think the name should express the iterative aspect something like 
startsWithAny of findStartsWith. And then there is also the variant with 
an offset in java-lang.String. Would we have that as well?

But frankly my feeling is that it would be better to find a generic 
solution for this kind of construct, instead of having more methods in 
DGM. But that not stop us in the past either and I do not have an idea 
to present here as well.

bye Jochen

RE: About adding DGM startsWith(String...) and endsWith(String...)

Posted by er...@thomsonreuters.com.
Isn't the trend to use CharSequence instead of String?  If it's startsWith, shouldn't the signature be startsWith(String one, String two, String... more) to avoid zero params and conflict with 1 param built-in method?

Couldn't this be just as easily expressed using the any DGM?

['prefix1', 'prefix2', ...].any { string.startsWith(it) }


Re: About adding DGM startsWith(String...) and endsWith(String...)

Posted by Daniel Sun <re...@hotmail.com>.
The issue to track the feature has been created:
https://issues.apache.org/jira/browse/GROOVY-8428

Cheers,
Daniel.Sun



--
Sent from: http://groovy.329449.n5.nabble.com/Groovy-Dev-f372993.html

Re: About adding DGM startsWith(String...) and endsWith(String...)

Posted by "Daniel.Sun" <su...@apache.org>.
OK. I'll create one later. Happy New Year :-)

Cheers,
Daniel. Sun




--
Sent from: http://groovy.329449.n5.nabble.com/Groovy-Dev-f372993.html

Re: About adding DGM startsWith(String...) and endsWith(String...)

Posted by Paul King <pa...@asert.com.au>.
+1, please create a Jira ticket - even though it's a small new feature it's
good to mention it in the release notes. Happy New Year from down under.

On Sun, Dec 31, 2017 at 10:24 PM, Daniel.Sun <su...@apache.org> wrote:

> Hi all,
>
>       I am going to add startsWith(String...) and endsWith(String...) for
> java.lang.String(i.e. if and only if the string starts with/ends with any
> specified strings, return true), because many senarios require them. For
> example:
>
> The following code
>
> https://github.com/apache/groovy/blob/master/gradle/
> assemble.gradle#L352-L355
> can be simplified as
>     it.file.name.startsWith('asm-', 'antlr-', 'antlr4-')
>
>     The similar senario appears at:
> https://github.com/apache/groovy/blob/master/gradle/
> assemble.gradle#L366-L369
>
>     Any thoughts?
>
> Cheers,
> Daniel.Sun
>
>
>
> --
> Sent from: http://groovy.329449.n5.nabble.com/Groovy-Dev-f372993.html
>