The boss loudly said: Find the oldest guy who has been at this company for the longest time and is still not making six figures. We'll fire him!
Impeccable management logic here: The guy has been with the company for the longest time. The guy is the oldest. He is still not making six figures. Obviously a sure loser. Fire this loser and save money!
Three programmers Tom, Dick & Harry overheard their boss and got to work immediately.
The employee database obviously looks like this:
So we see there are 10,000 employees at this company.
They are between 25 & 35 years old.
They have been with the company anywhere from 1 to 6 years.
They make between $85,000 to $150,000.
Tom was a PHP/JS programmer. He worked on rapidly prototyped webapps and didn't give a rat's ass about efficiency.
Tom reasoned: This is just a nested sort. Let me first sort on the tenure. That will tell me who has been with the company for the longest time. Then I'll sort on age. That tells me who the oldest bloke is. Then I'll sort on salary. That tells me who is making the least money. That should be it!
In Scala, Tom's code would look like:
Dick was a backend systems programmer. He thought deep and hard about algorithms & Big O performance and such.
So Dick said  there's no need to sort anything. We just want the oldest guy with the longest tenure who makes the least salary. That just an O(n) problem, NOT an O(nlog(n)) problem. We just need the min tuple.
In Scala, Dick's code looks like this 
Harry was a math major who went about looking for mathematical abstractions in the simplest of code.
Harry took the day off and thought long & hard about this problem while lounging in the bathtub.
Suddenly, it occured to Harry  Hey, this is a monoid!
Naked Harry happily danced the mathematician's Eureka Dance, and set about coding.
In Scala, Harry's code looks like:
In scalaz, it would look like this:
The next day, Tom, Dick & Harry met with the boss.
They happily announced  "We've found the unlucky bastard! He is employee number 7435. He has been with the company for 6 years. He is 35 years old. He makes $ 85,079. Fire him and we'll be profitable again!"
The boss was very impressed. But he wanted to know how they came up with this solution.
Tom said  This is just a nested sorting problem.
Dick said  There is no need to sort at all! Its just a mintuple problem. Takes O(n) to find the right answer.
Harry said  You are both trivially right. The most important insight is that this is a monoid!
"But what is a monoid ?" the boss asked.
"Well, you have a set with an associative plus and an identity. That's a monoid!", announced Harry.
Nobody understood anything, so Harry said  "Think of the employee database as a set of employees. I claim two employees can be added!"
"What do you mean ? How do you add an employee to another employee ?" they asked.
"Well", Harry said, "If you are asked to add two employees, return the guy who is older. But if both of them are the same age, then return the guy who has been with the company the longest. But if both guys have been working for us for the same number of years, then return the guy with the lowest salary. But if they both earn the same as well, then just randomly pick one over the other"
"And how does all that work ?", they asked.
"Well, lets define an identity employee. If you add something to this identity, you get back the thing you added. So we create a dummy employee who has been with us for 0 years, so that everybody else has been here longer than the identity. Then the addition works out".
"OK. I still don't see how all this works", said the boss.
"Well, if we seed a catamorphism with the identity employee and aggregate over the set, we collapse to the unlucky employee 7435, who has been here the longest, is the oldest & makes the least money", Harry triumphantly announced.
Everybody stared at Harry, absolutely speechless.
Long story short: Harry was immediately fired. Dick was made the CTO and Tom was appointed Dick's secretary.
Bonus Problem: What happened to employee number 7435 ?
Solution at the very end of this post.



























Solution: Employee number 7435 was the boss! You can't fire the boss!! He took the least amount of salary home for tax avoidance purposes. The bulk of his earnings came from equity.