1

I'm currently going through the book "Functional Programming" by Michael Fogus. I have two questions that I have been unable to answer through my own research.

I haven't seen this convention before :

function truthy(x) { return (x !== false) && existy(x) }

where existy is :

function existy(x) { return x != null }

I haven't seen the use of && when returning something. What is its purpose?

Another example of that later in the book is :

function plucker(field) {
   return function(obj) {
      return (obj && obj[field])
   }
}

In that example, if I replace obj && obj[field] with just obj[field] it returns the same thing. What is the point of writing it the way he did?

2 Answers 2

1

You're essentially returning ((x !== false) && existy(x)), which results in a boolean.

Maybe this example will make it more clear:

var number1 = 10;
var number2 = 20;

if(number1 === 10 && number2 === 20) { 
    return true;
} else {
    return false;
}

Is essentially the same as:

var number = 3;

return number1 === 10 && number2 === 20;

As for your the second part of your question, why the result is the same when you remove the obj && part from the statement:

This is because obj existsts and obj[field] exists too. If you'd try to check if something inside of an object (obj) exists, you'd get an error if it does not. Therefor , it's neccessary to check if the object exists first and then if the property inside of it exists.

So you want to ask your code:

Does object exist AND IF SO does parameter in object exist?

Which translates to:

obj && obj[field]
Sign up to request clarification or add additional context in comments.

1 Comment

I know it returns a boolean. I wasn't sure of why the && was needed in the return statement. That's why I included another example of it.
0

For an AND operation, we know that if one of the operand of the expression is false then the expression is false for sure.

Some Programming Languages make use of this laziness and do not call second operand if the first operand is false because it is a fact that the result is definitely false.

For example, in PHP, there is a very common usage:

connect() || die('Error'); 

if connect function returns true then there is no need to evaluate second operand since the result is true for sure. Because of that, die is never called.


In your plucker case, if obj is null then, it does not try to reach obj[field] because the result is already set as false (false && anything = false) and no need to evaluate it.

However, if obj is not null (true), then in order to calculate the return result, the second operand must be evaluated, which is obj[field]. So for this example, if obj[field] is not null then it returns true, otherwise returns false.

the return statement of plucker example can also be written as:

if (obj != null)
    return obj[field];
return false;

3 Comments

In this example then, if it reaches obj[field] then that is what is returned correct?
if obj is not null then the expression can be shrinken as return obj[field].
Okay I think I get it now. If I could upvote both of the answers in this thread I would.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.