0

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?

Wazowski
  • 27
  • 4
  • So, how exactly do you want your `seq_gen` to differentiate between `next_fib` and `next_even` and understand how to call it? – yeputons Nov 18 '20 at 18:19
  • E.g. you can make it look at number of arguments that the function expects ([link](https://stackoverflow.com/a/41188411/767632)). Even though, I don't see how it's impossible to distinguish between two cases if a function with variable number of parameters is passed to `seq_gen`. – yeputons Nov 18 '20 at 18:22

0 Answers0