PageRenderTime 45ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/DynamicSugar.Console/Program.cs

http://github.com/fredericaltorres/DynamicSugarNet
C# | 314 lines | 224 code | 77 blank | 13 comment | 14 complexity | 73000b25395ef4af77ed0e79d178f720 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using DynamicSugar;
  6. using System.Dynamic;
  7. namespace DynamicSugar.ConsoleApplication {
  8. class Program {
  9. static void Main(string[] args) {
  10. N.samples.Sample6_2();
  11. Console.WriteLine("Dynamic Sugar # Library\r\n");
  12. Why();
  13. Why2();
  14. In();
  15. FormatMethodSample();
  16. FormatMethodWithDictionarySample();
  17. FormatMethodWithExpandoObjectSample();
  18. DictMethodSample();
  19. RangeSample();
  20. List_Map();
  21. List_Inject();
  22. List_Filter();
  23. MultiValuesSample();
  24. ToFile_FromFile();
  25. Include();
  26. Without();
  27. First_Last_Rest();
  28. Pluck();
  29. Reject();
  30. N.samples.Sample13();
  31. N.samples.Sample12();
  32. Pause();
  33. }
  34. static void Why2(){
  35. // Regular Syntax
  36. var LastName = "TORRES";
  37. var Age = 45;
  38. var s1 = String.Format("LastName:{0}, Age:{1:000}", LastName, Age);
  39. // First syntax with DynamicSugar
  40. LastName = "TORRES";
  41. Age = 45;
  42. s1 = "LastName:{0}, Age:{1:000}".FormatString(LastName, Age);
  43. // Second syntax with DynamicSugar
  44. s1 = "LastName:{LastName}, Age:{Age:000}".Template( new { LastName, Age } );
  45. }
  46. static void Why(){
  47. // Syntax 1, with regular C#
  48. List<int> someIntegers = new List<int>() { 1, 2 ,3 };
  49. int i = 2;
  50. if(someIntegers.Contains(i)){
  51. Console.WriteLine("2 is in the list");
  52. }
  53. // Syntax 2, with regular C#
  54. i = 2;
  55. if((new List<int>() { 1, 2 ,3 }).Contains(i)){
  56. Console.WriteLine("2 is in the list");
  57. }
  58. // Syntax DynamicSugar
  59. i = 2;
  60. if(i.In(DS.List(1, 2 ,3))){
  61. Console.WriteLine("2 is in the list");
  62. }
  63. }
  64. enum CustomerType {
  65. Good,
  66. Bad,
  67. SoSo
  68. }
  69. static void In(){
  70. int i = 1;
  71. if(i.In(1,2,3)){
  72. }
  73. List<int> l = DS.List(1,2,3);
  74. if(i.In(l)){
  75. }
  76. var state = "good";
  77. if(state.In("good","bad","soso")){
  78. }
  79. var customerType = CustomerType.Good;
  80. if(customerType.In(CustomerType.Good, CustomerType.SoSo)){
  81. }
  82. var people = DS.List(
  83. new Person() { LastName = "Descartes", FirstName = "Rene", BirthDay = new DateTime(1596,3,31)},
  84. new Person() { LastName = "Montesquieu", FirstName = "Charles-Louis", BirthDay = new DateTime(1689,1,18)},
  85. new Person() { LastName = "Rousseau", FirstName = "JJ", BirthDay = new DateTime(1712,3,31)}
  86. );
  87. var Descartes = new Person() { LastName = "Descartes", FirstName = "Rene", BirthDay = new DateTime(1596,3,31)};
  88. if(Descartes.In(people)){
  89. Console.WriteLine("In the list people");
  90. }
  91. else{
  92. Console.WriteLine("Not in the list people");
  93. }
  94. }
  95. static void RangeSample(){
  96. foreach(var i in DS.Range(5)){
  97. Console.WriteLine(i);
  98. }
  99. }
  100. static void List_Map(){
  101. var l = DS.List(1,2,3,4).Map( e => e*e );
  102. Console.WriteLine(l.Format()); // => 1, 4, 9, 16
  103. var l2 = DS.ListHelper.Map( DS.Range(10), e => e*e );
  104. Console.WriteLine(l2.Format("'{0}'",",")); // => '0','1','4','9','16','25','36','49','64','81'
  105. }
  106. static void List_Inject(){
  107. // Sum the values from 0 to 9
  108. var l1 = DS.Range(10).Inject( (v,e) => v += e ) ;
  109. var l2 = DS.Range(10).Aggregate((v,e) => v += e ) ;
  110. Console.WriteLine( l1 );
  111. Console.WriteLine( l2 );
  112. }
  113. static void List_Filter(){
  114. var l = DynamicSugar.DS.Range(10).Filter(e => e % 2 == 0);
  115. Console.WriteLine( l.Format() );
  116. }
  117. // Here is a function returning 3 values
  118. static dynamic ComputeValues() {
  119. return DS.Values( new { a=1, b=2.0, c="Hello" } );
  120. }
  121. // Here is how to use the results
  122. static void MultiValuesSample() {
  123. var values = ComputeValues();
  124. Console.Write(values.a);
  125. Console.Write(values.b);
  126. Console.Write(values.c);
  127. }
  128. static void FormatMethodSample(){
  129. var firstName = "Fred";
  130. var age = 45;
  131. var s1 = String.Format("firstName:{0}, age:{1}", firstName, age);
  132. // Dynamic Sugar Syntax
  133. var s2 = "firstName:{firstName}, age:{age}".Template(new { firstName, age });
  134. Console.WriteLine(s1);
  135. Console.WriteLine(s2);
  136. Person p = new Person() {
  137. LastName = "TORRES" ,
  138. FirstName = "Frederic",
  139. BirthDay = new DateTime(1964,12, 11),
  140. DrivingLicenses = DS.List("Car", "Moto Bike")
  141. };
  142. //DrivingLicenses = new List<string>() { "Car", "Moto Bike" }
  143. Console.WriteLine( // Call 3 properties in the FormatString
  144. p.Format("FullName:'{LastName},{FirstName}', BirthDay:{BirthDay:MM/dd/yyyy}, DrivingLicenses:{DrivingLicenses}")
  145. );
  146. Console.WriteLine( // Call a method in the FormatString
  147. p.Format("LoadInformation:{LoadInformation()} ")
  148. );
  149. }
  150. static void FormatMethodWithDictionarySample(){
  151. var format = "LastName:{LastName}, FirstName:{FirstName}, Age:{Age:000}";
  152. var Values = new Dictionary<string, object>() {
  153. { "LastName" , "TORRES" },
  154. { "FirstName","Frederic" },
  155. { "Age" , 45 }
  156. };
  157. Console.WriteLine(ExtendedFormat.Format(format, Values));
  158. }
  159. static void FormatMethodWithExpandoObjectSample(){
  160. var format = "LastName:{LastName}, FirstName:{FirstName}, Age:{Age:000}";
  161. dynamic bag = new ExpandoObject() ;
  162. bag.LastName = "TORRES";
  163. bag.FirstName = "Frederic";
  164. bag.Age = 45;
  165. Console.WriteLine(ExtendedFormat.Format(format, bag));
  166. }
  167. static void DictMethodSample(){
  168. Person p = new Person() {
  169. LastName = "TORRES",
  170. FirstName = "Frederic",
  171. BirthDay = new DateTime(1964,12, 11)
  172. };
  173. foreach(var k in p.Dict()){
  174. Console.WriteLine("{0}='{1}'", k.Key, k.Value);
  175. }
  176. }
  177. private static void Pause() {
  178. Console.WriteLine("Press Enter");
  179. Console.ReadLine();
  180. }
  181. private static void Print(string f, params object [] defines){
  182. Console.WriteLine(String.Format(f, defines));
  183. }
  184. private static void ToFile_FromFile() {
  185. var l1 = DS.Range(10);
  186. //var fileName = String.Format(@"{0}\DSSharpLibrary_UnitTests.txt", Environment.GetEnvironmentVariable("TEMP"));
  187. var fileName = String.Format(@"{0}\DSSharpLibrary_UnitTests.txt", System.IO.Path.GetTempPath());
  188. l1.ToFile(fileName, true);
  189. var l2 = DS.ListHelper.FromFile<int>(fileName);
  190. DS.Assert.AreEqual(l1, l2);
  191. }
  192. private static void Include() {
  193. var l1 = DS.Range(10);
  194. if(l1.Include(5)){
  195. }
  196. if(l1.Include(1, 2, 3)){
  197. }
  198. if(l1.Include(DS.List(1, 2 , 3))){
  199. }
  200. }
  201. private static void Without() {
  202. var l1 = DS.Range(10);
  203. var l2 = l1.Without(0, 2, 4, 6, 8);
  204. var l3 = l1.Without(DS.List(0, 2, 4, 6, 8));
  205. Console.WriteLine(l2.Format()); // => 1, 3, 5, 7, 9
  206. Console.WriteLine(l3.Format()); // => 1, 3, 5, 7, 9
  207. }
  208. private static void First_Last_Rest() {
  209. var l1 = DS.Range(10);
  210. while(!l1.IsEmpty()){
  211. var first = l1.First();
  212. var last = l1.Last();
  213. l1 = l1.Rest();
  214. }
  215. }
  216. private static void Identical() {
  217. var l1 = DS.Range(10);
  218. var l2 = DS.Range(10);
  219. if(l1.Identical(l2)){
  220. }
  221. }
  222. public static void Pluck() {
  223. var people = DS.List(
  224. new Person() { LastName = "Descartes", FirstName = "Rene", BirthDay = new DateTime(1596,3,31) },
  225. new Person() { LastName = "Montesquieu", FirstName = "Charles-Louis", BirthDay = new DateTime(1689,1,18) },
  226. new Person() { LastName = "Rousseau", FirstName = "JJ", BirthDay = new DateTime(1712,3,31) }
  227. );
  228. Console.WriteLine(people.Pluck<int, Person>("Age").Format()); // => 415, 322, 299 in 2010
  229. }
  230. public static void Reject() {
  231. // Remove some elements based on an lambda expression
  232. Console.WriteLine(
  233. DS.Range(10).Reject(e => e % 2 == 0).Format()
  234. ); // => 1, 3, 5, 7, 9
  235. }
  236. }
  237. }