0

I want to write a wrapper function for eternity that will make it behave as if it's being lazily evaluated. In other words, its functionality should be made identical to that of the function lazyeternity below. I have this function packageN that takes a function and packages it into a lambda--or at least I think it does. (Packaging lazyeternity inside a lambda delayed its evaluation in the call of etest.) How can I modify packageN so that eternity = packageN(eternity) can mirror the behavior of lazyeternity?

    def eternity():
        return eternity()

    # How can I create a wrapper function for eternity...
    # that will make it behave identical to this function?
    def lazyeternity():
        return lambda: lazyeternity()

    def packageN(f):
        return lambda *x: f(*x)

    def etest(x, y):
        return x

    eternity = packageN(eternity)

    # I want these both to return 4.
    # Currently only the first one returns 4,...
    # because the second one evaluates the eternity()...
    # parameter forever. I want to delay the evaluation...
    # of the parameter.
    print etest(4, lazyeternity())
    print etest(4, eternity())
2
  • Can you add a more specific example of how you want to use this? The code in your question doesn't make a whole lot of sense. Commented Jun 5, 2016 at 22:11
  • Thanks, @Håken. I tried to clarify a bit, but let me know if I can be of more help. I want to be able to apply packageN to any function f to make it so that f is evaluated only when needed. Commented Jun 6, 2016 at 2:46

2 Answers 2

2

The difference between lazyeternity and eternity as returned by packageN is that calling lazyeternity returns a lambda, whereas eternity is a lambda which, when called, runs forever.

To make packageN return something that acts like lazyeternity, make it

def packageN(f):
    return lambda: lambda *x: f(*x)
Sign up to request clarification or add additional context in comments.

4 Comments

Wow, thank you, this works!! But wasn't packageN returning a lambda before? If I print eternity after running eternity = packageN(eternity) using either my version of packageN or your version, it says the object is a lambda.
packageN was returning a lambda, which is assigned to eternity, so calling eternity executed that lambda. As opposed to calling lazyeternity which just gives you a lambda (and doesn't execute it). So to do the same, packageN must return a lambda which returns a lambda.
Would this work with higher-order functions? I'm trying to wrap the lazy definition of the Y combinator, def Y(f): return f( Y(f) ) in such a way.
Sure, why not? ¯\_(ツ)_/¯
1

To make both eternity work, simply remove the (). You want to return a function, not the evaluation of a function (infinite loop).

1 Comment

eternity is not supposed to work in the context of etest. I want a wrapper function to modify it so that etest can work with eternity as an argument if eternity is not being used (i.e. it is the second argument in etest).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.