// - Main
Console.WriteLine(“I’d like some Spaghetti!”);
Console.WriteLine(“I’d like some Chocolate Moose!”);
public void SwedishChief(string food)
{
Console.WriteLine(string.Format(“I’d like some {0}!”, food));
}
SwedishChief(“Spaghetti”);
SwedishChief(“Chocolate Moose”);
Console.WriteLine(“get the lobster”);
PutInPot(“water”);
PutInPot(“lobster”);
Console.WriteLine(“get the chicken”);
BoomBoom(“chicken”);
BoomBoom(“coconut”);
// :
// ,
// alert.
public static T Alert<T>(T message)
{
Console.WriteLine(message.ToString());
return message;
}
public void Cook(string ingredientOne, string ingredientTwo, Action<string> function)
{
Console.WriteLine(string.Format("get the {0}",ingredientOne));
function(ingredientOne);
function(ingredientTwo);
}
Cook(“lobster”, “water”, PutInPot);
Cook(“chicken”, “coconut”, BoomBoom);
Cook("lobster",
"water",
x => Alert("pot " + x));
Cook("chicken",
"coconut",
x => Alert("boom " + x));
...
var list = new List<int> { 1, 2, 3 };
for (var i = 0; i < list.Count; i++)
{
list[i] = list[i] * 2;
}
foreach (var el in list)
{
Alert(el.ToString());
}
...
public static void Map<T>(Func<T, T> action, IList<T> list)
{
for (var i = 0; i < list.Count; i++)
list[i] = action(list[i]);
}
...
Map((x) => x * 2, list);
Map(Alert, list);
...
…
Alert(Sum(list));
Alert(Join(new[] { "a", "b", "c" }));
...
public static int Sum(IEnumerable<int> list)
{
var sum = 0;
foreach (var el in list)
sum += el;
return sum;
}
public static string Join(IEnumerable<string> list)
{
var result = string.Empty;
foreach (var el in list)
result += el;
return result;
}
public static T Reduce<T>(Func<T, T, T> func, IEnumerable<T> list, T init)
{
var accumulator = init;
foreach (var el in list)
accumulator = func(accumulator, el);
return accumulator;
}
public static int Sum(IEnumerable<int> list)
{
return Reduce((x, y) => x + y, list, 0);
}
public static string Join(IEnumerable<string> list)
{
return Reduce((a,b) => a + b, list, string.Empty);
}
Source: https://habr.com/ru/post/148037/
All Articles