Advertisement
yanchevilian

07. Predicate for names / Functional Programming

Sep 7th, 2021
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.41 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _7._Predicate_for_Names
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             //---------------------------- Variant vol. 1------------------------------------------
  12.             //int nameLenght = int.Parse(Console.ReadLine());
  13.             //List<string> allNames =
  14.             //    new List<string>(Console.ReadLine().Split(" ", StringSplitOptions.RemoveEmptyEntries).ToList());
  15.  
  16.             //Func<List<string>, int, List<string>> func = (collection, length) =>
  17.             //    collection.Where(n => n.Length <= nameLenght).ToList();
  18.             //allNames = func(allNames, nameLenght);
  19.             //Console.WriteLine(string.Join(Environment.NewLine, allNames));
  20.            
  21.             //------------------------------ Variant vol. 2------------------------------------------
  22.             int length = int.Parse(Console.ReadLine());
  23.             string[] allNames = Console.ReadLine().Split();
  24.             Action<string[]> printNames = name =>
  25.             {
  26.                 Predicate<string> validatePredicate = currName => currName.Length <= length;
  27.                 foreach (var currentName in allNames.Where(n => validatePredicate(n)))
  28.                 {
  29.                     Console.WriteLine(currentName);
  30.                 }
  31.             };
  32.             printNames(allNames);
  33.         }
  34.     }
  35. }
  36.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement