Patrick2788
I have used your challenge as a further exercise. I note that the formula that Sergei introduced to restructure the 3D Range, that contains the calendar, is recalculated in its entirety once for each staff member. Provided the calculation is fast that is not a problem, but out of interest I set out to limit the calculation step to a single occurrence. That meant I could not use a defined Name or a Thunk to pass the restructured array 'stack'.
I could create a monolithic block of code and place the calculation of 'stack' before the MAP that determines the eligible hours for each staff member. To move away from such monolithic code, I wanted to use a further nested Lambda function. This could either be defined within the outer Lambda function by using LET, in which case it would be within scope of the formula-local name, or 'stack' itself could be passed as a parameter. I implemented each, but chose the second option.
GrossPayλ
= LAMBDA(calendar, names, rate,
LET(
stack, SORT(WRAPROWS(TOCOL(calendar, , 1), 3)),
rate * MAP(staff, EquivalentHoursλ(stack))
)
);
EquivalentHoursλ
= LAMBDA(stack,
LAMBDA(name,
LET(
dates, TAKE(stack, , 1),
holiday?, COUNTIFS(holidays, dates),
weekend?, WEEKDAY(dates, 2) > 5,
time, 1 + 0.5 * weekend? + holiday?,
assigned, TAKE(stack, , -2),
onshift?, BYROW(assigned, LAMBDA(s, OR(s = name))),
SUM(IF(onshift?, 8 * time))
)
)
);
In order to pass the additional parameter into the MAP construct I needed to define the function 'EquivalentHoursλ' in a Curried form, passing one parameter at a time by using multiple nested LAMBDAs.
I am sorry if this reads like complete gobbledegook; I have tried my best to express the ideas clearly, but I am not convinced I have succeeded!