Page 1 of 1

RX (Reactive Extensions) Part 1 Rate Topic: -----

#1 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2262
  • View blog
  • Posts: 9,462
  • Joined: 29-May 08

Posted 17 March 2011 - 08:48 AM

RX (Reactive Extensions) Part 1


Reactive Extensions (Rx) is a library to compose asynchronous and event-based programs using observable reactive data sequences and LINQ-style query operators.

Also known as LINQ - To - Events.


Getting Started With RX

Firstly you need to go and download the library.
Downloads
DevLabs: RX



Project References
{ To be filled in }
{ Included Screenshots of .net references to include}

System.CoreEx

System.Reactive

System.Interactive.




Understanding the IEnumerable(Of T) / IObservable (Of T) Duality

It's really simple, let's imagine for while we're collection of eggs.

IEnumerable(Of Egg) The rest of your work is blocked until you get a Basket of eggs.

IObservable can be thought in two ways.

1. IObservable(Of Egg) You ask when whenever you have an egg. Tell me.
or
2. IObservable(Of Egg) Someone is throwing eggs at you to collect.

For a more in-depth explanation see.
Ch9 Videos (Video descriptions contain further links)




Generics Review

(Of Tin,Rout)

This tells the compiler, treat wherever I wrote Tin treat it as if i had used the type specified by Tin. Same for Rout.

Func(Of Tin,Rout)

Need an address of a function or a Lambda Function which has an Input parameter which is of the type specified by Tin and a return a type Rout.
The last generic type parameter of a Func is always the return type.
Eg.
Dim IncrementByOne As Func(Of Integer,Integer) = Function(x As Integer)
      Return x + 1
    End Function
Dim CharCount As Func(of String,Integer) = Function(s As String)
      Return s.Length
    End Function



Action(Of Tin)

Similar to a Func but doesn't return anything, so it more like a Subroutine.
Or in c-style languages returns Void.
So it needs either an address to subroutine (method) or a Lambda Statement (in-line sub)
Eg.
Dim OutputToConsole As Sub(Of Integer) = Sub(ByVal x As Integer) 
     Console.WriteLine( x )
   End Sub









Observable.Generate

Let's look at the signature (what's are its inputs and output) of the function.

Public Shared Function Generate(Of TState, TResult)(
  ByVal initialState As TState,
  ByVal condition As System.Func(Of TState, Boolean),
  ByVal resultSelector As System.Func(Of TState,
  ByVal iterate As System.Func(Of TState, TState)
TResult)
) As System.IObservable(Of TResult)

Understanding the parameters.
The first two are the generic type parameters.
 TState  What type are you using for your state.
TResult  What type your outputting

For the rest of the parameters, imagine you are providing the parameters for the follow section of code.

Dim xx = initialState
While condition(xx)
  Yield resultSelector(xx) ' This what you are outputting.
  xx = iterate(xx) ' How to calculate the next state / iteration value.
End While


Module Module1

    Sub Main()
    Dim TheObservable = Observable.Generate(Of Integer, Integer)(
        0,
            Function(xx) xx < 10,
         Function(xx) xx,
           Function(xx) xx + 1
       )
      Console.ReadKey()
  End Sub

End Module

Dim TheObservable = Observable.Generate(Of Integer, Integer)(
  0,
  Function(xx) xx < 10,
  Function(xx) xx,
  Function(xx) xx + 1
  )



Spoiler


Run this and it seem to do nothing, this because we need Observe it




Subscribe



Module Module1

    Sub Main()
    Dim TheObservable = Observable.Generate(Of Integer, Integer)(
        0,
            Function(xx) xx < 10,
         Function(xx) xx,
           Function(xx) xx + 1
       )

    Using TheSubscriber =  TheObservable.Subscribe(Sub(x)
                                    Console.WriteLine(x)
                                  End Sub)
      Console.WriteLine("Press any key to stop")
      Console.ReadKey()
    End Using
  End Sub

End Module



Why the Using?

The reason why I use the keyword Using is to do with unsubscribing and cleaning up of resources.





Playing with Time

I've slightly modified the previous example code.

Module Module1

    Sub Main()
    Dim TheObservable = Observable.Generate(Of Integer, Integer)(
        0,
            Function(xx) xx < 100,
         Function(xx)
           Console.WriteLine("Emitting: {0} at {1}", xx, Now)
           Return xx
         End Function,
         Function(xx) TimeSpan.FromMilliseconds(1500),
         Function(xx) xx + 1
       )

    Using  TheSubscriber = TheObservable.Subscribe(Sub(x)
                                    Console.WriteLine("Observed: {0} at {1}", x, Now)
                                  End Sub)
      Console.WriteLine("Press any key to stop")
      Console.ReadKey()
    End Using
  End Sub


End Module



Now the values appear every 1500ms (1.5 second).






Asyncronous Downloader


The follow example is based on the code from The Mechanical Bride Blog

Spoiler




Spoiler



Link to Part 2

This post has been edited by AdamSpeight2008: 17 March 2011 - 08:50 AM


Is This A Good Question/Topic? 3
  • +

Replies To: RX (Reactive Extensions) Part 1

#2 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1053
  • View blog
  • Posts: 4,080
  • Joined: 02-July 08

Posted 27 March 2011 - 07:23 PM

Adam, I have said it before, you are a madman!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1