Need help on: Understand Functional Programming Terminology

Hello All. I’m unable to understand some basic info in this challenge.
First, I don’t understand the comments:

/**

  • A long process to prepare green tea.
  • @return {string} A cup of green tea.
    **/

and

/**

  • A long process to prepare black tea.
  • @return {string} A cup of black tea.
    **/

What do they mean by ‘A long process to prepare green tea’ and ‘A long process to prepare black tea’ ? These don’t really seem relevant to the challenge unless I’m missing something.

Also, in the instructions they state: Callbacks are the functions that are slipped or passed into another function to decide the invocation of that function. What is meant by ‘to decide the invocation of that function’? Specifically, what does ‘invocation’ mean in this context? How are we deciding the invocation?

They also state, ’ Functions that can be assigned to a variable, passed into another function, or returned from another function just like any other normal value, are called first class functions. In JavaScript, all functions are first class functions.’ If all JS functions are first "class functions,’ why don’t we just call them ‘functions’ like we normally do? Why use more words to refer to the same thing?

Also, they state, ‘Note that the getTea function has been modified so it now takes a function as the first argument.’ How do we know that the first argument is a function?

I haven’t written any code yet since I’m still trying to figure out the explanation/instructions.

Any guidance on the above questions would be appreciated.

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/functional-programming/understand-functional-programming-terminology

Hell

/**
 * A long process to prepare green tea.
 * @return {string} A cup of green tea.
 **/

This is a description of the code below, just a function with some logic (=process to prepare tea, could be long, if we wrote it), which returns a string value.

const prepareGreenTea = () => 'greenTea';

Without arrow

const prepareGreenTea = function() {return  'greenTea';}

First-class function wiki
In JavaScript, all functions are first class functions, in other languages may not be.

1 Like

Hi and thanks for your reply.

I’m still not clear about how the comments describe the code below. What do you mean by “could be long, if we wrote it?” Do you mean that the function could be long? Long in comparison to what? And it mentions “a long process” - is that referring to the function?

I’m not understanding the logic here. Why would we need to have a function? Couldn’t we just assign the variable to a string, e.g. const prepareGreenTea = 'greenTea';

Thanks for mentioning that functions in other languages may not be first class functions. It would have made more sense if they had explained that in the instructions, so thanks for the clarification.

Any other help would be welcome, since I still don’t have enough understanding of the instructions to address the coding challenge.

it could be something like this
it just an example of what a function could be

function prepareGreenTea () => {
   // grow plant
   // collect leaves
   // let leaves dry
   // etc
   // boil water 
   // make tea
   // now you have tea
   return greenTea;
}

The terminology that I’m used to is not that ‘in some languages there are first class functions’, but rather, ‘in some languages, functions are first class objects.’

This refers to the fact that in JavaScript (among other languages, functional ones, hybrids and even OOP like Java 8 and C# when they introduced delegates and then lambdas), functions can be treated like other objects:

  • Assigned to variables:
  • Returned from functions
  • Passed to functions as arguments
  • Stored in arrays and key-value pair objects (JSON)
  • They have properties and methods too like .toString, .length, etc

This proves useful when you try to apply functional programming principles in JS such as currying, parameterization, partial application, emulated private scope and callbacks.

Now, receiving functions as arguments (let it be the first one, second one, nth one or whatever) and invoking them just means:

function executeFunctionWith5(someFunction) {
  someFunction(5)
  // or
  // someFunction.apply(this, [5])
  // or
  // someFunction.call(this, 5)
  // you just "invoked" the "callback" given as the first
  // argument (below this function declaration) and
  // specified as the first parameter here.
}

executeFunctionWith5(function(number) {
  console.log(`The double of ${number} is ${number * 2}!`)
})

//!> The dobule of 5 is 10!

Thanks for the reply.

After reviewing and understanding (I think!) the solution code, I don’t think the comments accurately describe the code. It’s confusing for someone reading the code, since there is no ‘long process’ in the code.

I think it would have made more sense to comment:

/**
 * @return {string} 'greenTea' which represents one cup of green tea. 
 **/

I’m still not clear on why we cannot just use:

const prepareTea = 'greenTea';

Is there any reason we need to make it into a function, rather than just assigning the variable to the string ‘greenTea?’

Thanks for taking the time to provide this detailed explanation.