removeleading with limits

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

removeleading with limits

Jolle Carlestam-2
I was in the need of removing leading chars but the plain string -> removeleading was too ambitious.

For example if you apply
’00001’ -> removeleading(’0’) it will leave ’1’. You can’t use the method if you only want to remove say one ’0’.
So I added a signature to fix this:

define string -> removeleading(find::string, count::integer)=> {
        local(looped = 0)

        {
                #looped++
                !.beginsWith(#find)?
                        return
                .remove(1, #find->size)
                #looped < #count ?
                currentCapture->restart
        }()
}


This allows you to do
’00001’ -> removeleading(’0’, 1)
’00001’ -> removeleading(’0’, 2)
’00001’ -> removeleading(’0’, 3)

->
0001
001
01

The plain, standard, string -> removeleading of course continues to work as before.

HDB
Jolle

#############################################################

This message is sent to you because you are subscribed to
  the mailing list Lasso [hidden email]
Official list archives available at http://www.lassotalk.com
To unsubscribe, E-mail to: <[hidden email]>
Send administrative queries to  <[hidden email]>
Reply | Threaded
Open this post in threaded view
|

Re: removeleading with limits

Johan Solve
I needed that option a little while ago. Ended up using a condition
and substring instead.

2015-11-08 10:54 GMT+01:00 Jolle Carlestam <[hidden email]>:

> I was in the need of removing leading chars but the plain string -> removeleading was too ambitious.
>
> For example if you apply
> ’00001’ -> removeleading(’0’) it will leave ’1’. You can’t use the method if you only want to remove say one ’0’.
> So I added a signature to fix this:
>
> define string -> removeleading(find::string, count::integer)=> {
>         local(looped = 0)
>
>         {
>                 #looped++
>                 !.beginsWith(#find)?
>                         return
>                 .remove(1, #find->size)
>                 #looped < #count ?
>                 currentCapture->restart
>         }()
> }
>
>
> This allows you to do
> ’00001’ -> removeleading(’0’, 1)
> ’00001’ -> removeleading(’0’, 2)
> ’00001’ -> removeleading(’0’, 3)
>
> ->
> 0001
> 001
> 01
>
> The plain, standard, string -> removeleading of course continues to work as before.
>
> HDB
> Jolle
>
> #############################################################
>
> This message is sent to you because you are subscribed to
>   the mailing list Lasso [hidden email]
> Official list archives available at http://www.lassotalk.com
> To unsubscribe, E-mail to: <[hidden email]>
> Send administrative queries to  <[hidden email]>



--
Mvh
Johan Sölve
____________________________________
Montania System AB
Halmstad   Stockholm
http://www.montania.se

Johan Sölve
Mobil +46 709-51 55 70
[hidden email]

Kristinebergsvägen 17, S-302 41 Halmstad, Sweden
Telefon +46 35-136800 |  Fax +46 35-136801

#############################################################

This message is sent to you because you are subscribed to
  the mailing list Lasso [hidden email]
Official list archives available at http://www.lassotalk.com
To unsubscribe, E-mail to: <[hidden email]>
Send administrative queries to  <[hidden email]>
Reply | Threaded
Open this post in threaded view
|

Re: removeleading with limits

Jolle Carlestam-2
I was looking at that too. But this was more fun and gave me cleaner code to look at.

HDB
Jolle

> 8 nov 2015 kl. 17:54 skrev Johan Solve <[hidden email]>:
>
> I needed that option a little while ago. Ended up using a condition
> and substring instead.
>
> 2015-11-08 10:54 GMT+01:00 Jolle Carlestam <[hidden email]>:
>> I was in the need of removing leading chars but the plain string -> removeleading was too ambitious.
>>
>> For example if you apply
>> ’00001’ -> removeleading(’0’) it will leave ’1’. You can’t use the method if you only want to remove say one ’0’.
>> So I added a signature to fix this:
>>
>> define string -> removeleading(find::string, count::integer)=> {
>>        local(looped = 0)
>>
>>        {
>>                #looped++
>>                !.beginsWith(#find)?
>>                        return
>>                .remove(1, #find->size)
>>                #looped < #count ?
>>                currentCapture->restart
>>        }()
>> }
>>
>>
>> This allows you to do
>> ’00001’ -> removeleading(’0’, 1)
>> ’00001’ -> removeleading(’0’, 2)
>> ’00001’ -> removeleading(’0’, 3)
>>
>> ->
>> 0001
>> 001
>> 01
>>
>> The plain, standard, string -> removeleading of course continues to work as before.
>>
>> HDB
>> Jolle


#############################################################

This message is sent to you because you are subscribed to
  the mailing list Lasso [hidden email]
Official list archives available at http://www.lassotalk.com
To unsubscribe, E-mail to: <[hidden email]>
Send administrative queries to  <[hidden email]>
Reply | Threaded
Open this post in threaded view
|

Re: removeleading with limits

Bil Corry-3
In reply to this post by Jolle Carlestam-2
I did something similar when L9 first came out, but instead of specifying
how many to remove, I specified how many I wanted.  E.g.

’00001’ -> removeleading(’0’, 1)
’00001’ -> removeleading(’0’, 2)
’00001’ -> removeleading(’0’, 3)

->
01
001
0001


This was the code I used, not sure if it still works in the latest version
of Lasso:

define string->removeleading(find::string,want::integer) => {
  (#want == 0 ? .removeleading(#find))
  if(#want > 0)
    .removeleading(#find)
    .prepend(#find * #want)
  else // #want < 0
    local('c') = 0
    local('max') = #want * -1
    local('loop') = (#max > .size ? .size | #max)
    while(++#c <= #loop && .get(1) == #find)
      .remove(1,1)
    /while
  /if
}

define string->removetrailing(find::string,want::integer) => {
  (#want == 0 ? .removetrailing(#find))
  if(#want > 0)
    .removetrailing(#find)
    .append(#find * #want)
  else // #want < 0
    local('c') = 0
    local('max') = #want * -1
    local('loop') = (#max > .size ? .size | #max)
    while(++#c <= #loop && .get(.size) == #find)
      .remove(.size)
    /while
  /if
}

/*
var('test') = 'aaabbb'
$test->removeleading('a',1)
$test->removetrailing('b',1)
$test
*/





- Bil

On Sun, Nov 8, 2015 at 10:54 AM, Jolle Carlestam <[hidden email]>
wrote:

> I was in the need of removing leading chars but the plain string ->
> removeleading was too ambitious.
>
> For example if you apply
> ’00001’ -> removeleading(’0’) it will leave ’1’. You can’t use the method
> if you only want to remove say one ’0’.
> So I added a signature to fix this:
>
> define string -> removeleading(find::string, count::integer)=> {
>         local(looped = 0)
>
>         {
>                 #looped++
>                 !.beginsWith(#find)?
>                         return
>                 .remove(1, #find->size)
>                 #looped < #count ?
>                 currentCapture->restart
>         }()
> }
>
>
> This allows you to do
> ’00001’ -> removeleading(’0’, 1)
> ’00001’ -> removeleading(’0’, 2)
> ’00001’ -> removeleading(’0’, 3)
>
> ->
> 0001
> 001
> 01
>
> The plain, standard, string -> removeleading of course continues to work
> as before.
>
> HDB
> Jolle
>
> #############################################################
>
> This message is sent to you because you are subscribed to
>   the mailing list Lasso [hidden email]
> Official list archives available at http://www.lassotalk.com
> To unsubscribe, E-mail to: <[hidden email]>
> Send administrative queries to  <[hidden email]>

#############################################################

This message is sent to you because you are subscribed to
  the mailing list Lasso [hidden email]
Official list archives available at http://www.lassotalk.com
To unsubscribe, E-mail to: <[hidden email]>
Send administrative queries to  <[hidden email]>
Reply | Threaded
Open this post in threaded view
|

Re: removeleading with limits

Jolle Carlestam-2
10 nov 2015 kl. 14:17 skrev Bil Corry <[hidden email]>:
>
> I did something similar when L9 first came out, but instead of specifying
> how many to remove, I specified how many I wanted.  E.g.
>
> ’00001’ -> removeleading(’0’, 1)
> ’00001’ -> removeleading(’0’, 2)
> ’00001’ -> removeleading(’0’, 3)

Haha, that’s funny! Let’s hope our code snippets never meet. :-)

The functionality yours is adding can, somewhat, be done using string->padLeading.

HDB
Jolle

#############################################################

This message is sent to you because you are subscribed to
  the mailing list Lasso [hidden email]
Official list archives available at http://www.lassotalk.com
To unsubscribe, E-mail to: <[hidden email]>
Send administrative queries to  <[hidden email]>