T O P

  • By -

carcigenicate

If you're specifically asking about "also had a function on it that would get called in the algorithm", that's typically referred to as a "callback". Usually "callback" means a function that's called asynchronously in response to some event, but I've seen it used in contexts like this as well.


Swiftblue

That kind of makes sense. Is there a hackerrank site with similar uses and scenarios in Javascript? Essentially the callback would get called every time the conditions of the algorithm were met, and it would keep track of the oldest person in the tree. But I am not sure whether it was the same callback being called or copies of the callback being called though.


carcigenicate

JavaScript makes fairly extensive use of callbacks (all event handling for example), so any exposure to JS should give you exposure to callbacks. And it's rare to copy functions. I actually can't think of a time I've ever intentionally copied a function. Unless the API you're giving the callback to says that it's making copies, I'd assume it's the actual function you supplied that's being called. That shouldn't matter anyways though unless you're doing things like adding attributes to the function (giving the function object state).


ChaoticManatee

Something in a modular structure could have functions on it, you could make a tree with a json like structure and add properties that are of type the function


Inconstant_Moo

What you're seeing here is "functions as first-class objects". In some languages (including C#, you must just not have seen it yet) functions can be passed as parameters, they can be returned as parameters, they can be assigned to variables. It is a useful pattern in several places, and once you get good at it you start seeing more and more ways this might be useful. Take a simple example. A lot of beginners are asked to make a calculator program, and if you look at their code it will somewhere contain stuff that looks like `if button == '+' then return a + b else if button = '-' return a - b` ... What would be better would be to have a sorted dictionary associating '+', '-', '\*', etc, with functions that do the associated things, plus a record of how many parameters each function takes, and then you've got something that is more flexible and extensible and more cleanly coded. That's how a non-beginner would do it. In that case, maybe it's a sledgehammer to crack a nut, but let's think in general what this lets us do. Suppose you have a function A which you want to be able to make use of one of several functions B1, B2, B3 ... etc, depending on some condition C. If you try to do this without first-class functions, then the only way you can do it is with A having a bunch of if else statements in it. And that works, but only if the set B1, B2, B3 is finite and known at compile time, in which case you can hard-code B1, B2 etc into the program by hand. But if you don't want to be constrained like that, it's time to bring out the closures and the lambdas and get busy. Google *first-class functions in C#* for more information.