Last Friday of each month: Difference between revisions

Content added Content deleted
m (use day_of_week())
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 177: Line 177:
2012-12-28</pre>
2012-12-28</pre>


=={{Header|AppleScript}}==
=={{header|AppleScript}}==


{{Trans|JavaScript}}
{{Trans|JavaScript}}
Line 506: Line 506:
2012-11-30
2012-11-30
2012-12-28</pre>
2012-12-28</pre>



=={{header|AutoHotkey}}==
=={{header|AutoHotkey}}==
Line 1,046: Line 1,045:
2012-12-28
2012-12-28
</pre>
</pre>

=={{header|Elm}}==
=={{header|Elm}}==
<lang elm>import Html exposing (Html, Attribute, text, div, input)
<lang elm>import Html exposing (Html, Attribute, text, div, input)
Line 2,018: Line 2,018:
2012-11-30
2012-11-30
2012-12-28</pre>
2012-12-28</pre>



=={{header|LiveCode}}==
=={{header|LiveCode}}==
Line 2,189: Line 2,188:
2012-11-30
2012-11-30
2012-12-28</pre>
2012-12-28</pre>



=={{header|Mathematica}}==
=={{header|Mathematica}}==
Line 2,543: Line 2,541:


ocaml unix.cma str.cma -I +calendar calendarLib.cma last_fridays.ml 2012
ocaml unix.cma str.cma -I +calendar calendarLib.cma last_fridays.ml 2012



=={{header|Oforth}}==
=={{header|Oforth}}==
Line 2,644: Line 2,641:
2012-12-28
2012-12-28
</pre>
</pre>

=={{header|Perl 6}}==
<lang perl6>sub MAIN (Int $year = Date.today.year) {
my @fri;
for Date.new("$year-01-01") .. Date.new("$year-12-31") {
@fri[.month] = .Str if .day-of-week == 5;
}
.say for @fri[1..12];
}</lang>

Example:
<pre>$ ./lastfri 2038
2038-01-29
2038-02-26
2038-03-26
2038-04-30
2038-05-28
2038-06-25
2038-07-30
2038-08-27
2038-09-24
2038-10-29
2038-11-26
2038-12-31</pre>

A solution without a result array to store things in:

<lang perl6>sub MAIN (Int $year = Date.today.year) {
say ~.value.reverse.first: *.day-of-week == 5
for classify *.month, Date.new("$year-01-01") .. Date.new("$year-12-31");
}</lang>

Here, <code>classify</code> sorts the dates into one bin per month (but preserves the order in each bin). We then take the list inside each bin (<code>.value</code>) and find the last (<code>.reverse.first</code>) date which is a Friday.

Another variation where the data flow can be read left to right using feed operators:

<lang perl6>sub MAIN (Int $year = Date.today.year) {
.say for Date.new("$year-01-01") .. Date.new("$year-12-31") ==> classify *.month ==>
map *.value.reverse.first: *.day-of-week == 5
}</lang>


=={{header|Phix}}==
=={{header|Phix}}==
Line 3,239: Line 3,196:
Fri 28 Dec 2012
Fri 28 Dec 2012
</pre>
</pre>

=={{header|Raku}}==
(formerly Perl 6)
<lang perl6>sub MAIN (Int $year = Date.today.year) {
my @fri;
for Date.new("$year-01-01") .. Date.new("$year-12-31") {
@fri[.month] = .Str if .day-of-week == 5;
}
.say for @fri[1..12];
}</lang>

Example:
<pre>$ ./lastfri 2038
2038-01-29
2038-02-26
2038-03-26
2038-04-30
2038-05-28
2038-06-25
2038-07-30
2038-08-27
2038-09-24
2038-10-29
2038-11-26
2038-12-31</pre>

A solution without a result array to store things in:

<lang perl6>sub MAIN (Int $year = Date.today.year) {
say ~.value.reverse.first: *.day-of-week == 5
for classify *.month, Date.new("$year-01-01") .. Date.new("$year-12-31");
}</lang>

Here, <code>classify</code> sorts the dates into one bin per month (but preserves the order in each bin). We then take the list inside each bin (<code>.value</code>) and find the last (<code>.reverse.first</code>) date which is a Friday.

Another variation where the data flow can be read left to right using feed operators:

<lang perl6>sub MAIN (Int $year = Date.today.year) {
.say for Date.new("$year-01-01") .. Date.new("$year-12-31") ==> classify *.month ==>
map *.value.reverse.first: *.day-of-week == 5
}</lang>


=={{header|REBOL}}==
=={{header|REBOL}}==