Skip to content
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

:nth() does not work with :x() or :g in .subst in Rakudo #467

Closed
p6rt opened this issue Dec 7, 2008 · 6 comments
Closed

:nth() does not work with :x() or :g in .subst in Rakudo #467

p6rt opened this issue Dec 7, 2008 · 6 comments

Comments

@p6rt
Copy link

p6rt commented Dec 7, 2008

Migrated from rt.perl.org#61130 (status was 'resolved')

Searchable as RT61130$

@p6rt
Copy link
Author

p6rt commented Dec 7, 2008

From @masak

The .subst method in Rakudo r33599 can understand :x()...

$ perl6 -e 'say "foo1foo2foo3foo4".subst("foo", "bar", :x(2))' # yes
bar1bar2foo3foo4

...and :nth()...

$ perl6 -e 'say "foo1foo2foo3foo4".subst("foo", "bar", :nth(2))' # yes
foo1bar2foo3foo4

...and :g...

$ perl6 -e 'say "foo1foo2foo3foo4".subst("foo", "bar", :g)' # yes
bar1bar2bar3bar4

...but not :x() together with :nth()...

$ perl6 -e 'say "foo1foo2foo3foo4".subst("foo", "bar", :x(2),
:nth(2))' # expected foo1bar2foo3bar4
foo1bar2foo3foo4

...and not :g together with :nth().

$ perl6 -e 'say "foo1foo2foo3foo4".subst("foo", "bar", :g, :nth(2))' #
expected foo1bar2foo3bar4
foo1bar2foo3foo4

The above are my personal expectations. The current version of S05 is
silent on how :nth() interacts with :x() and :g. There are spectests
for :g​:nth but not (as far as I can see) for :x​:nth.

@p6rt
Copy link
Author

p6rt commented Dec 7, 2008

From @moritz

On Sun Dec 07 07​:24​:07 2008, masak wrote​:

The .subst method in Rakudo r33599 can understand :x()...

$ perl6 -e 'say "foo1foo2foo3foo4".subst("foo", "bar", :x(2))' # yes
bar1bar2foo3foo4

...and :nth()...

$ perl6 -e 'say "foo1foo2foo3foo4".subst("foo", "bar", :nth(2))' # yes
foo1bar2foo3foo4

...and :g...

$ perl6 -e 'say "foo1foo2foo3foo4".subst("foo", "bar", :g)' # yes
bar1bar2bar3bar4

...but not :x() together with :nth()...

$ perl6 -e 'say "foo1foo2foo3foo4".subst("foo", "bar", :x(2),
:nth(2))' # expected foo1bar2foo3bar4
foo1bar2foo3foo4

...and not :g together with :nth().

$ perl6 -e 'say "foo1foo2foo3foo4".subst("foo", "bar", :g, :nth(2))' #
expected foo1bar2foo3bar4
foo1bar2foo3foo4

The above are my personal expectations. The current version of S05 is
silent on how :nth() interacts with :x() and :g. There are spectests
for :g​:nth but not (as far as I can see) for :x​:nth.

Since your personal expectations are the same as mine, I took the liberty to
turn our expectations into spec tests, in
t/spec/S05-substitution/subst.t (pugs r24207).

The reasoning behind it is quite simple​: I imagine :g to mean the same as
:x(*). Now a :x($x) and :nth($n) interact like this​:

for 1 .. $x {
  match here
  if ($x-1) % $n == 0 {
  do substitution
  }
}

(CC'ing p6l, since it defines language semantics, albeit just a bit)

@p6rt
Copy link
Author

p6rt commented Dec 7, 2008

The RT System itself - Status changed from 'new' to 'open'

@p6rt
Copy link
Author

p6rt commented Dec 8, 2008

From @pmichaud

On Sun, Dec 07, 2008 at 03​:09​:30PM -0800, Moritz Lenz via RT wrote​:

...but not :x() together with :nth()...

$ perl6 -e 'say "foo1foo2foo3foo4".subst("foo", "bar", :x(2),
:nth(2))' # expected foo1bar2foo3bar4
foo1bar2foo3foo4

The above are my personal expectations. The current version of S05 is
silent on how :nth() interacts with :x() and :g. There are spectests
for :g​:nth but not (as far as I can see) for :x​:nth.

Since your personal expectations are the same as mine, I took the liberty to
turn our expectations into spec tests, in
t/spec/S05-substitution/subst.t (pugs r24207).

The reasoning behind it is quite simple​: I imagine :g to mean the same as
:x(*). Now a :x($x) and :nth($n) interact like this​:

for 1 .. $x {
match here
if ($x-1) % $n == 0 {
do substitution
}
}

(CC'ing p6l, since it defines language semantics, albeit just a bit)

The problem with this reasoning is that :nth doesn't have to be an
integer -- S05 also allows things like

  :nth(1,2,3,5,8,13)
  :nth({.is_fibonacci})

It's not immediately obvious how those forms of :nth would interact
with :x or :g.

The way to do something like "replace every 3rd occurrence" would
seem to be to do things like​:

  :nth({ $_ % 3 == 0 })
  :nth(3..*​:by(3))

Another problem is that :x($n) specifies "perform $n substitutions",
such that if we say :x(4) and there aren't four things to substitute,
then none of the substitutions take place.

At the moment I think that :x specifies a constraint on the
total number of substitutions that are (must be) performed,
while :nth selects the candidate matches to be substituted.
Thus

  .subst( $pat, $rep, :nth(1,4,9,16,25,36), :x(4) )

would perform substitutions on the 1st, 4th, 9th, and 16th matches,
and do nothing if at least sixteen matches were not found.

Pm

@p6rt
Copy link
Author

p6rt commented May 31, 2010

From @moritz

It works now as currently specced, with lots of tests in
S05-substitution/subst.t

@p6rt p6rt closed this as completed May 31, 2010
@p6rt
Copy link
Author

p6rt commented May 31, 2010

@moritz - Status changed from 'open' to 'resolved'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant