Guest User

Untitled

a guest
Feb 15th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.39 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace Passwrods
  5. {
  6. internal static class Program
  7. {
  8. public static void Main(string[] args)
  9. {
  10. Func<char, bool> isSpecial = x => (new[] { '$', '#', '@' }).Contains(x);
  11.  
  12. ResultFunc<string, string> hasUpper = text => text.Check(x => x.Any(char.IsUpper));
  13. ResultFunc<string, string> hasLower = text => text.Check(x => x.Any(char.IsLower));
  14. ResultFunc<string, string> hasDigit = text => text.Check(x => x.Any(char.IsDigit));
  15. ResultFunc<string, string> hasSpecial = text => text.Check(x => x.Any(isSpecial));
  16. ResultFunc<string, string> greaterThan = text => text.Check(x => x.Length >= 6);
  17. ResultFunc<string, string> lessThan = text => text.Check(x => x.Length <= 12);
  18.  
  19. ResultFunc<string, string> checkPassword = hasUpper
  20. .Compose(hasLower)
  21. .Compose(hasDigit)
  22. .Compose(hasSpecial)
  23. .Compose(greaterThan)
  24. .Compose(lessThan);
  25.  
  26. string result = Console.ReadLine()
  27. .Split(',', StringSplitOptions.RemoveEmptyEntries)
  28. .Select(Result.Ok)
  29. .Select(checkPassword.Invoke)
  30. .Where(Result.IsOk)
  31. .Select(Result.Match)
  32. .Aggregate((left, right) => $"{left},{right}");
  33.  
  34. Console.WriteLine(result);
  35. }
  36. }
  37.  
  38. public class Result<T>
  39. {
  40. public Result(T value) { Value = value; }
  41.  
  42. public T Value { get; }
  43. }
  44.  
  45. public sealed class OkResult<T> : Result<T>
  46. {
  47. public OkResult(T value) : base(value) { }
  48. }
  49.  
  50. public sealed class ErrorResult : Result<string>
  51. {
  52. public ErrorResult(string message) : base(message) { }
  53. }
  54.  
  55. public static class Result
  56. {
  57. public static Result<T> Ok<T>(T value) => new OkResult<T>(value);
  58.  
  59. public static Result<string> Error(string message) => new ErrorResult(message);
  60.  
  61. public static bool IsOk<T>(this Result<T> result) => result is OkResult<T>;
  62.  
  63. public static bool IsError<T>(this Result<T> result) => result is ErrorResult;
  64.  
  65. public static T Match<T>(this Result<T> value)
  66. {
  67. switch (value)
  68. {
  69. case OkResult<T> okay:
  70. return okay.Value;
  71. case ErrorResult error:
  72. return default;
  73. default:
  74. return default;
  75. }
  76. }
  77.  
  78. public static Result<string> Check(this Result<string> text, Func<string, bool> func)
  79. {
  80. switch (text)
  81. {
  82. case OkResult<string> okay:
  83. return Check(okay.Value, func);
  84. case ErrorResult error:
  85. return error;
  86. default:
  87. return text;
  88. }
  89. }
  90.  
  91. private static Result<string> Check(string value, Func<string, bool> condition)
  92. {
  93. return condition(value) ? Ok(value) : Error("Value does not match a given condition.");
  94. }
  95. }
  96.  
  97. public delegate Result<T2> ResultFunc<T1, T2>(Result<T1> value);
  98.  
  99. public static class ResultFuncExtensions
  100. {
  101. public static ResultFunc<T1, T3> Compose<T1, T2, T3>(this ResultFunc<T1, T2> left, ResultFunc<T2, T3> right)
  102. {
  103. return new ResultFunc<T1, T3>(value => right(left(value)));
  104. }
  105. }
  106. }
Add Comment
Please, Sign In to add comment