Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Special pages
Niidae Wiki
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Lazy evaluation
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
====.NET ==== In the [[.NET]] framework, it is possible to do lazy evaluation using the class <syntaxhighlight inline lang="csharp">System.Lazy<T></syntaxhighlight>.<ref>{{cite web|url=http://msdn.microsoft.com/de-de/library/vstudio/dd642331.aspx|title=Lazy(T) Class (System)|publisher=Microsoft}}</ref> The class can be easily exploited in [[F Sharp (programming language)|F#]] using the <syntaxhighlight inline lang="fsharp">lazy</syntaxhighlight> keyword, while the <syntaxhighlight inline lang="fsharp">force</syntaxhighlight> method will force the evaluation. There are also specialized collections like <syntaxhighlight inline lang="csharp">Microsoft.FSharp.Collections.Seq</syntaxhighlight> that provide built-in support for lazy evaluation. <syntaxhighlight lang="fsharp"> let fibonacci = Seq.unfold (fun (x, y) -> Some(x, (y, x + y))) (0I,1I) fibonacci |> Seq.nth 1000 </syntaxhighlight> In C# and VB.NET, the class <syntaxhighlight inline lang="csharp">System.Lazy<T></syntaxhighlight> is directly used. <syntaxhighlight lang="csharp"> public int Sum() { int a = 0; int b = 0; Lazy<int> x = new Lazy<int>(() => a + b); a = 3; b = 5; return x.Value; // returns 8 } </syntaxhighlight> Or with a more practical example: <syntaxhighlight lang="csharp"> // recursive calculation of the n'th fibonacci number public int Fib(int n) { return (n == 1)? 1 : (n == 2)? 1 : Fib(n-1) + Fib(n-2); } public void Main() { Console.WriteLine("Which Fibonacci number do you want to calculate?"); int n = Int32.Parse(Console.ReadLine()); Lazy<int> fib = new Lazy<int>(() => Fib(n)); // function is prepared, but not executed bool execute; if (n > 100) { Console.WriteLine("This can take some time. Do you really want to calculate this large number? [y/n]"); execute = (Console.ReadLine() == "y"); } else execute = true; if (execute) Console.WriteLine(fib.Value); // number is only calculated if needed } </syntaxhighlight> Another way is to use the <syntaxhighlight inline lang="csharp">yield</syntaxhighlight> keyword: <syntaxhighlight lang="csharp"> // eager evaluation public IEnumerable<int> Fibonacci(int x) { IList<int> fibs = new List<int>(); int prev = -1; int next = 1; for (int i = 0; i < x; i++) { int sum = prev + next; prev = next; next = sum; fibs.Add(sum); } return fibs; } // lazy evaluation public IEnumerable<int> LazyFibonacci(int x) { int prev = -1; int next = 1; for (int i = 0; i < x; i++) { int sum = prev + next; prev = next; next = sum; yield return sum; } } </syntaxhighlight> {{Main|Thunk}} {{Expand section|date=May 2011}}
Summary:
Please note that all contributions to Niidae Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Encyclopedia:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Search
Search
Editing
Lazy evaluation
(section)
Add topic