2

I downloaded RxPY and was watching the Rx tutorials when I came across:

enter image description here

So how would I get the above working in Python with RxPY? In particular, the query q = from x in xs where... is not syntactically valid Python code -- so how would this be changed?

mchen
  • 9,808
  • 17
  • 72
  • 125

2 Answers2

3

All LINQ queries in C# could be easily converted to a extension methods (where is for Where, and select is for Select):

In [20]: from rx import Observable

In [21]: xs = Observable.range(1, 10)

In [22]: q = xs.where(lambda x: x % 2 == 0).select(lambda x: -x)

In [23]: q.subscribe(print)
-2
-4
-6
-8
-10

You may also use filter instead of where and map instead of select:

In [24]: q = xs.filter(lambda x: x % 2 == 0).map(lambda x: -x)

In [25]: q.subscribe(print)
-2
-4
-6
-8
-10
awesoon
  • 32,469
  • 11
  • 74
  • 99
  • Brilliant -- but how did you know about this? Where is this documented? – mchen Dec 19 '15 at 07:27
  • 2
    Unfortunately, I cannot find actual documentation for `Rx` in Python, however, I supposed, that the same methods should have the same (or similar) names across all implementations of the `Rx` library. I also visited [RxPY github](https://github.com/ReactiveX/RxPY/) and looked through the source code. – awesoon Dec 19 '15 at 07:32
0

The picture you've shown has code in C#, not Python. In Python it would be as follows:

from rx import Observable
xs = Observable.range(1, 10)
q = xs.filter(lambda x: x % 2 == 0).map(lambda x: -x)
q.subscribe(print)

The general documentation could be found at documentation section at http://reactivex.io. The Python specific docs are at https://github.com/ReactiveX/RxPY/blob/master/README.md.

Lev Romanov
  • 704
  • 6
  • 3