Last active
August 29, 2015 14:20
-
-
Save rawman/aba3456a34e098aca52a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| foreach(var user in users) | |
| { | |
| Console.WriteLine(user.Name); | |
| } | |
| users.ToObservable() | |
| .Subscribe(x => Console.WriteLine(x.Name)) | |
| public interface IEnumerable<T> | |
| { | |
| IEnumerator<T> GetEnumerator(); | |
| } | |
| public interface IEnumerator<T> : IDisposable | |
| { | |
| bool MoveNext(); | |
| T Current { get; } | |
| } | |
| public interface IObservable<T> | |
| { | |
| IDisposable Subscribe(IObserver<T> observer); | |
| } | |
| public interface IObserver<T> | |
| { | |
| void OnCompleted(); | |
| void OnError(Exception exception); | |
| void OnNext(T value); | |
| } | |
| public interface IEnumerator<T> : IDisposable | |
| { | |
| bool MoveNext(); //throws exception | |
| T Current { get; } | |
| } | |
| public interface IEnumerator<T> : IDisposable | |
| { | |
| (bool | Exception) MoveNext(); | |
| T Current { get; } | |
| } | |
| IObservable<TextChangedEvent> changedEvents = ...; | |
| var input = changedEvents.Select(x => x.TextBox.Text) | |
| .Where(x => x.Lenght > 5) | |
| .Throttle(TimeSpan.FromSeconds(1)) | |
| input.Subscribe() | |
| Observable.Empty<string>() //empty | |
| // Observe mouse events | |
| var mouseMove = GetMouseMove(); | |
| var mouseUp = GetMouseUp(); | |
| var mouseDown = GetMouseDown(); | |
| // Calculate Movement and Display | |
| var q = from start in mouseDown | |
| from delta in mouseMove.StartWith(start).TakeUntil(mouseUp) | |
| .Let(mm => mm.Zip(mm.Skip(1), (prev, cur) => | |
| new { X = cur.X - prev.X, Y = cur.Y - prev.Y })) | |
| select delta; | |
| q.Subscribe(value => | |
| { | |
| Canvas.SetLeft(image,Canvas.GetLeft(image) + value.X); | |
| Canvas.SetTop(image,Canvas.GetTop(image) + value.Y); | |
| })); | |
| Task<Contact> GetContact(int id); | |
| IObservable<Contact>() GetContact(int id); | |
| api.GetContact(1) | |
| IObservable<Contact>() GetContact(int id); | |
| IObservable<string> Translate(string word); //make a remote call and translate | |
| var changedEvents = GetTextChangedEvents(); | |
| var input = changedEvents.Select(x => x.TextBox.Text) | |
| .Throttle(TimeSpan.FromSeconds(1)) | |
| var translations = input.SelectMany(x => Translate(x)); | |
| IObservable<string> Translate(string word); //remote call | |
| var changedEvents = GetTextChangedEvents(); | |
| var input = changedEvents.Select(x => x.TextBox.Text) | |
| .Throttle(TimeSpan.FromSeconds(1)) | |
| var translations = from term in input | |
| where term.Length > 5 | |
| select Translate(term) | |
| service.Translate("wat") | |
| .Timeout(TimeSpan.FromMiliseconds(500)) | |
| .Retry(3) | |
| .Finally(e => Logger.LogError(e)) | |
| await ToObservable(_messageQueue, InternalRouting.SyncQueue(syncId)) | |
| .Timeout(AggreagationConfiguration.SyncTimeout) | |
| .Timeout(x => Observable.Timer(GetMessageTimeout(x))) | |
| .Where(x => x.DataType != typeof(SyncCompletedMessage)) | |
| .Buffer(AggreagationConfiguration.BufferTimeout, AggreagationConfiguration.BufferSize) | |
| .Do(PublishBatch); | |
| public IUserApi | |
| { | |
| Task<User> GetUser(int id); | |
| Task<IEnumerable<User>> GetUsers(); | |
| } | |
| public IUserApi | |
| { | |
| IObservable<User> GetUser(int id); | |
| IObservable<User> GetUsers(); | |
| } | |
| //pagination | |
| public interface IUserApi | |
| { | |
| Task<User> GetUser(int id); | |
| Task<Page<User>> GetUsers(); | |
| } | |
| api.GetUsers() | |
| .ContinueWith(x => | |
| { | |
| ProcessPage(x.Results.Items) | |
| if (!x.Result.IsLast) | |
| return GetNextPage(api, results); | |
| return Enumerable.Empty<User>(); | |
| }); | |
| var page = new Page<User> {IsLast = false}; | |
| while (page.IsLast) | |
| { | |
| page = await api.GetUsers(); | |
| ProcessPagepage.Items) | |
| } | |
| public interface IUserApi | |
| { | |
| IObservable<User> GetUser(int id); | |
| IObservable<IEnumerable<User>> GetUsers(); | |
| } | |
| api.GetUsers() | |
| Subscribe(ProcessPage) | |
| public interface IScheduler | |
| { | |
| IDisposable Schedule<TState>(TState state, Func<IScheduler, TState, IDisposable> action); | |
| } | |
| [TestMethod] | |
| public void Testing_with_test_scheduler() | |
| { | |
| var expectedValues = new long[] {0, 1, 2, 3, 4}; | |
| var actualValues = new List<long>(); | |
| var scheduler = new TestScheduler(); | |
| var interval = Observable | |
| .Interval(TimeSpan.FromSeconds(1), scheduler) | |
| .Take(5); | |
| interval.Subscribe(actualValues.Add); | |
| scheduler.Start(); | |
| CollectionAssert.AreEqual(expectedValues, actualValues); | |
| } | |
| IObservable<TResult> SelectMany<TSource,TResult<( | |
| this IObservable<TSource> source. | |
| Func<TSource, IObservable<TResult>> selector) | |
| IObservable<TResult> SelectMany<TSource, TResult>( | |
| this IObservable<TSource> source, | |
| Func<TSource, IObservable<TResult>> selector) | |
| weather.Where(x => x.City = "Kraków") | |
| .Where(x => Math.Abs(x.Temp.Tooday - x.Temp.Tomorrow) > 5) | |
| .Subscribe(DisplayNotification) | |
| wheater.Where(x => x.City = "Kraków" | |
| .GropuBy(x => ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment