What Programmers Value

Imagine you’re using JavaScript in a technical interview and the interviewer asks you to implement fold. Acceptable response vary depending on the interviewer’s programming values. Let’s look at solutions that will most resonate with a few different personas:

The Pragmatist:

Values getting the job done economically

function fold(arr, alg, init) {
  return arr.reduce(alg, init)
}

Pros

Cons

The Mathematician:

Values academic correctness

function foldr(alg, acc, xs) {
  if (xs.length === 0) return acc
  return alg(xs.at(0), foldr(alg, acc, xs.slice(1)))
}

function foldl(alg, acc, xs) {
  if (xs.length === 0) return acc
  return foldl(alg, alg(xs.at(0), acc), xs.slice(1))
}

Pros

Cons

The Optimizer:

Values performance

function fold(array, callback, init) {
  let acc = init
  for (let i = 0; i < array.length; i++) {
    acc = callback(acc, array[i])
  }
  return acc
}

Pros

Cons

All 3 implementations have the potential to annoy 2 of the 3 interviewer personas and not because they’re not correct, but because they don’t align with what those interviewers think is important.