# Is this a good example of Reactive Extensions? How can I make this better?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Linq;
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++)
{
//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);
}

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.

-
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

possible improvements :

1. make CallStackOverflow asynchronous

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);
}

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