Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where did the argument go in this example?

I am currently reading the Mostly Adequate Guide on functional programming, chapter 2.

There, the following example is given

var getServerStuff = function(callback) {
  return ajaxCall(function(json) {
    return callback(json);
  });
};

which is then refactored into:

var getServerStuff = ajaxCall;

While explaining the refactoring, the author argues that

return ajaxCall(function(json) {
  return callback(json);
});

is the same as

return ajaxCall(callback);

While I understand that ajaxCall is called with the return value of the anonymous function (which is just the return value of callback), I don't get how the refactored version is supposed to work - where did the json argument go? What am I missing?

like image 398
Sven Avatar asked Jul 17 '16 21:07

Sven


People also ask

What is an argument from example?

An argument by example (also known as argument from example) is an argument in which a claim is supported by providing examples. Most conclusions drawn in surveys and carefully controlled experiments are arguments by example and generalization.

Where is the argument in an essay?

In academic writing, an argument is usually a main idea, often called a “claim” or “thesis statement,” backed up with evidence that supports the idea.

How do you identify an argument example?

There are three steps to argument identification: Understand the Context: Is someone trying to convince you of something? Identify the Conclusion: What are they trying to convince you? Identify the Reasons: Why do they think you should believe them?

Where is the argument in an article?

In academic sources, argument summaries can be found in the abstract, as well as in the introduction and conclusion or discussion. The argument summary is expressed as the main point, conclusion or take-home message for the reader. In non-academic sources, look for the summary at the beginning or the conclusion.


1 Answers

The question has been answered but I think some bolding and strikethroughs make it very easy to see the code conversions. Hopefully this answer helps anyone that's struggling to visualize the problem.


You wouldn't write …

var floor = function(x) { return Math.floor(x) }

Instead, you would write …

var floor = Math.floor

… and it will work exactly the same. This is called an Eta Conversion and if you repeat it twice, you will see how Brian got his result in the Mostly Adequate Guide.

The basic rule of Eta conversion is this:

function(x) { return f(x) } === f

… they're completely interchangeable


You can use the same technique in the original code

var getServerStuff = function(callback) {
  return ajaxCall(function(json) {
    return callback(json)
  })
}

First look at …

return ajaxCall(function(json) { return callback(json) })

Eta conversion says …

function(json) { return callback(json) } === callback

So let's look at the whole code with the results of the first eta conversion …

// first step
var getServerStuff = function(callback) {
  return ajaxCall(function(json) {
    return callback(json)
  })
}

// eta converts to ...
var getServerStuff = function(callback) {
  return ajaxCall(callback)
}

This scenario should feel familiar to us. One more eta conversion will bring us to the final simplified form. I'll add bolding one more time so we can see it better

Eta conversion says …

function(callback) { return ajaxCall(callback) } === ajaxCall
// second step
var getServerStuff = function(callback) {
  return ajaxCall(callback)
}

// eta converts to ...
var getServerStuff = ajaxCall

for all intents and purposes, they are interchangeable. Mostly Adequate Guide to FP shows little concern for dynamic binding or use of this

like image 187
Mulan Avatar answered Sep 20 '22 23:09

Mulan