none
How to use Let and what is the LINQ "let" equivalent in Rx?

    Question

  • We need a good example of Let.

    The example that was shown in the video was a bit confusing:

    .Let ( x' => x'.Concat(x') )

     

    What that can mean?  Does this creates an infinite observer because it'll concat the X to X infinitely?

     

    What is the equivalent for "let" that we have in LINQ?

     

    for x in observer1
    let b = x*2
    select x + b
    
    

     

    How's the query above can be expressed in Rx chained representation? Is there a way to define a temp values in the sequence?


    See my blog: http://igorshare.wordpress.com
    Tuesday, March 30, 2010 2:37 PM

Answers

  • Hi,

    > The example that was shown in the video was a bit confusing: [snip]

    Observable.Let is used to compose an observable while sharing creation side-effects.  For example, if x is defined as var x = GetObservable(), then you could just use GetObservable().Concat(GetObservable()) instead.  But GetObservable() would have to be executed twice.  Or, you could use Observable.Let to bind the result of GetObservable() to a variable and then reuse the variable: GetObservable().Let(x => x.Concat(x)).

    You could essentially do this with a local variable instead if you're writing imperative code:

    var ob = GetObservable();
    ob = ob.Concat(ob);

    But what if you're not writing imperative code?  It's harder to do this in the middle of a LINQ query.  That's where Observable.Let really shines.

    var query = 
      from x in GetRootObservable()
      from y in GetObservable(x).Let(ys => ys.Concat(ys))
      select y;


    > Does this creates an infinite observer because it'll concat the X to X infinitely?

    No, it only concatenates x to itself once.  If x is equal to { 1, 2, 3 }, then the result of x.Concat(x) is equivalent to { 1, 2, 3, 1, 2, 3 }.


    > What is the equivalent for "let" that we have in LINQ?

    It's the same in reactive queries as it is for interactive queries.

    var query = from x in GetObservable()
                     let b = x * 2
                     select x + b;

    - Dave


    http://davesexton.com/blog
    Wednesday, March 31, 2010 1:11 AM

All replies

  • Hey Dude,

    let in LINQ's query comprehension syntax actually maps to an anonymous type created by the compiler.

    from i in Enumerable.Range(1,10)
    let c = i.ToString()
    select c

    Is roughly the same as;

    Enumerable.Range(1,10)
    .Select(i => new {i, c = i.ToString()})
    .Select(t => t.c)

     

    Tuesday, March 30, 2010 4:02 PM
  • Hi,

    > The example that was shown in the video was a bit confusing: [snip]

    Observable.Let is used to compose an observable while sharing creation side-effects.  For example, if x is defined as var x = GetObservable(), then you could just use GetObservable().Concat(GetObservable()) instead.  But GetObservable() would have to be executed twice.  Or, you could use Observable.Let to bind the result of GetObservable() to a variable and then reuse the variable: GetObservable().Let(x => x.Concat(x)).

    You could essentially do this with a local variable instead if you're writing imperative code:

    var ob = GetObservable();
    ob = ob.Concat(ob);

    But what if you're not writing imperative code?  It's harder to do this in the middle of a LINQ query.  That's where Observable.Let really shines.

    var query = 
      from x in GetRootObservable()
      from y in GetObservable(x).Let(ys => ys.Concat(ys))
      select y;


    > Does this creates an infinite observer because it'll concat the X to X infinitely?

    No, it only concatenates x to itself once.  If x is equal to { 1, 2, 3 }, then the result of x.Concat(x) is equivalent to { 1, 2, 3, 1, 2, 3 }.


    > What is the equivalent for "let" that we have in LINQ?

    It's the same in reactive queries as it is for interactive queries.

    var query = from x in GetObservable()
                     let b = x * 2
                     select x + b;

    - Dave


    http://davesexton.com/blog
    Wednesday, March 31, 2010 1:11 AM
  • Great job Dave!
    Friday, April 02, 2010 11:52 PM