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 <mimosin...@gmail.com> 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: &P > > > > Set.new: K X[,] X.grep: &P > > > > 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 <mimosin...@gmail.com> 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 >