Re: Variant of 'with' which returns target object

2016-06-06 Thread Eduardo Cavazos
Here's another example from gtk-simple:

https://github.com/perl6/gtk-simple/blob/d1fcc6efe1da3fd88a26b4328d6537c58607dee7/examples/07-text.pl6

Written with cascade:

https://gist.github.com/dharmatech/b8e8a71da8149382f192603e1b92d9b8

Ed

On Mon, Jun 6, 2016 at 4:12 PM, Eduardo Cavazos 
wrote:

> Looks like I can get pretty close to what I was looking for with this
> subroutine:
>
> sub cascade ($obj, &proc) { proc($obj); $obj; }
>
> Then, given the Point class again:
>
> class Point { has $.x is rw; has $.y is rw; }
>
> this:
>
> (Point.new.&cascade: {.x = 10;}; Point.new.&cascade: {.y = 20;})
>
> returns:
>
> (Point.new(x => 10, y => Any) Point.new(x => Any, y => 20))
>
> This came out of looking to see if it was possible to support method
> cascade syntax in Perl 6. See this question on stackoverflow:
>
> http://stackoverflow.com/questions/37559870/method-cascade-syntax
>
> Below, I've included the gtk-simple example written to use 'cascade'.
>
> As an aside, the Swift community has been discussing adding something like
> 'cascade' to their standard library under the name 'with':
>
> https://gist.github.com/erica/96d9c5bb4eaa3ed3b2ff82dc35aa8dae
>
> Ed
>
> use v6;
>
> use GTK::Simple;
>
> sub cascade ($obj, &proc) { proc($obj); $obj; }
>
> GTK::Simple::App.new(title => 'abc').&cascade: {
>
> my $app = $_;
>
> my $button;
>
> .set-content:
> GTK::Simple::VBox.new(
> GTK::Simple::Button.new(label => 'bcd').&cascade: {
> .clicked.tap({ .sensitive = False; $button.sensitive =
> True; });
>};
>
>$button = GTK::Simple::Button.new(label => 'cde').&cascade: {
>.clicked.tap({ $app.exit; });
>}
> );
>
> .border-width = 20;
>
> .run;
> };
>
>
>
> On Mon, Jun 6, 2016 at 1:12 PM, Eduardo Cavazos 
> wrote:
>
>> Hello,
>>
>> Here's a simple class:
>>
>> class Point { has $.x is rw; has $.y is rw; }
>>
>> 'with' seems to return the result of evaluating the block. E.g. this
>> expression will return a Point:
>>
>> with Point.new { .x = 10; $_; }
>>
>> Whereas this will return 10:
>>
>> with Point.new { .x = 10; }
>>
>> Is there a way for a user to define a variant of 'with' which returns the
>> target object instead of the result of the evaluating the block? I.e.:
>>
>> with_alt Point.new { .x = 10; }
>>
>> would return a Point.
>>
>> And this:
>>
>> (with_alt Point.new {.x = 10}; with_alt Point.new {.y = 20})
>>
>> would return a list of two Points.
>>
>> Thanks!
>>
>> Ed
>>
>
>


Re: Variant of 'with' which returns target object

2016-06-06 Thread yary
On Mon, Jun 6, 2016 at 5:47 PM, yary  wrote:

> GTK::Simple::Button.new(label => 'bcd').clicked. ...


whoops, that should be

with GTK::Simple::Button.new(label => 'bcd') {
 .clicked.tap({ .sensitive = False; $button.sensitive =
True; });
}

alas I am on windows and Panda can't install GTK::Simple on my machine, not
able to test.

-y


Re: Variant of 'with' which returns target object

2016-06-06 Thread yary
That's a nice & small answer. It does seem overkill for the gtk example...

use v6;

use GTK::Simple;

with GTK::Simple::App.new(title => 'abc') {

my $app = $_;

my $button;

.set-content:
GTK::Simple::VBox.new(
GTK::Simple::Button.new(label => 'bcd').clicked.tap({
.sensitive = False; $button.sensitive = True; });

   $button = GTK::Simple::Button.new(label => 'cde');
   $button.clicked.tap({ $app.exit; });
);

.border-width = 20;

.run;
};


Re: Variant of 'with' which returns target object

2016-06-06 Thread Eduardo Cavazos
Looks like I can get pretty close to what I was looking for with this
subroutine:

