Light at the End of the Tunnel
The light at the end of the tunnel
is the headlight of an oncoming train.

The light at the end of the tunnel
is the headlight of an oncoming train.
I briefly covered a mapping function in "The Functional Shape of Loops" which can be used for one-to-one mapping, meaning that one item in the input collection produces one value in the output collection:
[1, 2, 3] |> map((n) => n * 2); // [2, 4, 6]
Sometimes it's useful to have a one-to-many mapping, where one input produces multiple output values.
I was writing some code in a template that looked a lot like:
if (collection.HasItems)
{
foreach (var item in collection)
{
<some markup>
}
}
else
{
<some message about no items>
}
and I realized that it would be awfully nice for for, foreach, and while loops to have an else clause.
Learning to program, it's quite common to come across many resources and code samples that include imperative loops of various sorts (particularly for and while).
Frequently when I see examples that include loops, I cringe. They often include many steps that make them harder to understand because there is so much happening every iteration.
Instead, I much prefer to use a functional approach to iteration. Each step can be converted to an individual function and those functions can be applied to the entire collection to produce a result.
There are several advantages to the functional approach over imperative:
Good judgement comes from experience.
Experience comes from bad judgement.
Don't cling to a mistake just because you spent a lot of time making it.
This is a follow-up to my "Multisort via Composition" post, and won't make sense if you haven't read that post.
When sorting on a single dimension, changing the sort direction can usually be handled by changing the sorter function directly.
const data = [33, 1, 95, 77, 54, 91, 38, 89, 48, 24];
// ascending
data.sort((a, b) => a - b);
// descending
data.sort((a, b) => b - a);
making this act dynamically based on some external input can be reasonably handled by updating the function as well:
const ascending = getAscending(); // true or false
data.sort((a, b) => (ascending ? a - b : b - a));
This works fine for a single sorter, but quickly becomes untenable once multiple sorting dimensions are used:
Sorting a collection of data in a single dimension is pretty straightforward in most programming languages. It's quite common to use some sort of comparison interface that exposes a function with two parameters of the type being sorted, and expected return values of -1, 0, or 1.
For example, JavaScript has Array.prototype.sort, C# has IComparer, and PHP has usort.
This works quite well when you want to sort on a single property:
Sometimes your knight in shining armor is just an idiot wrapped in tinfoil.
I recently saw Amber Weinberg's tweet after it blew up on twitter:
I refuse to use Sass
— Amber Weinberg (@amberweinberg) December 1, 2017
I refuse to use React
I refuse to use Redux
I’m still here and still have plenty of clients who appreciate clear, clean code.
My gut reaction was that it was a bit harsh to those of us who choose to use Sass, React, or Redux. It implies that anyone who uses those tools isn't writing clear, or clean code, which explains why so many front-end developers collectively flipped thier shit at it.
But seeing as Amber's a WordPress developer, it entirely makes sense to me that she would have no use for React and Redux. I can even see why she'd avoid Sass too*. The types of problems best solved by a WordPress site do not necessitate any use of React or Redux.