Re: Objects, TWEAK and return

2021-03-22 Thread yary
Agreed!
-y


On Mon, Mar 22, 2021 at 6:45 PM Ralph Mellor 
wrote:

> On Mon, Mar 22, 2021 at 2:12 PM yary  wrote:
> >
> > It's not good practice to use "map" for side effects only, discarding
> > the returned value–which happens here
>
> I agree.
>
> > Would [using `for`] also work-around the lack of sink context in TWEAK?
>
> Yes.
>
> > I think it is a cause of the unexpected behavior.
>
> Perhaps.
>
> That said, in response to the issue I filed in which I wrote:
>
> > BUILD and TWEAK are not called in sink context by the existing standard
> > object construction machinery shipping with standard Raku 6.d. Perhaps it
> > would be best if they were?
>
> jnthn has commented:
>
> > Yes, with a potential optimization of seeing if the BUILD or TWEAK
> already
> > declared its return type as Nil (which is a common practice) so we don't
> > increase code size of the generated BUILDALL without a need.
>
> --
> love raiph
>


Re: Objects, TWEAK and return

2021-03-22 Thread Ralph Mellor
On Mon, Mar 22, 2021 at 2:12 PM yary  wrote:
>
> It's not good practice to use "map" for side effects only, discarding
> the returned value–which happens here

I agree.

> Would [using `for`] also work-around the lack of sink context in TWEAK?

Yes.

> I think it is a cause of the unexpected behavior.

Perhaps.

That said, in response to the issue I filed in which I wrote:

> BUILD and TWEAK are not called in sink context by the existing standard
> object construction machinery shipping with standard Raku 6.d. Perhaps it
> would be best if they were?

jnthn has commented:

> Yes, with a potential optimization of seeing if the BUILD or TWEAK already
> declared its return type as Nil (which is a common practice) so we don't
> increase code size of the generated BUILDALL without a need.

--
love raiph


Re: Objects, TWEAK and return

2021-03-22 Thread yary
Good to see this investigated down to the details, yet I just realized
something that was bothering me about it. Going back to the original post:

 submethod TWEAK {
$!filelist.lines».split(',').map( -> ($a, $b) { @!show.push: ( $a, $b
)  });
  }

It's not good practice to use "map" for side effects only, discarding the
returned value–which happens here, and I think it is a cause of the
unexpected behavior.

Would this also work-around the lack of sink context in TWEAK?

 submethod TWEAK {
for $!filelist.lines».split(',')  -> ($a, $b) { @!show.push: ( $a, $b
)  };
  }

-y


On Sun, Mar 21, 2021 at 7:11 PM Ralph Mellor 
wrote:

> On Sun, Mar 21, 2021 at 9:47 PM  wrote:
> >
> > Waw! :) Following your examples and suggestions, these work:
> >
> > > class { submethod TWEAK(-->Nil) { Any.map: {say 99}  } }.new;
> > > class { submethod TWEAK { sink Any.map: {say 99}  } }.new;
> > > class { submethod TWEAK { eager Any.map: {say 99}  } }.new;
> >
> > I really appreciate your discussion. Let me know if you think it
> > is worth to include this issue in the TWEAK Raku documentation.
>
> I think the first step is to see if it's considered a Raku(do) bug.
>
> I've filed an issue: https://github.com/rakudo/rakudo/issues/4260
>
> --
> love, raiph
>


Re: Objects, TWEAK and return

2021-03-21 Thread Ralph Mellor
On Sun, Mar 21, 2021 at 9:47 PM  wrote:
>
> Waw! :) Following your examples and suggestions, these work:
>
> > class { submethod TWEAK(-->Nil) { Any.map: {say 99}  } }.new;
> > class { submethod TWEAK { sink Any.map: {say 99}  } }.new;
> > class { submethod TWEAK { eager Any.map: {say 99}  } }.new;
>
> I really appreciate your discussion. Let me know if you think it
> is worth to include this issue in the TWEAK Raku documentation.

I think the first step is to see if it's considered a Raku(do) bug.

I've filed an issue: https://github.com/rakudo/rakudo/issues/4260

--
love, raiph


Re: Objects, TWEAK and return

2021-03-21 Thread mimosinnet
Waw! :) Following your examples and suggestions, these work:

> class { submethod TWEAK(-->Nil) { Any.map: {say 99}  } }.new;
> class { submethod TWEAK { sink Any.map: {say 99}  } }.new;
> class { submethod TWEAK { eager Any.map: {say 99}  } }.new;

I really appreciate your discussion. Let me know if you think it is worth
to include this issue in the TWEAK Raku documentation.

Thanks very much for the detailed explanation.

Cheers!


Missatge de Ralph Mellor  del dia dg., 14 de març
2021 a les 23:00:

> On Sun, Mar 14, 2021 at 2:12 AM Brad Gilbert  wrote:
> >
> > Ralph, the last value in all functions are not sunk by default,
> > so of course the last one in `TWEAK` is not sunk by default.
>
> Right. Thanks for correcting this misleading part of my explanation.
>
> > The bug is that the calling code is not sinking the result.
>
> The code that normally calls BUILD and TWEAK is shipped with
> standard Raku settings. It isn't sinking the result. That sounds like
> a bug to me. Though perhaps it's not one worth considering fixing
> this side of RakuAST landing with its new sink handling code that
> will, with luck, have higher performance.
>
> (That is what I was trying to say, but it was 3am...)
>
> --
> love, raiph
>


Re: Objects, TWEAK and return

2021-03-14 Thread Ralph Mellor
On Sun, Mar 14, 2021 at 2:12 AM Brad Gilbert  wrote:
>
> Ralph, the last value in all functions are not sunk by default,
> so of course the last one in `TWEAK` is not sunk by default.

Right. Thanks for correcting this misleading part of my explanation.

> The bug is that the calling code is not sinking the result.

The code that normally calls BUILD and TWEAK is shipped with
standard Raku settings. It isn't sinking the result. That sounds like
a bug to me. Though perhaps it's not one worth considering fixing
this side of RakuAST landing with its new sink handling code that
will, with luck, have higher performance.

(That is what I was trying to say, but it was 3am...)

--
love, raiph


Re: Objects, TWEAK and return

2021-03-13 Thread Brad Gilbert
Ralph, the last value in all functions are not sunk by default, so of
course the last one in `TWEAK` is not sunk by default.
This is intended behaviour.

It is up to the code that calls a function to sink the result if that is
the desired behaviour.

sub foo {
'a b c'.words».uc.map: *.say;
}

my $ = foo(); # nothing is printed

sink foo(); # «A␤B␤C␤» is printed

The bug is that the calling code is not sinking the result.


On Sat, Mar 13, 2021 at 8:05 PM Ralph Mellor 
wrote:

> Here's a golf:
>
> class { submethod TWEAK  { Any.map: {say 99} } }.new; #
> class { submethod TWEAK  { Any.map: {say 99}; 42 } }.new; # 99
> class { submethod TWEAK (--> 42) { Any.map: {say 99} } }.new; # 99
>
> The last line in a `BUILD` or `TWEAK` submethod is not eagerly evaluated
> by default.
>
> That sounds like a bug to me. But if not, and in the meantime,
> because your `$!filelist.lines...` line is the last one in your `TWEAK`
> submethod, the `@!show` array is being left empty, so the value to
> the right of `%` evaluates to zero, hence the error message you're seeing.
>
> On Sat, Mar 13, 2021 at 8:30 PM  wrote:
> >
> > Hi,
> >
> > When working with this week challenge for the PerlWeeklyChallenge, I
> noticed this behaviour with TWEAK:
> >
> > This does not work:
> >   submethod TWEAK {
> > $!filelist.lines».split(',').map( -> ($a, $b) { @!show.push: ( $a,
> $b )  });
> >   }
> >
> > This works:
> >   submethod TWEAK {
> > $!filelist.lines».split(',').map( -> ($a, $b) { @!show.push: ( $a,
> $b )  });
> > return;
> >   }
> >
> > It also works with other commands instead of 'return' (like assigning a
> value to a variable). From the examples in the documentation, I am not
> certain this is the expected behaviour.
> >
> > Thanks for this extraordinary language,
> >
> > Joan
> >
> >
> > P.S: This is the context where the command appears.  Apologies for the
> messy-Raku,
> > --
> > use Test;
> >
> > my $data = '1709363,"Les Miserables Episode 1: The Bishop (broadcast
> date: 1937-07-23)"
> > 1723781,"Les Miserables Episode 2: Javert (broadcast date: 1937-07-30)"
> > 1723781,"Les Miserables Episode 3: The Trial (broadcast date:
> 1937-08-06)"
> > 1678356,"Les Miserables Episode 4: Cosette (broadcast date: 1937-08-13)"
> > 1646043,"Les Miserables Episode 5: The Grave (broadcast date:
> 1937-08-20)"
> > 1714640,"Les Miserables Episode 6: The Barricade (broadcast date:
> 1937-08-27)"
> > 1714640,"Les Miserables Episode 7: Conclusion (broadcast date:
> 1937-09-03)"';
> >
> > class Movies {
> >
> >   has $.starttime;
> >   has $.currenttime;
> >   has $.filelist;
> >   has @!show; # ( [ time, show ] )
> >
> >   submethod TWEAK {
> > # miliseconds -> seconds
> > $!filelist.lines».split(',').map( -> ($a, $b) { @!show.push: ( $a,
> $b )  });
> > return;
> >   }
> >
> >   method what-show() {
> > my $position =  ( $!currenttime - $!starttime ) %
> @!show[*;0].sum/1000;
> > my ($time, $show);
> > for @!show[*;0] -> $show-time {
> >   $time += $show-time;
> >   return @!show[$show++;1] if $time > $position;
> > }
> >   }
> > }
> >
> > my $mv = Movies.new(
> >   starttime   => '1606134123',
> >   currenttime => '1614591276',
> >   filelist=> $data
> > );
> >
> > is $mv.what-show, '"Les Miserables Episode 1: The Bishop (broadcast
> date: 1937-07-23)"';
>


