Advertisement
gabi11

Functional Programming - 05. Applied Arithmetics

May 26th, 2019
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.46 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6.  
  7.  
  8. namespace CSharpAdvanced
  9. {
  10.  
  11.     class Program
  12.     {
  13.         static void Main(string[] args)
  14.         {
  15.             var numbers = Console.ReadLine()
  16.                 .Split()
  17.                 .Select(int.Parse)
  18.                 .ToList();
  19.  
  20.             Func<int, int> funcAdd = n => n + 1;
  21.             Func<int, int> funcMultiply = n => n * 2;
  22.             Func<int, int> funcSubtract = n => n - 1;
  23.  
  24.             Action<List<int>> printAction = n => Console.WriteLine(string.Join(" ", n));
  25.  
  26.             while (true)
  27.             {
  28.                 var command = Console.ReadLine();
  29.  
  30.                 if (command == "end")
  31.                 {
  32.                     break;
  33.                 }
  34.  
  35.                 switch (command)
  36.                 {
  37.                     case "add":
  38.                         numbers = numbers.Select(n => funcAdd(n)).ToList();
  39.                         break;
  40.  
  41.                     case "multiply":
  42.                         numbers = numbers.Select(n => funcMultiply(n)).ToList();
  43.                         break;
  44.  
  45.                     case "subtract":
  46.                         numbers = numbers.Select(n => funcSubtract(n)).ToList();
  47.                         break;
  48.  
  49.                     case "print":
  50.                         printAction(numbers);
  51.                         break;
  52.                 }
  53.             }
  54.         }
  55.     }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement