fork download
  1. // https://g...content-available-to-author-only...b.com/koropicot/6483060
  2.  
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7.  
  8. namespace PartialApply
  9. {
  10. class Program
  11. {
  12. static void Main(string[] args)
  13. {
  14. var func = (Func<string, string, string, string, string>)((s1, s2, s3, s4) => string.Join(" ",new[]{s1,s2,s3,s4}));
  15. var funcPartialApplied = func.Curry()("My")._()("is")._().Uncurry();
  16. Console.WriteLine(funcPartialApplied("name","koropicot"));
  17. }
  18. }
  19.  
  20. public delegate TResult PartialApply<T,TResult>(T arg);
  21. public static class PartialApply
  22. {
  23. //1引数関数
  24. public static Func<T_, TResult> _<T_, TResult>(
  25. this Func<T_, TResult> func)
  26. {
  27. return func;
  28. }
  29. //2引数関数
  30. public static Func<T1,Func<T_,TResult>> _<T_,T1,TResult>(
  31. this Func<T_,Func<T1,TResult>> func)
  32. {
  33. return v1 => _ => func(_)(v1);
  34. }
  35. //3引数関数
  36. public static Func<T1,Func<T2, Func<T_, TResult>>> _<T_, T1, T2, TResult>(
  37. this Func<T_, Func<T1,Func<T2, TResult>>> func)
  38. {
  39. return v1 => v2 => _ => func(_)(v1)(v2);
  40. }
  41. //4引数関数
  42. public static Func<T1, Func<T2,Func<T3, PartialApply<T_, TResult>>>> _<T_, T1, T2, T3, TResult>
  43. (this Func<T_, Func<T1, Func<T2,Func<T3, TResult>>>> func)
  44. {
  45. return v1 => v2 => v3 => _ => func(_)(v1)(v2)(v3);
  46. }
  47. }
  48.  
  49. public static class Func
  50. {
  51. public static Func<T1, Func<T2, TResult>> Curry<T1, T2, TResult>(this Func<T1, T2, TResult> func)
  52. {
  53. return v1 => v2 => func(v1, v2);
  54. }
  55.  
  56. public static Func<T1, T2, TResult> Uncurry<T1, T2, TResult>(this Func<T1, Func<T2, TResult>> func)
  57. {
  58. return (v1, v2) => func(v1)(v2);
  59. }
  60.  
  61. public static Func<T1, Func<T2, Func<T3, TResult>>> Curry<T1, T2, T3, TResult>(this Func<T1, T2, T3, TResult> func)
  62. {
  63. return v1 => v2 => v3 => func(v1, v2, v3);
  64. }
  65.  
  66. public static Func<T1, T2, T3, TResult> Uncurry<T1, T2, T3, TResult>(this Func<T1, Func<T2, Func<T3, TResult>>> func)
  67. {
  68. return (v1, v2, v3) => func(v1)(v2)(v3);
  69. }
  70.  
  71. public static Func<T1, Func<T2, Func<T3, Func<T4, TResult>>>> Curry<T1, T2, T3, T4, TResult>(this Func<T1, T2, T3, T4, TResult> func)
  72. {
  73. return v1 => v2 => v3 => v4 => func(v1, v2, v3, v4);
  74. }
  75.  
  76. public static Func<T1, T2, T3, T4, TResult> Uncurry<T1, T2, T3, T4, TResult>(this Func<T1, Func<T2, Func<T3, Func<T4, TResult>>>> func)
  77. {
  78. return (v1, v2, v3, v4) => func(v1)(v2)(v3)(v4);
  79. }
  80. }
  81. }
Success #stdin #stdout 0.02s 33992KB
stdin
Standard input is empty
stdout
My name is koropicot