2
\$\begingroup\$
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.

\$\endgroup\$
3
  • 5
    \$\begingroup\$ Please describe what it does, what is it's purpose, and what features of Rx does it demonstrate. \$\endgroup\$
    – m0s
    Commented Jan 15, 2012 at 23:34
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Jan 16, 2012 at 4:50
  • \$\begingroup\$ 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. \$\endgroup\$
    – Lars-Erik
    Commented Jan 25, 2012 at 10:35

1 Answer 1

3
\$\begingroup\$

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;
   }
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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