Last Friday of each month: Difference between revisions

Content added Content deleted
m (Corrected header for JavaScript)
(→‎JS ES6: Updated primitives, tidied)
Line 1,812: Line 1,812:


===ES6===
===ES6===

<lang JavaScript>(() => {
<lang JavaScript>(() => {
'use strict'
"use strict";

// ------------ LAST FRIDAY OF EACH MONTH ------------


// lastWeekDaysOfYear :: Int -> Int -> [Date]
// lastWeekDaysOfYear :: Int -> Int -> [Date]
const lastWeekDaysOfYear = (iWeekDay, y) => [
const lastWeekDaysOfYear = iWeekDay =>
31,
y => {
0 === y % 4 && 0 !== y % 100 || 0 === y % 400 ? 29 : 28,
const isLeap = n => (
31, 30, 31, 30, 31, 31, 30, 31, 30, 31
(0 === n % 4) && (0 !== n % 100)) || (
]
0 === y % 400
.map((d, m) =>
);

new Date(Date.UTC(
y, m, d - ((new Date(Date.UTC(y, m, d))
return [
.getDay() + (7 - iWeekDay)) % 7))));
31, isLeap(y) ? 29 : 28,
31, 30, 31, 30, 31, 31, 30, 31, 30, 31
]
.map((d, m) =>
new Date(Date.UTC(
y, m, d - ((
new Date(Date.UTC(
y, m, d
))
.getDay() + (7 - iWeekDay)
) % 7)
))
);
};



const days = {
const days = {
Line 1,837: Line 1,852:
};
};


// ---------------------- TEST -----------------------
// GENERIC FUNCTIONS
const main = () =>
transpose(
enumFromTo(2015)(2019)
.map(lastWeekDaysOfYear(days.friday))
)
.map(
row => row.map(isoDateString).join("\t")
)
.join("\n");


// ---------------- GENERIC FUNCTIONS ----------------

// enumFromTo :: Int -> Int -> [Int]
const enumFromTo = m =>
n => Array.from({
length: 1 + n - m
}, (_, i) => m + i);


// curry :: ((a, b) -> c) -> a -> b -> c
const curry = f => a => b => f(a, b);


// isoDateString :: Date -> String
// isoDateString :: Date -> String
Line 1,847: Line 1,878:
.substr(0, 10);
.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]]
// transpose :: [[a]] -> [[a]]
const transpose = lst =>
const transpose = rows =>
// The columns of the input transposed
lst[0].map((_, iCol) =>
lst.map(row => row[iCol]));
// into new rows.
0 < rows.length ? rows[0].map(
(x, i) => rows.flatMap(
v => v[i]
)
) : [];


// TEST
// MAIN ---
return transpose(
return main();
range(2015, 2019)
.map(curry(lastWeekDaysOfYear)(days.friday))
)
.map(row => row
.map(isoDateString)
.join('\t'))
.join('\n');
})();</lang>
})();</lang>
{{Out}}
{{Out}}