Exemple #1
0
        public static async Task <int> MainAsync()
        {
            Console.WriteLine("App thread {0}", Thread.CurrentThread.ManagedThreadId);
            CancellationTokenSource cts        = new CancellationTokenSource();
            FilterStringList        oddFilter  = new FilterStringList(OddFilter);
            FilterStringList        evenFilter = new FilterStringList(EvenFilter);

            FilterStringList[] filters = { oddFilter, evenFilter };
            EventSimulator.Start();
            StartSensors();

            Task <List <string> > collectData = StartCollectionInterval(cts);
            await Task.Delay(2000);

            cts.Cancel();              //propogate cancellation among async operations -- which causes return of that task
            await         collectData; //now I do the actual awaiting
            List <string> collectionOfData = collectData.Result;

            // if ( collectionOfData.IsCompleted ) {
            //     Console.WriteLine("THIS IS THE RESULT" + string.Join(",", collectionOfData.Result) );
            // }



            string[] allResults = collectionOfData.ToArray();

            JObject[] jsonObjs = allResults.Select(s => JObject.Parse(s)).ToArray();

            foreach (JObject o in jsonObjs)
            {
                // Console.WriteLine(o.ToString());s
                // JToken data = o.GetValue("data");

                JArray eventArray = (JArray)o["data"];

                //Are these auto optimized? or do i needs a transducer
                List <JObject> importantEvents = eventArray
                                                 .Where(ev => ev.ToString().Length > 0)      //make sure the jtoken was not a newline
                                                 .Select(ev => JObject.Parse(ev.ToString())) //JToken -> Jobject
                                                 .Where(Filter.IsImportantEvent)             //Filter out "non-important" events
                                                 .ToList();


                if (importantEvents.Count > 0)
                {
                    Console.WriteLine(JsonConvert.SerializeObject(importantEvents));
                }

                // bool test = Filter.IsImportantEvent(ev);
                // Console.WriteLine(ev + " " + test);
                Console.WriteLine("------------------------");
            }

            // Console.WriteLine( string.Join(",",allResults) + "\n\n" + jsonObjs[1].ToString());


            return(0);
        }
        public static void Demo()
        {
            Add add4 = AddFour;
            Add add3 = AddThree;
            Add sub4 = SubtractFour;

            Console.WriteLine("should be 8 : " + add4(4));
            Console.WriteLine("should be 6 : " + add3(3));

            Console.WriteLine("Should be 7 : " + AddAdd(add4, add3, 0));

            //subtract has the same signature so I can use it as well
            Console.WriteLine("Should be 0 : " + AddAdd(add4, sub4, 0));



            List <string> myStringList = new List <string>()
            {
                "test", "strings", "are", "interesting?"
            };

            FilterStringList oddFilterFn  = new FilterStringList(OddFilter);
            FilterStringList evenFilterFn = new FilterStringList(EvenFilter);


            FilterStringList oddFilterFn_func = new FilterStringList(OddFilter_func);


            FilterStringList[] filterArray = { oddFilterFn, evenFilterFn, oddFilterFn_func };


            // Predicate<string> isEvenLength = s => s.Length % 2 == 0;

            Func <string, bool> isEvenLength = s => s.Length % 2 == 0;

            string[] areEven = myStringList.Where(isEvenLength).ToArray();

            Console.WriteLine(" areEven : " + string.Join(",", areEven));
        }