|
18 | 18 | (currently, the Fibonacci numbers and the prime numbers). |
19 | 19 | """ |
20 | 20 |
|
21 | | -__all__ = ["s", "m", "almosteq", |
| 21 | +__all__ = ["s", "m", "mg", "almosteq", |
22 | 22 | "sadd", "ssub", "sabs", "spos", "sneg", "sinvert", "smul", "spow", |
23 | 23 | "struediv", "sfloordiv", "smod", "sdivmod", |
24 | 24 | "sround", "strunc", "sfloor", "sceil", |
|
27 | 27 | "fibonacci", "primes"] |
28 | 28 |
|
29 | 29 | from itertools import repeat, takewhile, count |
| 30 | +from functools import wraps |
30 | 31 | from operator import add as primitive_add, mul as primitive_mul, \ |
31 | 32 | pow as primitive_pow, mod as primitive_mod, \ |
32 | 33 | floordiv as primitive_floordiv, truediv as primitive_truediv, \ |
@@ -507,6 +508,23 @@ def __ror__(self, other): |
507 | 508 | return sor(other, self) |
508 | 509 | # TODO: conversion (bool, complex, int, float) and comparison operators? Do we want those? |
509 | 510 |
|
| 511 | +def mg(gfunc): |
| 512 | + """Decorator: make gfunc m() the returned generator instances. |
| 513 | +
|
| 514 | + Return a new gfunc, which passes all its arguments to the original ``gfunc``. |
| 515 | +
|
| 516 | + Example:: |
| 517 | +
|
| 518 | + a = mg(imemoize(s(1, 2, ...))) |
| 519 | + assert last(take(5, a())) == 5 |
| 520 | + assert last(take(5, a())) == 5 |
| 521 | + assert last(take(5, a() + a())) == 10 |
| 522 | + """ |
| 523 | + @wraps(gfunc) |
| 524 | + def mathify(*args, **kwargs): |
| 525 | + return m(gfunc(*args, **kwargs)) |
| 526 | + return mathify |
| 527 | + |
510 | 528 | # The *settings mechanism is used by round and pow. |
511 | 529 | # These are recursive to support iterables containing iterables (e.g. an iterable of math sequences). |
512 | 530 | def _make_termwise_stream_unop(op, *settings): |
|
0 commit comments