Re: list comprehension

2019-02-24 Thread Brian Duggan
Yes, +1 and we have this documented on the py-to-perl6 nutshell page:

https://docs.perl6.org/language/py-nutshell#List_comprehensions

On Friday, February 22, Lucas Buchala wrote: 
> Hello folks. Did I understand correctly that this thread is about list
> comprehension syntax in Perl 6? :-)
> I don't if it was mentioned, but I think this syntax just simply works
> already. See an example:
> 
>   > say ($_~$_ if $_ %% 2 for ^10).Set
>   set(00 22 44 66 88)
> 
>   > say ($_.item if $_[0] eq 'a' or $_[1] == 2 for  X ^3).Set
>   set((a 0) (a 1) (a 2) (b 2) (c 2))
> 
> (Just drop the .Set from the end if it's not needed)


Re: list comprehension

2019-02-22 Thread Lucas Buchala
Hello folks. Did I understand correctly that this thread is about list
comprehension syntax in Perl 6? :-)
I don't if it was mentioned, but I think this syntax just simply works
already. See an example:

  > say ($_~$_ if $_ %% 2 for ^10).Set
  set(00 22 44 66 88)

  > say ($_.item if $_[0] eq 'a' or $_[1] == 2 for  X ^3).Set
  set((a 0) (a 1) (a 2) (b 2) (c 2))

