Miss understand some part of Higher-order functions

Miss understand some part of Higher-order functions
0

#1

I understand how this function works. But I do not understand where // → 2 is even comes? It calls by repeat function together with unless ?
Thank you for answer.


#2

I’m not sure if im qualified enough, but:

  1. Repeat is just a function that takes two arguments: a number(n), and a function(body). The repeat's sole purpose in life is to call body function n times and passing a current number as body's argument. Here, you are calling repeat(3, body(n)) which basically equals this:
    body(0);
    body(1);
    body(2);

  2. Unless is a function that takes some statement (test) and a function (then). Unless calls then function if the test's result is a falsy value. So, what you have at lines 2782-2785 can be translated into:
    unless(0 % 2, then); // 0 % 2 == 0
    unless(1 % 2, then); // 1 % 2 == 1
    unless(2 % 2, then); // 2 % 2 == 0

So your then() function will be called only for the first and third call of unless, printing whatever you have passed into console.log.

I know it’s not exactly the clearest explanation, but I hope it helps.


#3

Yes,it helps)) Thanks


#4

Glad I could help :D.


#5

I’ve never thought that eric cartman will help me to explain high funct)


#6

His dickish attitude is just a pose, hes really sensitive and helpful on the inside :stuck_out_tongue:


#7

I forgot to ask the question. In this case 0 == false? Or it’s always so?


#8

0 is considered a falsy value by JavaScript. Falsy value is a value that, after converting to boolean, is false. Falsy values in JS are:
false
0
NaN
null
undefined
"" //empty string

So 0 is not technically false but the snippet you provided uses if statement. In JS, the condition you provide for if is implicitly coerced (“coerce” mean more or less “convert”) to boolean. So the whole 0 % 2 statement is later coerced to boolean by JS behind the covers, and that resolves to false.
Again, not the cleanest explanation, sorry for that…

On the side note, when you use == operator (loose equality) you allow JS to implicitly coerce values while comparing them. So if you actually type 0 == false in the console, it will print true. To compare values with coercion disallowed, you use ===.


#9

I understand.Thank u.
You have a polish surname. Are you polish?)


#10

Cartman is in love now! No wonder he’s being helpful, Butters;)

Anyway, I wrote an extensive answer on higher order function (although for different question). Hope this might help you get yourself clarified.


#11

@SerafimPoch Yup, I’m from Warsaw more specifically :slight_smile:

@adityaparab And girls can be totally funny dude. Good link there, very detailed answer!


#12

@SerafimPoch
Here is a great resource comparing all the funky JS truth-false values: http://dorey.github.io/JavaScript-Equality-Table/


#13

I’m from Ukraine. Live in Kiev. I want go to Krakov or Warshava for excursion . Watch some architecture etc. What do you think, I should go to Krakow or Warshava ?)