Rx Let function
Ive been investiging the Rx library and have tried to replicate the example from the following video...
http://channel9.msdn.com/blogs/j.van.gogh/writing-your-first-rx-application
it all works (with some modifications to things that have been changed/deprecated) up until he used...
.Let(mm => ...)
开发者_开发技巧
This throws a compiler error saying that there is no definition for let, so I assume that Let has been changed to something else, or removed completely, but I cant find any solutions from a googling.
So does anybody know what to use in this instance?
As per Jim Wooley's suggestion.
I think the code you are looking at is
var q = from start in mouseDown
from delta in mouseMove.StartWith(start).Until(mouseUp)
.Let(mm=> mm.Zip(mm.Skip(1), (prev, curr) =>
new { X = curr.X - prev.X, Y = curr.Y - prev.Y}))
select delta;
Remember that was written in 2009 and Rx has move along some since then. I think this is what you want. I think the Let is a feature you want to avoid (even if available to you) in Rx as it can encourage side effects. Use transformation with Select instead. In the case below, the let is just not needed.
//Gets the delta of positions.
var mouseMovements = mouseMove.Zip(mouseMove.Skip(1), (prev, curr) =>
new { X = curr.X - prev.X, Y = curr.Y - prev.Y}));
//Only streams when mouse is down
var dragging = from md in mouseDown
from mm in mouseMovement.TakeUntil(mouseUp)
select mm;
Try using another .Select and project a type that includes both your new variable and the incoming observable value.
You are probably trying to use EnumerableEx.Let which has been removed from the current Experimental Version of the Interactive Extensions. They'll put it back in.
In the meantime you can define it yourself easily like so:
public static class EnumerableExx
{
public static U Let<T,U>(this T source, Func<T,U> f)
{
return f(source);
}
}
Note that this version works with any type not just IEnumerable.
You could also use Memoize(...) instead which can be better as it caches the collection on the left hand side.
Let
just allows you to give a expression its own name so you can reuse it later without having to use a local variable. You can always split an Rx expression into pieces and assign the pieces to local variables. Can you post the original query and I'll show you how to de-Let'ify it?
Let still exists, does this not work for you?
var observable = new Subject<int>();
observable.Let(mm => mm);
Perhaps you forgot a Namespace import? (System.Reactive.Linq) ?
精彩评论