(Just drop the .Set from the end if it's not needed)


Re: list comprehension

2019-02-21 Thread Joan Pujol Tarrés
El Monday, 11 de February del 2019 a les 17:04, Brad Gilbert va escriure:

>Actually I would suggest NOT adding Perl6, because the best way to
>create a Set is not to use “list comprehension”, but to just call
>`.Set`

Ups :O .Thanks for the conceptual clarification. :D  I will leave Perl6 
out of the wikipedia article. 

Cheers! 

>On Mon, Feb 11, 2019 at 12:51 PM mimosinnet  
>wrote:
>>
>> Dear Brad,
>>
>> Thanks very much for the answer. I have been playing with your examples in 
>> the
>> code below (and learned a lot!). Based on your insight, I would suggest these
>> solutions to be added to the wikipedia:
>> https://en.wikipedia.org/wiki/Set-builder_notation#Parallels_in_programming_languages
>>
>> Example1: Set.new: gather { for L { take $_ } };
>> Example2: Set.new: cross( K, X.grep: P(x) );
>>
>> Cheers!
>>
>> <--- Working code
>> my \L = 1..10; my \K = 1..10; my \X = 5..15;
>>
>> # Example 1
>> my $e1 = Set.new: gather { for L { take $_ } };
>>
>> # Example 2
>> my $s1 = Set.new: gather {
>>for K -> \k {
>>for X -> \x {
>>if x < 8 {
>>take (k,x);
>>}
>>}
>>}
>> }
>> my $s2 = Set.new: (K X[,] X).grep: -> ( \k, \x ) { x < 8 };
>> my $s3 = Set.new: ( -> \x { |(-> \k { (k,x) if x < 8 } for K) } for X );
>> my $s4 = Set.new: gather { -> \k { -> \x { take (k,x) if x < 8; } for X } 
>> for K }
>> my $s5 = Set.new: cross( K, X.grep: * < 8 );
>>
>> say $e1; say $s1; say $s2; say $s3; say $s4; say $s5;
>> <---
>>
>> El Sunday, 10 de February del 2019 a les 12:05, Brad Gilbert va
>> escriure:
>>
>> >In
>> >
>> > {l for l in L}
>> >
>> >The reason it is in `{}` is to create a Set from iterating over `L`.
>> >
>> >> In Python, the set-builder's braces are replaced with square brackets, 
>> >> parentheses, or curly braces, giving list, generator, and set objects, 
>> >> respectively.
>> >
>> >So in Python:
>> >
>> >[ l for l in L ] gives a list
>> >( l for l in L ) gives a generator
>> >{ l for l in L } gives a set
>> >
>> >In Perl6 those would most likely be written as:
>> >
>> >L.List   or   L.Array   or   L.list
>> >L.Seq
>> >L.Set
>> >
>> >---
>> >
>> >The way to do that is
>> >
>> >my \L = ((1..10) xx 3).flat.pick(*).list;
>> >
>> >set( L ) # A
>> >L.Set # B
>> >
>> >my %set is SetHash;
>> >{ ++%set{$_} for L }  # C
>> >
>> ># D
>> >do {
>> ># add the {} syntax to create a Set (lexically)
>> >my sub circumfix:«{ }» ( \L ) { L.Set };
>> >
>> >{ $_ for L } # <--
>> >}
>> >
>> >Something that seems similar to me is `unique`
>> >
>> >.say for L.unique;
>> >
>> >By that I mean, some places where you would use a Set, it makes sense
>> >to use `.unique` instead
>> >
>> >---
>> >
>> >As for `{(k, x) for k in K for x in X if P(x)}`
>> >
>> >The easiest one to directly translate appears to be the Scala one
>> >
>> >my \K = 1..10;
>> >my \X = 5..15;
>> >
>> ># for (k <- K; x <- X if P(x)) yield (k,x)
>> >Set.new: gather {
>> >for K -> \k {
>> >for X -> \x {
>> >if P(x) {
>> >take (k,x);
>> >}
>> >}
>> >}
>> >}
>> >
>> >Other ways:
>> >
>> >Set.new: (K X[,] X).grep: -> ( \k, \x ) { P(x) }
>> >
>> >Set.new: K X[,] X.grep: 
>> >
>> >Set.new: K X[,] X.grep: 
>> >
>> >Set.new: ( -> ( \k, \x ) { (k,x) if P(x) } for K X[,] X )
>> >
>> >Set.new: ( -> \x { |(-> \k { (k,x) if P x } for K) } for X)
>> >
>> >On Sun, Feb 10, 2019 at 10:26 AM mimosinnet  wrote:
>> >>
>> >> Hi all,
>> >>
>> >> I wonder what would be the Perl notation for 'set-builders', as exposed
>> >> in this wikipedia article:
>> >>
>> >> https://en.wikipedia.org/wiki/Set-builder_nota

Re: list comprehension

2019-02-11 Thread Brad Gilbert
Actually I would suggest NOT adding Perl6, because the best way to
create a Set is not to use “list comprehension”, but to just call
`.Set`

That whole page is about Set Builder Notation, but Perl6 doesn't
actually have such a thing.

You create a Set through a method call, or a subroutine call.

That is true even in the translations I did.

The closest one is where I added a circumfix operator.

my sub circumfix:«{ }» ( \L ) { L.Set };

I mean this:

Set.new: gather { for L { take $_ } };

can be simplified to:

Set.new( L );

Or if you're being pedantic:

Set.new( L.Seq );

On Mon, Feb 11, 2019 at 12:51 PM mimosinnet  wrote:
>
> Dear Brad,
>
> Thanks very much for the answer. I have been playing with your examples in the
> code below (and learned a lot!). Based on your insight, I would suggest these
> solutions to be added to the wikipedia:
> https://en.wikipedia.org/wiki/Set-builder_notation#Parallels_in_programming_languages
>
> Example1: Set.new: gather { for L { take $_ } };
> Example2: Set.new: cross( K, X.grep: P(x) );
>
> Cheers!
>
> <--- Working code
> my \L = 1..10; my \K = 1..10; my \X = 5..15;
>
> # Example 1
> my $e1 = Set.new: gather { for L { take $_ } };
>
> # Example 2
> my $s1 = Set.new: gather {
>for K -> \k {
>for X -> \x {
>if x < 8 {
>take (k,x);
>}
>}
>}
> }
> my $s2 = Set.new: (K X[,] X).grep: -> ( \k, \x ) { x < 8 };
> my $s3 = Set.new: ( -> \x { |(-> \k { (k,x) if x < 8 } for K) } for X );
> my $s4 = Set.new: gather { -> \k { -> \x { take (k,x) if x < 8; } for X } for 
> K }
> my $s5 = Set.new: cross( K, X.grep: * < 8 );
>
> say $e1; say $s1; say $s2; say $s3; say $s4; say $s5;
> <---
>
> El Sunday, 10 de February del 2019 a les 12:05, Brad Gilbert va
> escriure:
>
> >In
> >
> > {l for l in L}
> >
> >The reason it is in `{}` is to create a Set from iterating over `L`.
> >
> >> In Python, the set-builder's braces are replaced with square brackets, 
> >> parentheses, or curly braces, giving list, generator, and set objects, 
> >> respectively.
> >
> >So in Python:
> >
> >[ l for l in L ] gives a list
> >( l for l in L ) gives a generator
> >{ l for l in L } gives a set
> >
> >In Perl6 those would most likely be written as:
> >
> >L.List   or   L.Array   or   L.list
> >L.Seq
> >L.Set
> >
> >---
> >
> >The way to do that is
> >
> >my \L = ((1..10) xx 3).flat.pick(*).list;
> >
> >set( L ) # A
> >L.Set # B
> >
> >my %set is SetHash;
> >{ ++%set{$_} for L }  # C
> >
> ># D
> >do {
> ># add the {} syntax to create a Set (lexically)
> >my sub circumfix:«{ }» ( \L ) { L.Set };
> >
> >{ $_ for L } # <--
> >}
> >
> >Something that seems similar to me is `unique`
> >
> >.say for L.unique;
> >
> >By that I mean, some places where you would use a Set, it makes sense
> >to use `.unique` instead
> >
> >---
> >
> >As for `{(k, x) for k in K for x in X if P(x)}`
> >
> >The easiest one to directly translate appears to be the Scala one
> >
> >my \K = 1..10;
> >my \X = 5..15;
> >
> ># for (k <- K; x <- X if P(x)) yield (k,x)
> >Set.new: gather {
> >for K -> \k {
> >for X -> \x {
> >if P(x) {
> >take (k,x);
> >}
> >}
> >}
> >}
> >
> >Other ways:
> >
> >Set.new: (K X[,] X).grep: -> ( \k, \x ) { P(x) }
> >
> >Set.new: K X[,] X.grep: 
> >
> >Set.new: K X[,] X.grep: 
> >
> >Set.new: ( -> ( \k, \x ) { (k,x) if P(x) } for K X[,] X )
> >
> >Set.new: ( -> \x { |(-> \k { (k,x) if P x } for K) } for X)
> >
> >On Sun, Feb 10, 2019 at 10:26 AM mimosinnet  wrote:
> >>
> >> Hi all,
> >>
> >> I wonder what would be the Perl notation for 'set-builders', as exposed
> >> in this wikipedia article:
> >>
> >> https://en.wikipedia.org/wiki/Set-builder_notation#Parallels_in_programming_languages
> >>
> >> This is the Python notation:
> >>
> >> Example 1: {l for l in L}
> >> Example 2: {(k, x) for k in K for x in X if P(x)}
> >>
> >> This is another example in Python:
> >>
> >> s = {v for v

Re: list comprehension

2019-02-11 Thread mimosinnet

Dear Brad,

Thanks very much for the answer. I have been playing with your examples in the
code below (and learned a lot!). Based on your insight, I would suggest these
solutions to be added to the wikipedia:
https://en.wikipedia.org/wiki/Set-builder_notation#Parallels_in_programming_languages

Example1: Set.new: gather { for L { take $_ } };
Example2: Set.new: cross( K, X.grep: P(x) );

Cheers!

<--- Working code
my \L = 1..10; my \K = 1..10; my \X = 5..15;

# Example 1
my $e1 = Set.new: gather { for L { take $_ } };

# Example 2
my $s1 = Set.new: gather {
  for K -> \k {
  for X -> \x {
  if x < 8 {
  take (k,x);
  }
  }
  }
}
my $s2 = Set.new: (K X[,] X).grep: -> ( \k, \x ) { x < 8 };
my $s3 = Set.new: ( -> \x { |(-> \k { (k,x) if x < 8 } for K) } for X );
my $s4 = Set.new: gather { -> \k { -> \x { take (k,x) if x < 8; } for X } for K 
}
my $s5 = Set.new: cross( K, X.grep: * < 8 );

say $e1; say $s1; say $s2; say $s3; say $s4; say $s5;
<---

El Sunday, 10 de February del 2019 a les 12:05, Brad Gilbert va 
escriure:



In

{l for l in L}

The reason it is in `{}` is to create a Set from iterating over `L`.


In Python, the set-builder's braces are replaced with square brackets, 
parentheses, or curly braces, giving list, generator, and set objects, 
respectively.


So in Python:

   [ l for l in L ] gives a list
   ( l for l in L ) gives a generator
   { l for l in L } gives a set

In Perl6 those would most likely be written as:

   L.List   or   L.Array   or   L.list
   L.Seq
   L.Set

---

The way to do that is

   my \L = ((1..10) xx 3).flat.pick(*).list;

   set( L ) # A
   L.Set # B

   my %set is SetHash;
   { ++%set{$_} for L }  # C

   # D
   do {
   # add the {} syntax to create a Set (lexically)
   my sub circumfix:«{ }» ( \L ) { L.Set };

   { $_ for L } # <--
   }

Something that seems similar to me is `unique`

   .say for L.unique;

By that I mean, some places where you would use a Set, it makes sense
to use `.unique` instead

---

As for `{(k, x) for k in K for x in X if P(x)}`

The easiest one to directly translate appears to be the Scala one

   my \K = 1..10;
   my \X = 5..15;

   # for (k <- K; x <- X if P(x)) yield (k,x)
   Set.new: gather {
   for K -> \k {
   for X -> \x {
   if P(x) {
   take (k,x);
   }
   }
   }
   }

Other ways:

   Set.new: (K X[,] X).grep: -> ( \k, \x ) { P(x) }

   Set.new: K X[,] X.grep: 

   Set.new: K X[,] X.grep: 

   Set.new: ( -> ( \k, \x ) { (k,x) if P(x) } for K X[,] X )

   Set.new: ( -> \x { |(-> \k { (k,x) if P x } for K) } for X)

On Sun, Feb 10, 2019 at 10:26 AM mimosinnet  wrote:


Hi all,

I wonder what would be the Perl notation for 'set-builders', as exposed
in this wikipedia article:

https://en.wikipedia.org/wiki/Set-builder_notation#Parallels_in_programming_languages

This is the Python notation:

Example 1: {l for l in L}
Example 2: {(k, x) for k in K for x in X if P(x)}

This is another example in Python:

s = {v for v in 'ABCDABCD' if v not in 'CB'}

https://en.wikipedia.org/wiki/List_comprehension#Similar_constructs

I have been playing with the code below. Nevertheless, I am unsure on
how to use the code to define a set.

Cheers!

<--- Code
#!/usr/bin/env perl6

my @L = 1 .. 10;
my @K = 1 .. 10;
my @X = 5 .. 15;

say "Example 1:";
for @L -> $l {
  print "$l " if $l ∈ @L;
}

say "\nExample 2:";
for @K -> $k { for @X -> $x {
print "($k, $x), " if ($k ∈ @K and $x ∈ @X and $x < 8);
}}
<---

--
(≧∇≦) Mimosinnet (Linux User: #463211)


--
(≧∇≦) Mimosinnet (Linux User: #463211)

(≧∇≦) Ningún Lugar

★ Activisme Cultural per a la Transformació Social

(≧∇≦) Fractalitats en Investigació Crítica

* Investigació Crítica per a la Transformació Social
* http://psicologiasocial.uab.es/fic


Re: list comprehension

2019-02-10 Thread Brad Gilbert
In

 {l for l in L}

The reason it is in `{}` is to create a Set from iterating over `L`.

> In Python, the set-builder's braces are replaced with square brackets, 
> parentheses, or curly braces, giving list, generator, and set objects, 
> respectively.

So in Python:

[ l for l in L ] gives a list
( l for l in L ) gives a generator
{ l for l in L } gives a set

In Perl6 those would most likely be written as:

L.List   or   L.Array   or   L.list
L.Seq
L.Set

---

The way to do that is

my \L = ((1..10) xx 3).flat.pick(*).list;

set( L ) # A
L.Set # B

my %set is SetHash;
{ ++%set{$_} for L }  # C

# D
do {
# add the {} syntax to create a Set (lexically)
my sub circumfix:«{ }» ( \L ) { L.Set };

{ $_ for L } # <--
}

Something that seems similar to me is `unique`

.say for L.unique;

By that I mean, some places where you would use a Set, it makes sense
to use `.unique` instead

---

As for `{(k, x) for k in K for x in X if P(x)}`

The easiest one to directly translate appears to be the Scala one

my \K = 1..10;
my \X = 5..15;

# for (k <- K; x <- X if P(x)) yield (k,x)
Set.new: gather {
for K -> \k {
for X -> \x {
if P(x) {
take (k,x);
}
}
}
}

Other ways:

Set.new: (K X[,] X).grep: -> ( \k, \x ) { P(x) }

Set.new: K X[,] X.grep: 

Set.new: K X[,] X.grep: 

Set.new: ( -> ( \k, \x ) { (k,x) if P(x) } for K X[,] X )

Set.new: ( -> \x { |(-> \k { (k,x) if P x } for K) } for X)

On Sun, Feb 10, 2019 at 10:26 AM mimosinnet  wrote:
>
> Hi all,
>
> I wonder what would be the Perl notation for 'set-builders', as exposed
> in this wikipedia article:
>
> https://en.wikipedia.org/wiki/Set-builder_notation#Parallels_in_programming_languages
>
> This is the Python notation:
>
> Example 1: {l for l in L}
> Example 2: {(k, x) for k in K for x in X if P(x)}
>
> This is another example in Python:
>
> s = {v for v in 'ABCDABCD' if v not in 'CB'}
>
> https://en.wikipedia.org/wiki/List_comprehension#Similar_constructs
>
> I have been playing with the code below. Nevertheless, I am unsure on
> how to use the code to define a set.
>
> Cheers!
>
> <--- Code
> #!/usr/bin/env perl6
>
> my @L = 1 .. 10;
> my @K = 1 .. 10;
> my @X = 5 .. 15;
>
> say "Example 1:";
> for @L -> $l {
>   print "$l " if $l ∈ @L;
> }
>
> say "\nExample 2:";
> for @K -> $k { for @X -> $x {
> print "($k, $x), " if ($k ∈ @K and $x ∈ @X and $x < 8);
> }}
> <---
>
> --
> (≧∇≦) Mimosinnet (Linux User: #463211)


list comprehension

2019-02-10 Thread mimosinnet

Hi all,

I wonder what would be the Perl notation for 'set-builders', as exposed 
in this wikipedia article: 


https://en.wikipedia.org/wiki/Set-builder_notation#Parallels_in_programming_languages

This is the Python notation:

Example 1: {l for l in L}
Example 2: {(k, x) for k in K for x in X if P(x)}

This is another example in Python:

s = {v for v in 'ABCDABCD' if v not in 'CB'}

https://en.wikipedia.org/wiki/List_comprehension#Similar_constructs

I have been playing with the code below. Nevertheless, I am unsure on 
how to use the code to define a set.  

Cheers! 


<--- Code
#!/usr/bin/env perl6

my @L = 1 .. 10;
my @K = 1 .. 10;
my @X = 5 .. 15; 

say "Example 1:"; 
for @L -> $l { 
 print "$l " if $l ∈ @L;

}

say "\nExample 2:"; 
for @K -> $k { for @X -> $x {

   print "($k, $x), " if ($k ∈ @K and $x ∈ @X and $x < 8);
}}
<---

--
(≧∇≦) Mimosinnet (Linux User: #463211)