Re: Objects, TWEAK and return

2021-03-13 Thread Ralph Mellor
Here's a golf:

class { submethod TWEAK  { Any.map: {say 99} } }.new; #
class { submethod TWEAK  { Any.map: {say 99}; 42 } }.new; # 99
class { submethod TWEAK (--> 42) { Any.map: {say 99} } }.new; # 99

The last line in a `BUILD` or `TWEAK` submethod is not eagerly evaluated
by default.

That sounds like a bug to me. But if not, and in the meantime,
because your `$!filelist.lines...` line is the last one in your `TWEAK`
submethod, the `@!show` array is being left empty, so the value to
the right of `%` evaluates to zero, hence the error message you're seeing.

On Sat, Mar 13, 2021 at 8:30 PM  wrote:
>
> Hi,
>
> When working with this week challenge for the PerlWeeklyChallenge, I noticed 
> this behaviour with TWEAK:
>
> This does not work:
>   submethod TWEAK {
> $!filelist.lines».split(',').map( -> ($a, $b) { @!show.push: ( $a, $b )  
> });
>   }
>
> This works:
>   submethod TWEAK {
> $!filelist.lines».split(',').map( -> ($a, $b) { @!show.push: ( $a, $b )  
> });
> return;
>   }
>
> It also works with other commands instead of 'return' (like assigning a value 
> to a variable). From the examples in the documentation, I am not certain this 
> is the expected behaviour.
>
> Thanks for this extraordinary language,
>
> Joan
>
>
> P.S: This is the context where the command appears.  Apologies for the 
> messy-Raku,
> --
> use Test;
>
> my $data = '1709363,"Les Miserables Episode 1: The Bishop (broadcast date: 
> 1937-07-23)"
> 1723781,"Les Miserables Episode 2: Javert (broadcast date: 1937-07-30)"
> 1723781,"Les Miserables Episode 3: The Trial (broadcast date: 1937-08-06)"
> 1678356,"Les Miserables Episode 4: Cosette (broadcast date: 1937-08-13)"
> 1646043,"Les Miserables Episode 5: The Grave (broadcast date: 1937-08-20)"
> 1714640,"Les Miserables Episode 6: The Barricade (broadcast date: 1937-08-27)"
> 1714640,"Les Miserables Episode 7: Conclusion (broadcast date: 1937-09-03)"';
>
> class Movies {
>
>   has $.starttime;
>   has $.currenttime;
>   has $.filelist;
>   has @!show; # ( [ time, show ] )
>
>   submethod TWEAK {
> # miliseconds -> seconds
> $!filelist.lines».split(',').map( -> ($a, $b) { @!show.push: ( $a, $b )  
> });
> return;
>   }
>
>   method what-show() {
> my $position =  ( $!currenttime - $!starttime ) % @!show[*;0].sum/1000;
> my ($time, $show);
> for @!show[*;0] -> $show-time {
>   $time += $show-time;
>   return @!show[$show++;1] if $time > $position;
> }
>   }
> }
>
> my $mv = Movies.new(
>   starttime   => '1606134123',
>   currenttime => '1614591276',
>   filelist=> $data
> );
>
> is $mv.what-show, '"Les Miserables Episode 1: The Bishop (broadcast date: 
> 1937-07-23)"';


Re: Objects, TWEAK and return

