Functional Programming and Higher Order Abstraction

Trying to wrap my brain around stacked abstractions is making me feel dumb. Seems like maybe making through chapter 6 in Eloquent JavaScript should be easier.

The following is what I would describe as a five level stack we were supposed to come up with as an exercise. Huh? Reminds me of chess.

function forEach(array, action) {
  for (var i = 0; i < array.length; i++)
    action(array[i]);
}

function reduce(combine, base, array) {
forEach(array, function (element) {
base = combine(base, element);
});
return base;
}

function count(test, array) {
return reduce(function(total, element) {
return total + (test(element) ? 1 : 0);
}, 0, array);
}

function equals(x) {
return function(element) {return x === element;};
}

function countZeroes(array) {
return count(equals(6), array);
}

print(countZeroes([0,0,0,0,0,0,1,2,3,4,5,6]));
 

Basically what you’re doing is bringing an environment to each function (variables, arrays, other functions), and I made a little Concept Map using the IHMC’s CMAP Tools to try and visualize better WTF exactly is going on here:

Eloquent Functional Programming code to count the number of zeroes in an array.
Eloquent Functional Programming to count the number of zeroes in an array.

Maybe it’ll help you. Maybe it’ll help me. Maybe you’ll look it an offer some further insight.

As the book states:

“The program source is Data. Control arises from it. The Control proceeds to create new Data. The one is born from the other, the other is useless without the one. This is the harmonious cycle of Data and Control.”

Partial applications (at least in JavaScript) were also confusing.



function asArray(quasiArray, start) {
  var result = [];
  for (var i = (start || 0); i < quasiArray.length; i++)
    result.push(quasiArray[i]);
  return result;
}

function partial(func) {
  var fixedArgs = asArray(arguments, 1);
  return function(){
    return func.apply(null, fixedArgs.concat(asArray(arguments)));
  };
}

What does he mean by quasiArray, and why does he start at the second argument by asArray(arguments, 1) cutting off the "0" element of the damn "quasiArray"? And why don't the arguments need to be received by the partial function?

How when a function is sent to another function, do it's arguments seem to magically be with it?

Came across another insightful tutorial by someone named Ban Alman. Had forgotten that in JavaScript a function is an object, and when it is called, a property called arguments is created, which is array-like, but not exactly an array. So when we call

partial(the_function, arg)

and partial receives

function partial(the_function)

the arguments sent are included in the arguments property of the_function. Sweet.

But why is the first element of the properties quasiArray being ignored? Because the first argument in the arguments array is the function itself. In the EloquentJavascript console this can be seen by simply "show"ing the arguments property:

function partial(func) {
  show(arguments);
  var fixedArgs = asArray(arguments, 1);
  return function(){
    return func.apply(null, fixedArgs.concat(asArray(arguments)));
  };
}

How exciting. This is starting to make sense.