Closures/Value capture: Difference between revisions

Content added Content deleted
No edit summary
m (used a different font for (italic) lowercase "i".)
Line 2: Line 2:


;Task:
;Task:
Create a list of ten functions, in the simplest manner possible &nbsp; (anonymous functions are encouraged), &nbsp; such that the function at index&nbsp;&nbsp;<big> ''i'' </big> &nbsp; (you may choose to start &nbsp; <big> ''i'' </big> &nbsp; from either &nbsp; <big> '''0''' </big> &nbsp; or &nbsp; <big> '''1'''), </big> &nbsp; when run, should return the square of the index, &nbsp; that is, &nbsp; <big> ''i'' <sup>2</sup>.</big>
Create a list of ten functions, in the simplest manner possible &nbsp; (anonymous functions are encouraged), &nbsp; such that the function at index&nbsp;&nbsp;<big> ''<b> i </b>'' </big> &nbsp; (you may choose to start &nbsp; <big> ''<b> i </b>'' </big> &nbsp; from either &nbsp; <big> '''0''' </big> &nbsp; or &nbsp; <big> '''1'''), </big> &nbsp; when run, should return the square of the index, &nbsp; that is, &nbsp; <big> ''<b> i </b>'' <sup>2</sup>.</big>


Display the result of running any but the last function, to demonstrate that the function indeed remembers its value.
Display the result of running any but the last function, to demonstrate that the function indeed remembers its value.