2021-03-13 Thread Brad Gilbert
I think that this is caused because it is returning a 「Sequence」 that is
not getting sunk.
This is because the last value from a function is never sunk in that
function.

You could also use 「eager」 「sink」 or follow it with 「Nil」 or some other
value (instead of 「return」)

eager $!filelist.lines».split(',').map( -> ($a, $b) { @!show.push: (
$a, $b )  });
sink $!filelist.lines».split(',').map( -> ($a, $b) { @!show.push: ( $a,
$b )  });
$!filelist.lines».split(',').map( -> ($a, $b) { @!show.push: ( $a, $b )
 });  Nil
$!filelist.lines».split(',').map( -> ($a, $b) { @!show.push: ( $a, $b )
 });  'some other value'

You can also make 「TWEAK」 return a 「Nil」 in the signature (or any
constant/literal).

submethod TWEAK (-->Nil) { … }

Since the last value in the function is never returned it is also sunk.

---

It might be a good idea if the code that calls 「TWEAK」 (and maybe 「BUILD」)
sinks any result that they get.
Or maybe it should only sink 「Sequence」 values.

There are good reasons for allowing a 「submethod」 to return a value that
doesn't get sunk, so that should not change.

At any rate I don't think there is a bug in how 「submethod TWEAK」 is
compiled and run.
That part is working as intended.

On Sat, Mar 13, 2021 at 2:30 PM  wrote:

> Hi,
>
> When working with this week challenge for the PerlWeeklyChallenge
> , I noticed this behaviour with TWEAK:
>
> *This does not work:*
>   submethod TWEAK {
> $!filelist.lines».split(',').map( -> ($a, $b) { @!show.push: ( $a, $b
> )  });
>   }
>
> *This works:*
>   submethod TWEAK {
> $!filelist.lines».split(',').map( -> ($a, $b) { @!show.push: ( $a, $b
> )  });
> return;
>   }
>
> It also works with other commands instead of 'return' (like assigning a
> value to a variable). From the examples in the documentation, I am not
> certain this is the expected behaviour.
>
> Thanks for this extraordinary language,
>
> Joan
>
>
> P.S: This is the context where the command appears.  Apologies for the
> messy-Raku,
> --
> use Test;
>
> my $data = '1709363,"Les Miserables Episode 1: The Bishop (broadcast date:
> 1937-07-23)"
> 1723781,"Les Miserables Episode 2: Javert (broadcast date: 1937-07-30)"
> 1723781,"Les Miserables Episode 3: The Trial (broadcast date: 1937-08-06)"
> 1678356,"Les Miserables Episode 4: Cosette (broadcast date: 1937-08-13)"
> 1646043,"Les Miserables Episode 5: The Grave (broadcast date: 1937-08-20)"
> 1714640,"Les Miserables Episode 6: The Barricade (broadcast date:
> 1937-08-27)"
> 1714640,"Les Miserables Episode 7: Conclusion (broadcast date:
> 1937-09-03)"';
>
> class Movies {
>
>   has $.starttime;
>   has $.currenttime;
>   has $.filelist;
>   has @!show; # ( [ time, show ] )
>
>   submethod TWEAK {
> # miliseconds -> seconds
> $!filelist.lines».split(',').map( -> ($a, $b) { @!show.push: ( $a, $b
> )  });
> return;
>   }
>
>   method what-show() {
> my $position =  ( $!currenttime - $!starttime ) % @!show[*;0].sum/1000;
> my ($time, $show);
> for @!show[*;0] -> $show-time {
>   $time += $show-time;
>   return @!show[$show++;1] if $time > $position;
> }
>   }
> }
>
> my $mv = Movies.new(
>   starttime   => '1606134123',
>   currenttime => '1614591276',
>   filelist=> $data
> );
>
> is $mv.what-show, '"Les Miserables Episode 1: The Bishop (broadcast date:
> 1937-07-23)"';
>


Re: Objects, TWEAK and return

2021-03-13 Thread Vadim Belman

At the first glance it looks like bug. Possibly a result of over-optimization. 
Worth opening an issue at https://github.com/rakudo/rakudo/issues

Best regards,
Vadim Belman

