def test_window_sum(self): res = [] rx.from_(range(6)).pipe( ops.window_with_count(count=3, skip=1), ops.flat_map(lambda i: i.pipe(ops.sum(), )), ).subscribe(on_next=res.append) assert res == [3, 6, 9, 12, 9, 5, 0]
def _solve(print=print): total = rx.from_iterable(primes()) \ .pipe( ops.take_while(lambda p: p < 2000000), ops.sum(), ).run() print(f'The sum of primes below 2m: {total}') return True
def _sum(key_mapper: Optional[Mapper] = None) -> Callable[[Observable], Observable]: if key_mapper: return pipe( ops.map(key_mapper), ops.sum() ) return ops.reduce(seed=0, accumulator=lambda prev, curr: prev + curr)
def _solve(print: t_print = print) -> bool: limit = 1000 total = rx.range(limit) \ .pipe( ops.filter(lambda n: n % 3 == 0 or n % 5 == 0), ops.sum(), ).run() print(total) return True
def run_metaepoch(self): node_jobs = [] for node in self.level_nodes[2]: node_jobs.append(node.run_metaepoch()) for node in self.level_nodes[1]: node_jobs.append(node.run_metaepoch()) for node in self.level_nodes[0]: node_jobs.append(node.run_metaepoch()) # _plot_node(node, 'r', [[0, 1], [0, 3]]) node_costs = [] for node_job in node_jobs: node_job.pipe( ops.subscribe_on(NewThreadScheduler()), ops.map(lambda message: self._update_cost(message)), ops.sum(), ops.do_action( on_next=lambda cost: node_costs.append(cost))).run() # self.cost += max(node_costs) self.cost += sum(node_costs)
import rx import rx.operators as ops numbers = rx.from_([1, 2, 3, 4]) numbers.pipe(ops.sum()).subscribe( on_next=lambda i: print("on_next {}".format(i)), on_error=lambda e: print("on_error: {}".format(e)), on_completed=lambda: print("on_completed"))
# Find the sum of all the multiples of 3 or 5 below 1000. from rx import from_list, operators as op max_number = 1000 # Legacy method multiples = [] for number in range(max_number): if (number % 3 == 0 or number % 5 == 0): multiples.append(number) print(sum(multiples)) # With reactive programming from_list(range(max_number)).pipe( op.filter(lambda n: n % 3 == 0 or n % 5 == 0), op.sum()).subscribe(lambda s: print(s))
"""this operator will return an observable with the sum of all the values from source observables. :parameter key_mapper: optional. this is the function, that is applied to the values coming from the source observable. :return it returns an observable with the sum of all the values from the source observable. """ from rx import of, operators as op of(1,2,3,4,5,6,7,8,9,10)\ .pipe( # op.sum() # the sum is 55 op.sum(lambda n: n + 1) # the sum is 65 ).subscribe(lambda x: print("the sum is {}".format(x)))
# Example of summing all the values in a data stream import rx from rx import operators as op # Set up a source observable = rx.from_list([2, 3, 5, 7]) # Apply sum function to initial source observable2 = observable.pipe(op.sum()) # Subscribe to the result generated by sum observable2.subscribe(lambda v: print(v)) print('-' * 20) # Rolling or incremental sum rx.from_([2, 3, 5, 7]).pipe( op.scan(lambda subtotal, i: subtotal + i)).subscribe(lambda v: print(v))
def create(): return xs.pipe(ops.sum(lambda x: len(x)))
def create(): return xs.pipe(ops.sum())
rx.from_() rx.interval() rx.just() rx.range() rx.repeat_value() rx.start() rx.timer() """Mathematical""" op.average() op.concat() op.count() op.max() op.min() op.reduce() op.sum() """Transformation""" op.buffer() op.group_by() op.map() op.scan() # ... """Filtering""" op.debounce() op.distinct() op.filter() op.element_at() op.first() op.ignore_elements()
# By considering the terms in the Fibonacci sequence whose values do not # exceed four million, find the sum of the even-valued terms. MAX_VALUE = 4e6 # Legacy method def fill_fibonacci(max_value): fib_list = [1, 1] increment = 1 while (True): fib_list.append(fib_list[increment - 1] + fib_list[increment]) increment = increment + 1 if (fib_list[increment] > max_value): fib_list.pop() break return fib_list print(sum([num for num in fill_fibonacci(MAX_VALUE) if num % 2 == 0])) # With reactive programming from rx import from_list, operators as op source = from_list(fill_fibonacci(MAX_VALUE)) source.pipe(op.filter(lambda x: x % 2 == 0), op.sum()).subscribe(lambda i: print(i))
import rx from rx import operators as ops pipe = rx.range(1, 11) \ .pipe(ops.filter(lambda i: i % 2 == 0), ops.filter(lambda i: i < 3), ops.map(lambda i: i + i), ops.map(lambda i: i ** i), ops.average(lambda x: x + x), ops.sum()) pipe.subscribe(lambda x: print("value is {0}".format(x)))