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

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
13 messages Options
12
Reply | Threaded
Open this post in threaded view
|

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

Daniel.Sun
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
Daniel Sun
Apache Groovy committer

Blog: http://blog.sunlan.me
Twitter: @daniel_sun
Reply | Threaded
Open this post in threaded view
|

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

Marcin Erdmann
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 <[hidden email]> 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

Reply | Threaded
Open this post in threaded view
|

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

Daniel.Sun
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
Daniel Sun
Apache Groovy committer

Blog: http://blog.sunlan.me
Twitter: @daniel_sun
MG
Reply | Threaded
Open this post in threaded view
|

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

MG
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" <[hidden email]>
Datum: 31.12.17 13:59 (GMT+01:00)
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
MG
Reply | Threaded
Open this post in threaded view
|

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

MG
In reply to this post by Daniel.Sun
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" <[hidden email]>
Datum: 31.12.17 13:59 (GMT+01:00)
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
Reply | Threaded
Open this post in threaded view
|

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

Guillaume Laforge
Administrator
In reply to this post by Daniel.Sun
Sounds useful!
I would have used it a few times already.

On Sun, Dec 31, 2017 at 3:35 PM, mg <[hidden email]> 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" <[hidden email]>
Datum: 31.12.17 13:59 (GMT+01:00)
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

Reply | Threaded
Open this post in threaded view
|

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

Jochen Theodorou
In reply to this post by Daniel.Sun
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
MG
Reply | Threaded
Open this post in threaded view
|

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

MG
+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
>

Reply | Threaded
Open this post in threaded view
|

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

Daniel Sun
In reply to this post by Jochen Theodorou
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
Reply | Threaded
Open this post in threaded view
|

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

paulk_asert
In reply to this post by Daniel.Sun
+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 <[hidden email]> 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

12