flat a lazy gather ?

hello people,

AÀM (Appel À Mousser) is the monthly social event of the Strasbourg(fr)
LUG. the dates of those meetings are scheduled by an algorithm
implemented in aam_dates_of_year (line 6).

Now i would like to show 25 AAM dates staring from 2019-01. line 14
works fine but is too long. i really would have those dates be read
as a simple list.

i wrote the line 18 (so i tought ) but when i want to get the 2 next
dates, i have the dates for the 2 next years. it seems .flat is ignored
and i don't know why.

any help on it ?

regards,
marc

   1	sub date_for (Date $d is copy, Int \dow) {
   2	  $d.=later(:1day) while $d.day-of-week != dow;
   3	  $d.later(:1week);
   4	}
   5	
   6	sub aam_dates_of_year ($year) {
   7	   ( for 1..12 { Date.new(:$year, :$^month, :1day) } )
   8	     Z[&date_for] | (2..4) xx *
   9	}
  10	
  11	# works:
  12	# Date.today.year.&aam_dates_of_year[7].say;
  13	# (2019..2025)>>.&aam_dates_of_year.flat>>.say;
  14	# ( lazy gather for 2019..∞ { take .&aam_dates_of_year }).flat.head(2)>>.say;
  15	
  16	# more reusable ?
  17	
  18	my @future_aams =
  19	  lazy gather for 2019..∞ { take .&aam_dates_of_year };
  20	
  21	@future_aams.flat.head(1)>>.say;
0
eiro
8/9/2019 2:34:52 PM
perl.perl6.users 1202 articles. 0 followers. Follow

2 Replies
11 Views

Similar Articles

[PageSpeed] 28


> On Aug 9, 2019, at 9:34 AM, Marc Chantreux <eiro@phear.org> wrote:
>=20
> hello people,

Hi Marc!

>=20
> A=C3=80M (Appel =C3=80 Mousser) is the monthly social event of the =
Strasbourg(fr)
> LUG. the dates of those meetings are scheduled by an algorithm
> implemented in aam_dates_of_year (line 6).
>=20
> Now i would like to show 25 AAM dates staring from 2019-01. line 14
> works fine but is too long. i really would have those dates be read
> as a simple list.
>=20
> i wrote the line 18 (so i tought ) but when i want to get the 2 next
> dates, i have the dates for the 2 next years. it seems .flat is =
ignored
> and i don't know why.
>=20
> any help on it ?
=E2=80=94snip=E2=80=94
>=20
>  18	my @future_aams =3D
>  19	  lazy gather for 2019..=E2=88=9E { take .&aam_dates_of_year };
>  20=09
>  21	@future_aams.flat.head(1)>>.say;

Short answer: Add `flat` before `lazy in line 19, and remove `.flat` =
from line 21. Like so:
    my @future_aams =3D
        flat lazy gather for 2019..=E2=88=9E { take .&aam_dates_of_year =
};

    @future_aams.head(1)>>.say;

Medium answer: In refactoring line 14, you moved from .flat on a List to =
..flat on an Array.
List and Array do not treat `.flat` the same way. E.g.:
$ perl6 -e 'say (<a b>, <c>).flat.elems'
3
$ perl6 -e 'say [<a b>, <c>].flat.elems'
2

Full answer coming when time allows. For such a small fix, it was =
surprisingly effortful to figure out.
See https://docs.perl6.org/language/list#Itemization

=E2=80=94=20
Hope this helps,
Bruce Gray (Util of PerlMonks)=
0
robertbrucegray3
8/9/2019 10:06:11 PM
hello Bruce,

> Short answer: Add `flat` before `lazy in line 19, and remove `.flat` from line 21. Like so:
>     my @future_aams =
>         flat lazy gather for 2019..∞ { take .&aam_dates_of_year };

sure but i loose the ability to use @future_aams year by year.

the best i i have here to keep both behaviors available is:

  sub flat_stream { lazy gather for @^list -> @l { take $_ for @l } }
  @future_aams.&flat_stream.head(1)>>.say;

> Full answer coming when time allows. For such a small fix, it was surprisingly effortful to figure out.
> See https://docs.perl6.org/language/list#Itemization

i'll read again and try to find by myself.
thanks

marc
0
eiro
8/10/2019 7:10:05 AM
Reply: