Ejemplo n.º 1
0
 public static void Execute(ITalkToUser ui)
 {
     var operation = ChooseOperation(ui);
     var results = Integers().Select(operation);
     var filteredResults = ChooseFilter(ui)(results);
     ui.Display(string.Join(", ", filteredResults));
 }
Ejemplo n.º 2
0
 static Func<int, int> ChooseOperation(ITalkToUser ui)
 {
     var operation = ui.Ask("choose operation");
     if( operation == "syracuse" )
     {
         var rank = int.Parse(ui.Ask("rank"));
         return i => SyracuseSequence(i).ElementAt(rank-1);
     }
     else if( operation == "integer sum" )
     {
         return i => i*(i+1)/2;
     }
     else
     {
         return i => i*i;
     }
 }
Ejemplo n.º 3
0
 static Func<IEnumerable<int>, IEnumerable<int>> ChooseFilter(ITalkToUser ui)
 {
     var filter = ui.Ask("choose filter");
     if( filter == "odd under" )
     {
         var maxValue = int.Parse(ui.Ask("max value"));
         return e => e.TakeWhile( n => n <= maxValue ).Where( n => n%2==1 );
     }
     else if( filter == "under" )
     {
         var maxValue = int.Parse(ui.Ask("max value"));
         return e => e.TakeWhile( n => n <= maxValue );
     }
     else
     {
         var numberOfItems = int.Parse(ui.Ask("number of items"));
         return e => e.Take(numberOfItems);
     }
 }