> On Mar 13, 2021, at 3:29 PM, mimosin...@gmail.com wrote:
> 
> Hi,
> 
> When working with this week challenge for the PerlWeeklyChallenge 
> , I noticed this behaviour with TWEAK:
> 
> This does not work:
>   submethod TWEAK {
> $!filelist.lines».split(',').map( -> ($a, $b) { @!show.push: ( $a, $b )  
> });
>   }
> 
> This works:
>   submethod TWEAK {
> $!filelist.lines».split(',').map( -> ($a, $b) { @!show.push: ( $a, $b )  
> });
> return;
>   }
> 
> It also works with other commands instead of 'return' (like assigning a value 
> to a variable). From the examples in the documentation, I am not certain this 
> is the expected behaviour.
> 
> Thanks for this extraordinary language,
> 
> Joan
> 
> 
> P.S: This is the context where the command appears.  Apologies for the 
> messy-Raku, 
> --
> use Test;
> 
> my $data = '1709363,"Les Miserables Episode 1: The Bishop (broadcast date: 
> 1937-07-23)"
> 1723781,"Les Miserables Episode 2: Javert (broadcast date: 1937-07-30)"
> 1723781,"Les Miserables Episode 3: The Trial (broadcast date: 1937-08-06)"
> 1678356,"Les Miserables Episode 4: Cosette (broadcast date: 1937-08-13)"
> 1646043,"Les Miserables Episode 5: The Grave (broadcast date: 1937-08-20)"
> 1714640,"Les Miserables Episode 6: The Barricade (broadcast date: 1937-08-27)"
> 1714640,"Les Miserables Episode 7: Conclusion (broadcast date: 1937-09-03)"';
> 
> class Movies {
> 
>   has $.starttime;
>   has $.currenttime;
>   has $.filelist;
>   has @!show; # ( [ time, show ] )
> 
>   submethod TWEAK {
> # miliseconds -> seconds
> $!filelist.lines».split(',').map( -> ($a, $b) { @!show.push: ( $a, $b )  
> });
> return;
>   }
> 
>   method what-show() {
> my $position =  ( $!currenttime - $!starttime ) % @!show[*;0].sum/1000;
> my ($time, $show);
> for @!show[*;0] -> $show-time {
>   $time += $show-time;
>   return @!show[$show++;1] if $time > $position;
> }
>   }
> }
> 
> my $mv = Movies.new( 
>   starttime   => '1606134123',
>   currenttime => '1614591276',
>   filelist=> $data
> );
> 
> is $mv.what-show, '"Les Miserables Episode 1: The Bishop (broadcast date: 
> 1937-07-23)"';



Objects, TWEAK and return

2021-03-13 Thread mimosinnet
Hi,

When working with this week challenge for the PerlWeeklyChallenge
, I noticed this behaviour with TWEAK:

*This does not work:*
  submethod TWEAK {
$!filelist.lines».split(',').map( -> ($a, $b) { @!show.push: ( $a, $b
)  });
  }

*This works:*
  submethod TWEAK {
$!filelist.lines».split(',').map( -> ($a, $b) { @!show.push: ( $a, $b
)  });
return;
  }

It also works with other commands instead of 'return' (like assigning a
value to a variable). From the examples in the documentation, I am not
certain this is the expected behaviour.

Thanks for this extraordinary language,

Joan


P.S: This is the context where the command appears.  Apologies for the
messy-Raku,
--
use Test;

my $data = '1709363,"Les Miserables Episode 1: The Bishop (broadcast date:
1937-07-23)"
1723781,"Les Miserables Episode 2: Javert (broadcast date: 1937-07-30)"
1723781,"Les Miserables Episode 3: The Trial (broadcast date: 1937-08-06)"
1678356,"Les Miserables Episode 4: Cosette (broadcast date: 1937-08-13)"
1646043,"Les Miserables Episode 5: The Grave (broadcast date: 1937-08-20)"
1714640,"Les Miserables Episode 6: The Barricade (broadcast date:
1937-08-27)"
1714640,"Les Miserables Episode 7: Conclusion (broadcast date:
1937-09-03)"';

class Movies {

  has $.starttime;
  has $.currenttime;
  has $.filelist;
  has @!show; # ( [ time, show ] )

  submethod TWEAK {
# miliseconds -> seconds
$!filelist.lines».split(',').map( -> ($a, $b) { @!show.push: ( $a, $b
)  });
return;
  }

  method what-show() {
my $position =  ( $!currenttime - $!starttime ) % @!show[*;0].sum/1000;
my ($time, $show);
for @!show[*;0] -> $show-time {
  $time += $show-time;
  return @!show[$show++;1] if $time > $position;
}
  }
}

my $mv = Movies.new(
  starttime   => '1606134123',
  currenttime => '1614591276',
  filelist=> $data
);

is $mv.what-show, '"Les Miserables Episode 1: The Bishop (broadcast date:
1937-07-23)"';