Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Linq;
using System.Threading;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using RestSharp;

namespace ReactiveExtensions
    {
    class Program
        {
        static void Main(string[] args)
        {
            var start = Observable.Start(() =>
            {
                Console.WriteLine("Getting some work done");
                for (var i = 0; i < 10; i++)
                    {
                    Thread.Sleep(10000);
                    //go call stackoverflow
                    var d = CallStackOverFlow();
                    //send the result for checking to a delegate
                    foreach (var q in d.questions)
                        {
                            var list = (q.tags as JArray).Values().Select(v => v.ToString()).ToList();
                            var result = FindByTag(list);
                            if(result)
                                Console.WriteLine("Quesion title :{0}",q.title);
                        }
                 }
                return "I am done observing";
            });
            Console.WriteLine("Subscribing"); // Make visible when we subscribe
            start.Subscribe(Console.WriteLine);
            Console.ReadKey();
        }

        private static readonly Func<List<String>,bool> FindByTag =(x => x.Contains("c#"));

        private static dynamic CallStackOverFlow()
        {
            var client = new RestClient("http://api.stackoverflow.com/1.1");
            var request = new RestRequest("/questions/no-answers", Method.POST);
            var response = client.Execute(request);
            var content = response.Content; // raw content as string
            dynamic deserialised = JsonConvert.DeserializeObject(content);
            return deserialised;
        }
     }
  }

Please add the tag for ReactiveExtensions , my reputation does not allow me to do that.

share|improve this question
5  
Please describe what it does, what is it's purpose, and what features of Rx does it demonstrate. –  m0s Jan 15 '12 at 23:34
    
i am not sure exactly what features of Rx it demonstrates. I think i have created a pseudo stream , although i am not doing much work in an async manner . I think i should make the code do the checking while it waits for the next round of results. –  ashutosh raina Jan 16 '12 at 4:50
    
At least start by removing nesting levels. Extract the parts of each for loop to a method. Consider whether parameters should be fields. It will make each part of your logic easier to maintain and read. –  Lars-Erik Jan 25 '12 at 10:35
add comment

1 Answer 1

possible improvements :

  1. make CallStackOverflow asynchronous
  2. use Observavle.Timer instead of thread.sleep

Here is what it would look like

class Program
{
   static void Main(string[] args)
   {

        Observable
        .Timer(TimeSpan.Zero,TimeSpan.FromSeconds(10))
        .Take(10)
        .Select(() => CallStackOverFlow().ToObservable())
        .Select(d => {
             //send the result for checking to a delegate
               foreach (var q in d.questions)
                   {
                       var list = (q.tags as JArray).Values().Select(v => v.ToString()).ToList();
                       var result = FindByTag(list);
                       if(result)
                           Console.WriteLine("Quesion title :{0}",q.title);
                   }
         })
         .Wait();

       Console.WriteLine("Subscribing"); // Make visible when we subscribe
       start.Subscribe(Console.WriteLine);
       Console.ReadKey();
   }

   private static readonly Func<List<String>,bool> FindByTag =(x => x.Contains("c#"));

   private static async  Task<dynamic> CallStackOverFlow()
   {
       var client = new RestClient("http://api.stackoverflow.com/1.1");
       var request = new RestRequest("/questions/no-answers", Method.POST);
       // -- async invocation
       var response = await client.ExecuteAsync(request); 
       // or  var response = await Task.Factory.FromAsync<TResponse>(client.BeginExecute, client.EndExecute, null);
       var content = response.Content; // raw content as string
       dynamic deserialised = JsonConvert.DeserializeObject(content);
       return deserialised;
   }
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.