Tuply singleton

I noticed this while at a company team meetup. Whenever we were discussing which place we should eat food at, most of my coworkers were paying attention to two numbers: rating and number of votes.

For example, Restaurant #1 can have a 5.0-star rating with 1 vote, and Restaurant #2 can have a 3.9-star rating with 17 votes.

This metric makes a tuple, where the first element is the rating and the second element is the vote count. It is represented by, e.g. (3.9, 17) in Haskell (and is of type (Double, Int)), or [3.9, 17] (or { fst: 3.9, snd: 17 }) in JavaScript.

So we need two elements (or numbers) to capture this metric. What is a good way to combine them in a single element, such that both can be extracted at any time?

Continue reading “Tuply singleton”

Meet them all

Here’s a funny algorithm task I was thinking about during lunch at Automattic Grand Meetup.

Given infinite tables and 900 people – they have lunch, then dinner, then lunch, etc. repeatedly. They can’t have lunch after lunch, or dinner after dinner. A lunch table consists of 8 persons and a dinner table consists of 4.

What is the least number (or a good lower bound) of lunches and dinners they need to have for every person to have met all other persons?

Continue reading “Meet them all”

Abstraction and generalization of objects

Abstraction is the process of removing details of objects. We step back from concrete objects to consider a number of objects with identical properties. So a concrete object can be looked at as a “superset” of a more abstract object.

A generalization, then, is the formulation of general concepts from specific instances by abstracting common properties. A concrete object can be looked at as a “subset” of a more generalized object.

Continue reading “Abstraction and generalization of objects”

Arithmetic on Algebraic Data Types

Per Wikipedia, an algebraic data type is a kind of composite type, i.e., a type formed by combining other types.

Haskell’s algebraic data types are named such since they correspond to an initial algebra in category theory, giving us some laws, some operations and some symbols to manipulate. — Don Stewart

The operations being:

  • Sum type = + (data Either a b = Left a | Right b)
  • Product type = * (data Product a b = Product a b)
  • Function type = ^ (The -> kind in Haskell)

Continue reading “Arithmetic on Algebraic Data Types”