Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Lazy evaluation
(section)
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}}
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)