Special Cases Are a Code Smell
A straightforward approach might look like this:
Notice how we treat the endpoints as special cases, complicating the simple rule “sum both neighbors of every element.” Our attempt fails because it special cases the even elements, wrapping only those in an array. Let’s take a full week schedule, and a merchant with both edge cases:
The crucial observation – the rule with no exceptions – is this: the time intervals we display on a given day are exactly those whose left endpoint is contained in that day. We’ve translated a problem that would require special cases – treating each day’s hours, and the overlaps between them, individually – to one in which all opening and closing times in a week are treated uniformly.
Source: blog.conjur.org