So I try to create function which can generate subsequent words of the sequence.
For example:
I have function like this:
def next_even(a):
return a + 2
And i Have to build generator like this:
def seq_gen(fun,*args):
counter = 0
x=args[counter]
while(True):
yield x
x = fun(x)
#x=1,y=1
#x=1,y=2
#x=2,y=3
g = seq_gen(next_even,0);
When I use print(next(g))
It will return:
0;
2;
4;
6;
...
But I try to create sen_gen()
which can work with others functions.
For example fibonacci :
def next_fib(a,b):
return a+b
def seq_gen(fun,*args):
counter = 0
x=args[counter]
y=args[counter+1]
while(True):
yield x
x, y = y, fun(x,y)
#x=1,y=1
#x=1,y=2
#x=2,y=3
g = seq_gen(next_fib,0,1);
When I use print(next(g))
It will return:
0;
1;
1;
2;
3;
5;
.....
What should I use to create unique sen_gen()
which can work with variable types of functions?