develooper Front page | perl.perl6.users | Postings from February 2019

list comprehension

Thread Next
From:
mimosinnet
Date:
February 10, 2019 16:26
Subject:
list comprehension
Message ID:
20190210162630.GC32715@mimomedia
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)

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About