Last Friday of each month: Difference between revisions

→‎JS ES6: Updated primitives, tidied
m (Corrected header for JavaScript)
(→‎JS ES6: Updated primitives, tidied)
Line 1,812:
 
===ES6===
 
<lang JavaScript>(() => {
'"use strict'";
 
// ------------ LAST FRIDAY OF EACH MONTH ------------
 
// lastWeekDaysOfYear :: Int -> Int -> [Date]
const lastWeekDaysOfYear = (iWeekDay, y) => [
y => 31,{
0const ===isLeap y % 4 && 0 !== y % 100 || 0n === y % 400 ? 29 :> 28,(
31, 30, 31, 30, 31,(0 31,=== 30,n 31,% 30,4) 31&& (0 !== n % 100)) || (
] 0 === y % 400
.map((d, m) => );
 
new Date(Date.UTC(
return y, m, d - ((new Date(Date.UTC(y, m, d))[
.getDay31, isLeap(y) +? (729 -: iWeekDay)) % 7))));28,
31, 30, 31, 30, 31, 31, 30, 31, 30, 31
}, (_, i) => m + i); ]
lst[0] .map((_d, iColm) =>
new Date(Date.UTC(
y, m, d - ((
new Date(Date.UTC(
y, m, d
.join('\t' ))
.getDay() + (7 - iWeekDay)
.map(isoDateString ) % 7)
range(2015, 2019 ))
);
.join('\n')};
 
 
const days = {
Line 1,837 ⟶ 1,852:
};
 
// ---------------------- TEST -----------------------
// GENERIC FUNCTIONS
const rangemain = (m, n) =>
Array.fromtranspose({
enumFromTo(2015)(2019)
.map(curry(lastWeekDaysOfYear)(days.friday))
)
.map(row => row
row => row.map(isoDateString).join("\t")
)
.join("\n");
 
 
// ---------------- GENERIC FUNCTIONS ----------------
 
// rangeenumFromTo :: Int -> Int -> [Int]
const enumFromTo = m =>
n => Array.from({
length: Math.floor(1 + n - m) + 1
}, (_, i) => m + i);
 
// curry :: ((a, b) -> c) -> a -> b -> c
const curry = f => a => b => f(a, b);
 
// isoDateString :: Date -> String
Line 1,847 ⟶ 1,878:
.substr(0, 10);
 
// range :: Int -> Int -> [Int]
const range = (m, n) =>
Array.from({
length: Math.floor(n - m) + 1
}, (_, i) => m + i);
 
// transpose :: [[a]] -> [[a]]
const transpose = lstrows =>
// The columns of the input transposed
lst[0].map((_, iCol) =>
// into new lstrows.map(row => row[iCol]));
0 < rows.length ? rows[0].map(
(x, i) => rows.flatMap(
v => v[i]
)
) : [];
 
// TESTMAIN ---
return transposemain();
range(2015, 2019)
.map(curry(lastWeekDaysOfYear)(days.friday))
)
.map(row => row
.map(isoDateString)
.join('\t'))
.join('\n');
})();</lang>
{{Out}}
9,655

edits