Skip to content

Instantly share code, notes, and snippets.

@Retterath
Created August 8, 2019 05:42
Show Gist options
  • Save Retterath/9770273a05dcc57f4ec1d67856fc1e56 to your computer and use it in GitHub Desktop.
Save Retterath/9770273a05dcc57f4ec1d67856fc1e56 to your computer and use it in GitHub Desktop.
This program is useful for learning the basics of the function and action.
using System;
using System.Linq;
namespace AppliedArithmetics
{
class Program
{
static void Main(string[] args)
{
int[] input = Console.ReadLine()
.Split()
.Select(int.Parse)
.ToArray();
string command = Console.ReadLine();
Func<int, int> add = x => x += 1;
Func<int, int> multiply = x => x *= 2;
Func<int, int> subtract = x => x -= 1;
Action<int[]> print = x => Console.WriteLine(string.Join(" ", x));
while (command != "end")
{
switch (command)
{
// We have to change the collection each time we have to do a function on it
// Therefore we use the input = input.Select ..., otherwise input.Select alone doesnt work!
case "add":
input = input.Select(add).ToArray();
break;
case "multiply":
input = input.Select(multiply).ToArray();
break;
case "subtract":
input = input.Select(subtract).ToArray();
break;
case "print":
print(input);
break;
default:
break;
}
command = Console.ReadLine();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment