Nested function: Difference between revisions

From Rosetta Code
Content added Content deleted
m (Remove unnecessary empty lines)
(added python)
Line 26: Line 26:
}
}


Console.WriteLine(MakeList(". "));
Console.WriteLine(MakeList(". "));</lang>
</lang>


=={{header|JavaScript}}==
=={{header|JavaScript}}==
Line 41: Line 40:
}
}


console.log(makeList(". "));
console.log(makeList(". "));</lang>
</lang>


=={{header|Lua}}==
=={{header|Lua}}==
Line 56: Line 54:
end
end


print(makeList(". "))
print(makeList(". "))</lang>

</lang>
=={{header|Python}}==
{{works with|Python|3+}}
<lang python>def makeList(separator):
counter = 1

def makeItem(item):
nonlocal counter
result = str(counter) + separator + item + "\n"
counter += 1
return result

return makeItem("first") + makeItem("second") + makeItem("third")

print(makeList(". "))</lang>


=={{header|zkl}}==
=={{header|zkl}}==

Revision as of 18:15, 18 September 2016

Nested function is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

In many languages, functions can be nested, so there are outer functions and inner functions. The inner function can then access the variables of the outer function. In most languages, the inner function can also modify variables from the outer function.

See: Wikipedia: Nested function article.


The following examples for MakeList or makeList generate the following text:

1. first
2. second
3. third

C#

<lang csharp>string MakeList(string separator) {

   var counter = 1;
   var makeItem = new Func<string, string>((item) => {
       return counter++ + separator + item + "\n";
   });
   return makeItem("first") + makeItem("second") + makeItem("third");

}

Console.WriteLine(MakeList(". "));</lang>

JavaScript

<lang javascript>function makeList(separator) {

 var counter = 1;
 function makeItem(item) {
   return counter++ + separator + item + "\n";
 }
 return makeItem("first") + makeItem("second") + makeItem("third");

}

console.log(makeList(". "));</lang>

Lua

<lang lua>function makeList(separator)

 local counter = 1
 local function makeItem(item)
   return counter .. separator .. item .. "\n"
 end
 return makeItem("first") .. makeItem("second") .. makeItem("third")

end

print(makeList(". "))</lang>

Python

Works with: Python version 3+

<lang python>def makeList(separator):

   counter = 1
   def makeItem(item):
       nonlocal counter
       result = str(counter) + separator + item + "\n"
       counter += 1
       return result
   return makeItem("first") + makeItem("second") + makeItem("third")

print(makeList(". "))</lang>

zkl

zkl functions don't have direct access to another functions scope, they are not nested. If a function is defined in another function, the compiler moves it out and hands you a reference to the function. So, you are unable to modify variables in the enclosing scope unless you are given a container which can be modified. Partial application can be used to bind [copies] of scope information to a function, that information is fixed at the point of application and becomes strictly local to the binding function (ie changes do not propagate). A Ref[erence] is a container that holds an object so it can be modified by other entities. <lang zkl>fcn makeList(separator){

 counter:=Ref(1);  // a container holding a one. A reference.
 // 'wrap is partial application, in this case binding counter and separator
 makeItem:='wrap(item){ c:=counter.inc(); String(c,separator,item,"\n") };
 makeItem("first") + makeItem("second") + makeItem("third")

}

print(makeList(". "));</lang>

Output:
1. first
2. second
3. third