sub cascade ($obj, &proc) { proc($obj); $obj; }

Then, given the Point class again:

class Point { has $.x is rw; has $.y is rw; }

this:

(Point.new.&cascade: {.x = 10;}; Point.new.&cascade: {.y = 20;})

returns:

(Point.new(x => 10, y => Any) Point.new(x => Any, y => 20))

This came out of looking to see if it was possible to support method
cascade syntax in Perl 6. See this question on stackoverflow:

http://stackoverflow.com/questions/37559870/method-cascade-syntax

Below, I've included the gtk-simple example written to use 'cascade'.

As an aside, the Swift community has been discussing adding something like
'cascade' to their standard library under the name 'with':

https://gist.github.com/erica/96d9c5bb4eaa3ed3b2ff82dc35aa8dae

Ed

use v6;

use GTK::Simple;

sub cascade ($obj, &proc) { proc($obj); $obj; }

GTK::Simple::App.new(title => 'abc').&cascade: {

my $app = $_;

my $button;

.set-content:
GTK::Simple::VBox.new(
GTK::Simple::Button.new(label => 'bcd').&cascade: {
.clicked.tap({ .sensitive = False; $button.sensitive =
True; });
   };

   $button = GTK::Simple::Button.new(label => 'cde').&cascade: {
   .clicked.tap({ $app.exit; });
   }
);

.border-width = 20;

.run;
};



On Mon, Jun 6, 2016 at 1:12 PM, Eduardo Cavazos 
wrote:

> Hello,
>
> Here's a simple class:
>
> class Point { has $.x is rw; has $.y is rw; }
>
> 'with' seems to return the result of evaluating the block. E.g. this
> expression will return a Point:
>
> with Point.new { .x = 10; $_; }
>
> Whereas this will return 10:
>
> with Point.new { .x = 10; }
>
> Is there a way for a user to define a variant of 'with' which returns the
> target object instead of the result of the evaluating the block? I.e.:
>
> with_alt Point.new { .x = 10; }
>
> would return a Point.
>
> And this:
>
> (with_alt Point.new {.x = 10}; with_alt Point.new {.y = 20})
>
> would return a list of two Points.
>
> Thanks!
>
> Ed
>


Re: Variant of 'with' which returns target object

2016-06-06 Thread Elizabeth Mattijsen

> On 06 Jun 2016, at 21:17, Eduardo Cavazos  wrote:
> 
> On Mon, Jun 6, 2016 at 2:08 PM, yary  wrote:
> 
> For your particular case, would it be sufficient to set the values in
> the constructor?
> 
> with Point.new( :x(10) ) { .say }
>   # says "Point.new(x => 10, y => Any)"
> 
> Normally, that would be a good way to construct the Points in my example. I'm 
> just using the approach in my note to demonstrate 'with'.

“with” is completely agnostic about what it is working on.  It merely checks 
for definedness and sets the topicalizer if so.

Please also note there is nothing special about the method .new per se: it’s 
just that there is some nice functionality built into the core if you don’t 
create your own method “new” in your class and you inherit from Any (which is 
the default).

Your suggestion seems rather specialized for your situation.


Liz

Re: Variant of 'with' which returns target object

2016-06-06 Thread Eduardo Cavazos
On Mon, Jun 6, 2016 at 2:08 PM, yary  wrote:

>
> For your particular case, would it be sufficient to set the values in
> the constructor?
>
> with Point.new( :x(10) ) { .say }
>   # says "Point.new(x => 10, y => Any)"
>

Normally, that would be a good way to construct the Points in my example.
I'm just using the approach in my note to demonstrate 'with'.

Ed


Re: Variant of 'with' which returns target object

2016-06-06 Thread yary
On Mon, Jun 6, 2016 at 2:12 PM, Eduardo Cavazos  wrote:
>
> with_alt Point.new { .x = 10; }
>
> would return a Point.
>
> And this:
>
> (with_alt Point.new {.x = 10}; with_alt Point.new {.y = 20})

For your particular case, would it be sufficient to set the values in
the constructor?

with Point.new( :x(10) ) { .say }
  # says "Point.new(x => 10, y => Any)"

with Point.new( :x(10) ), Point.new( :y(20) ) { .perl }
  # says "(Point.new(x => 10, y => Any), Point.new(x => Any, y => 20))"

It's not the general solution, but it does the trick for the examples.

-y