I have a javascript coding exercise to do which has got me a bit stuck (I'm only just starting javascript).  The exercise is as follows:  Write a function multiply(a) that returns a function capable of multiplying by a. Call this function with b as a parameter.  So far I have the main skeleton (not difficult):  function multiply(a) {     return //Stuck here }   I'm not sure if the question is to call multiply(b) and have it give us the result of a*b or something else...  I tried writing a function directly after the return statement but this just printed out the function name.  function multiply(a) {     return function f { return a * b } //Here I assume b is a variable defined somewhere }   Thanks in advance!
24212
I've seen references to curried functions in several articles and blogs but I can't find a good explanation (or at least one that makes sense!)
89
I'm not that into dynamic programming languages but I've written my fair share of JavaScript code. I never really got my head around this prototype-based programming, does any one know how this works? var obj = new Object(); obj.prototype.test = function() { alert('Hello?'); }; var obj2 = new obj(); obj2.test();  I remember a lot discussion I had with people a while back (I'm not exactly sure what I'm doing) but as I understand it, there's no concept of a class. It's just an object, and instances of those objects are clones of the original, right? But what is the exact purpose of this &quot;.prototype&quot; property in JavaScript? How does it relate to instantiating objects? Update: correct way var obj = new Object(); // not a functional object obj.prototype.test = function() { alert('Hello?'); }; // this is wrong!  function MyObject() {} // a first class functional object MyObject.prototype.test = function() { alert('OK'); } // OK  Also these  really helped a lot.
12542
Are there good reasons why it's a better practice to have only one return statement in a function?   Or is it okay to return from a function as soon as it is logically correct to do so, meaning there may be many return statements in the function?
56167
I mostly use lambda functions but sometimes use nested functions that seem to provide the same behavior. Here are some trivial examples where they functionally do the same thing if either were found within another function: Lambda function &gt;&gt;&gt; a = lambda x : 1 + x &gt;&gt;&gt; a(5) 6  Nested function &gt;&gt;&gt; def b(x): return 1 + x  &gt;&gt;&gt; b(5) 6  Are there advantages to using one over the other? (Performance? Readability? Limitations? Consistency? etc.) Does it even matter? If it doesn't then does that violate the Pythonic principle:  .
9415
I have been using AMPscript for a while and I know that there is no way to create custom functions. I was wondering if anybody has come up with an alternative for this?  thanks
51596
I'm a new user of Matlab, can you please help: I have the following code in an .M file:  function f = divrat(w, C) S=sqrt(diag(diag(C))); s=diag(S); f=sqrt(w'*C*w)/(w'*s);   I have stored this file (divrat.M) in the normal Matlab path, and therefore I'm assuming that Matlab will read the function when it's starting and that this function therefore should be available to use.  However, when I type  &gt;&gt; divrat(w, C)   I get the following error     ??? Undefined function or method 'divrat' for input arguments of type 'double'.     What is the error message telling me to do, I can't see any error in the code or the function call?
87498
I've gotten stuck at the following exercise;     There are m functions from a one-element set to the set {1, 2, …, m}.   How many functions are there from a two-element set to {1, 2, …, m}?   From a three-element set? Give a recurrence for the number of T(n) of   functions from an n-element set to {1, 2, …, m}. Solve the recurrence.   I'm not sure how to begin solving it?
44049
Is there a preferred way to return multiple values from a C++ function?  For example, imagine a function that divides two integers and returns both the quotient and the remainder.  One way I commonly see is to use reference parameters:  void divide(int dividend, int divisor, int&amp; quotient, int&amp; remainder);   A variation is to return one value and pass the other through a reference parameter:  int divide(int dividend, int divisor, int&amp; remainder);   Another way would be to declare a struct to contain all of the results and return that:  struct divide_result {     int quotient;     int remainder; };  divide_result divide(int dividend, int divisor);   Is one of these ways generally preferred, or are there other suggestions?  Edit: In the real-world code, there may be more than two results.  They may also be of different types.
29305
What does it mean to extend a function? Can someone please give an example?  Thanks in advance!
74496
Please advise how to pass parameters into a function called using setInterval.  My example setInterval(funca(10,3), 500); is incorrect.
40166
Is there a way to use constants in JavaScript?  If not, what's the common practice for specifying variables that are used as constants?
1023
I've seen this in a few   function fn() {     return +new Date; }   And I can see that it is returning a timestamp rather than a date object, but I can't find any documentation on what the plus sign is doing.  Can anyone explain?
41585
Could someone explain? I understand the basic concepts behind them but I often see them used interchangeably and I get confused.   And now that we're here, how do they differ from a regular function?
81008
Can the product of $A, B$ be computed using only $+, -,$ and reciprocal operators using a calculator? You can use calculator's memory function (multiply and divide are broken though).  Additional: I should have mentioned earlier, in addition to the 3 operators, the numberpad of the calculator can be used so yes 1 can be used.
13706
The canonical way to return multiple values in languages that support it is often . Option: Using a tuple Consider this trivial example: def f(x):   y0 = x + 1   y1 = x * 3   y2 = y0 ** y3   return (y0, y1, y2)  However, this quickly gets problematic as the number of values returned increases. What if you want to return four or five values? Sure, you could keep tupling them, but it gets easy to forget which value is where. It's also rather ugly to unpack them wherever you want to receive them. Option: Using a dictionary The next logical step seems to be to introduce some sort of 'record notation'. In Python, the obvious way to do this is by means of a dict. Consider the following: def g(x):   y0 = x + 1   y1 = x * 3   y2 = y0 ** y3   return {'y0': y0, 'y1': y1 ,'y2': y2}  (Just to be clear, y0, y1, and y2 are just meant as abstract identifiers. As pointed out, in practice you'd use meaningful identifiers.) Now, we have a mechanism whereby we can project out a particular member of the returned object. For example, result['y0']  Option: Using a class However, there is another option. We could instead return a specialized structure. I've framed this in the context of Python, but I'm sure it applies to other languages as well. Indeed, if you were working in C this might very well be your only option. Here goes: class ReturnValue:   def __init__(self, y0, y1, y2):      self.y0 = y0      self.y1 = y1      self.y2 = y2  def g(x):   y0 = x + 1   y1 = x * 3   y2 = y0 ** y3   return ReturnValue(y0, y1, y2)  In Python the previous two are perhaps very similar in terms of plumbing - after all { y0, y1, y2 } just end up being entries in the internal __dict__ of the ReturnValue. There is one additional feature provided by Python though for tiny objects, the __slots__ attribute. The class could be expressed as: class ReturnValue(object):   __slots__ = [&quot;y0&quot;, &quot;y1&quot;, &quot;y2&quot;]   def __init__(self, y0, y1, y2):      self.y0 = y0      self.y1 = y1      self.y2 = y2  From the :  The __slots__ declaration takes a sequence of instance variables and reserves just enough space in each instance to hold a value for each variable. Space is saved because __dict__ is not created for each instance.  Option: Using a  (Python 3.7+) Using Python 3.7's new dataclasses, return a class with automatically added special methods, typing and other useful tools: @dataclass class Returnvalue:     y0: int     y1: float     y3: int  def total_cost(x):     y0 = x + 1     y1 = x * 3     y2 = y0 ** y3     return ReturnValue(y0, y1, y2)  Option: Using a list Another suggestion which I'd overlooked comes from Bill the Lizard: def h(x):   result = [x + 1]   result.append(x * 3)   result.append(y0 ** y3)   return result  This is my least favorite method though. I suppose I'm tainted by exposure to Haskell, but the idea of mixed-type lists has always felt uncomfortable to me. In this particular example the list is -not- mixed type, but it conceivably could be. A list used in this way really doesn't gain anything with respect to the tuple as far as I can tell. The only real difference between lists and tuples in Python is that lists are , whereas tuples are not. I personally tend to carry over the conventions from functional programming: use lists for any number of elements of the same type, and tuples for a fixed number of elements of predetermined types. Question After the lengthy preamble, comes the inevitable question. Which method (do you think) is best?
78669
I would like to define a mathematical function by its action. Something like       \documentclass{article}      \begin{document}      \[         \begin{array}{cccc}          f\colon &amp; R\times R &amp; \rightarrow &amp; R\times R\\                  &amp; v &amp; \mapsto &amp; f(v)         \end{array}       \]       \end{document}   In this way, spacing is horrible. What is the best way to produce it?  I want the elements in the second line centered with respect to the element of the first line.
62941
Find all functions $f$ that assign a real number $f(x)$ to every real number $x$ such that $$(x+y)f(x)+f(y^2)=(x+y)f(y)+f(x^2)$$  I've tried subbing in heaps of values but I keep getting things like $f(0)=f(0)$ and other such useless results.  Any help would be hugely appreciated.