functional programming principles applied in C#
Laziness in computing means deferring a computation until its result is needed. This is beneficial when the computation is expensive and its result may not be needed.
using System;
using System.Collections.Generic;
using System.Linq;
using static System.Console;
internal class TestLazyEvaluation
{
private static IEnumerable<int> LazyNumbers
{
get
{
yield return 1;
yield return 2;
yield return 3;
yield return 4;
yield return 5;
yield return 6;
yield return 7;
yield return 8;
yield return 9;
yield return 10;
}
}
internal static void Run()
{
LazinessWithFunctions();
LazinessWithEnumerators();
}
private static void LazinessWithFunctions()
{
var random = new Random();
Func<bool> isLessThanZeroPointFive = () => random.NextDouble() < 0.5;
Func<int> left = () => 23;
Func<int> right = () => 42;
Func<int> lazyExecuted = () => isLessThanZeroPointFive()
? left()
: right();
// all the previous code is evaluated
// until we call lazyExecuted function
WriteLine(lazyExecuted());
}
private static void LazinessWithEnumerators()
{
var skipFour = LazyNumbers.Skip(4);
var takeFour = skipFour.Take(4);
// all the previous code is evaluated
// until we enumerate the collection in a foreach
foreach (var number in takeFour)
WriteLine(number);
}
}