Rx Workshop: Reactive Coincidence
- Posted: Jul 11, 2011 at 12:05 PM
- 27,351 Views
- 11 Comments
Loading User Information from Channel 9
Something went wrong getting user information from Channel 9
Loading User Information from MSDN
Something went wrong getting user information from MSDN
Loading Visual Studio Achievements
Something went wrong getting the Visual Studio Achievements
Right click “Save as…”
Learn how to model events with duration and how to use the LINQ Join operator to express complex queries involving coincidence.
Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation,
please create a new thread in our Forums,
or
Contact Us and let us know.
Follow the Discussion
Oops, something didn't work.
What does this mean?
Following an item on Channel 9 allows you to watch for new content and comments that you are interested in. You need to be signed in to Channel 9 to use this feature.What does this mean?
Following an item on Channel 9 allows you to watch for new content and comments that you are interested in and view them all on your notifications page.sign up for email notifications?
awsome stuff..
Am I allowed to post the solution of the challange?
Benny
Here is what I came up with
var query = Observable.Join( mouseDown, mouseMove, _ => mouseUp, _ => Observable.Empty<Unit>(), (_, r) => r) .Publish(evt => evt.Zip(evt.Skip(1), (previous, current) => Subtract(current, previous)));I am having difficulty translating this into query comprehension syntax. Anyone figure that out? Thanks.
Alright here is a working query comprehension version
var query = from start in mouseDown join move in mouseMove on mouseUp equals Observable.Empty<Unit>() into moveGroup let drag = moveGroup.Zip(moveGroup.Skip(1), (prev, curr) => Subtract(curr, prev)) from delta in drag select delta;Still curious if this could be cleaner. Thanks.
Here is my solution:
var query = Observable.Join(
mouseDown,
mouseMove.Buffer(2),
_ => mouseUp,
m => Observable.Empty<Unit>(),
(_, r) => Subtract(r.Last(),r.First()));
I think my solution is close to the optimum!?
But I had to modify the subscibtion from deltas to absolute points.
var query = mouseDown.Join(mouseMove, l => mouseUp, r => Observable.Empty<Point>(),(l,r)=> Subtract(r,l)); query.Subscribe(pos=> { Canvas.SetLeft(image, pos.X); Canvas.SetTop(image, pos.Y); });I notied there is a bug when releasing the mouse button from outside of the window. Here is a fix that will merge mouseUp and mouseLeave to accomplish the task. What is normal UI guidance for drag and drop when going outside the window? Thanks.
var mouseLeave = from evt in Observable.FromEventPattern<MouseEventHandler, MouseEventArgs>(h => MouseLeave += h, h => MouseLeave -= h) select evt.EventArgs.GetPosition(this); var query = from start in mouseDown join move in mouseMove.Buffer(2) on mouseUp.Merge(mouseLeave) equals Observable.Empty<Unit>() select Subtract(move.Last(), move.First());Hey what happened to the challenges. They are showing up 404. I was trying to dowload again. Thanks.
Here is my (edited) suggestion:
The Subtract method is not needed.
Here is a solution that uses the Scan method:
var query = mouseDown.Join(
mouseMove
.Scan(new { Prev = new Point(), Delta = new Point() },
(current, next) => new { Prev = next, Delta = Subtract(next, current.Prev) })
.Select(x => x.Delta),
down => mouseUp,
delta => Observable.Empty<Unit>(),
(down, delta) => delta);
Remove this comment
Remove this thread
close