Move History

Fork Selected
  • Description

    create a function (0) that takes a function_ (1) and returns a decorator (2) that calls function_ (1) on the output of the func (3) the decorator (2) reads.

    Okay, that was confusing. We want to be able to apply a func to the output of another func, in a way that affects the func (e.g. it might be easiest to write a func using yield, but that causes issues if you want to memoize it and reuse the output)

    Code
    def add_func(operator):
        def inner(func):
            def thingy(*args, **kwargs):
                return operator( func(*args, **kwargs))
            return thingy
        return inner
    Test Cases
    import functools
    
    Test.assert_equals(add_func(tuple)(range)(5),  tuple(range(5)) )
    
    @functools.lru_cache(None)
    @add_func(list)
    def vals(n):
        for i in range(n):
            yield n+i
    
    
    Test.assert_equals(tuple(vals(5)),  tuple(vals(5)) ) 
    Test.assert_equals(vals(5),  list(range(5,10)) )