Python Help -- Apply a procedure repeatedly

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • boapython
    New Member
    • Feb 2007
    • 4

    Python Help -- Apply a procedure repeatedly

    Hi, I'm trying to write a function that takes in as arguments a procedure with one argument and an integer. The integer represents how many times the procedure will execute on the argument.

    Code:
    def compose(f,g):
        return lambda x: f(g(x))
    
    
    def repeatedlyApply (p,n):
        i=0
        q=0
        while (i<n):
            q = compose(p,p)
            i=i+2
        return q
    This is what I had in mind... I had q = q + compose(p,p) but it tells me it can't add a function and an integer. Which I see. But when I have what's written above, it returns the value of q and not a memory location.

    I'm a brand new programmer... Any ideas would be greatly appreciated.
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #2
    Originally posted by boapython
    Hi, I'm trying to write a function that takes in as arguments a procedure with one argument and an integer. The integer represents how many times the procedure will execute on the argument.

    Code:
    def compose(f,g):
        return lambda x: f(g(x))
    
    
    def repeatedlyApply (p,n):
        i=0
        q=0
        while (i<n):
            q = compose(p,p)
            i=i+2
        return q
    This is what I had in mind... I had q = q + compose(p,p) but it tells me it can't add a function and an integer. Which I see. But when I have what's written above, it returns the value of q and not a memory location.

    I'm a brand new programmer... Any ideas would be greatly appreciated.
    I'm going to look at your code when I get a break. I just wanted to whip off a quick note to welcome you and say "for a beginner you sure got a great handle". Thanks for joining,
    Barton

    Comment

    • boapython
      New Member
      • Feb 2007
      • 4

      #3
      Originally posted by bartonc
      I'm going to look at your code when I get a break. I just wanted to whip off a quick note to welcome you and say "for a beginner you sure got a great handle". Thanks for joining,
      Barton
      Thanks a lot, Barton!

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        Originally posted by boapython
        Hi, I'm trying to write a function that takes in as arguments a procedure with one argument and an integer. The integer represents how many times the procedure will execute on the argument.

        Code:
        def compose(f,g):
            return lambda x: f(g(x))
        
        
        def repeatedlyApply (p,n):
            i=0
            q=0
            while (i<n):
                q = compose(p,p)
                i=i+2
            return q
        This is what I had in mind... I had q = q + compose(p,p) but it tells me it can't add a function and an integer. Which I see. But when I have what's written above, it returns the value of q and not a memory location.

        I'm a brand new programmer... Any ideas would be greatly appreciated.
        What type of arguments do you intend to pass to repeatedlyApply ? It appears that one of the arguments, 'p', is another function like 'sqrt'. Function compose returns an anonymous function which must be evaluated in order to return a value. Can you give us some more information so we can better help you?
        This all I could come up with:
        Code:
        def compose(f,g):
            return lambda x: f(g(x))
        
        def repeatedlyApply (p, n, v):
            for _ in [num for num in range(n) if num % 2 == 1]:
                z = compose(p,p)
                print z
                v = z(v)
            return v
        
        from math import sqrt
        
        print repeatedlyApply(sqrt, 5, 15823959.67)
        
        """
        >>> <function <lambda> at 0x00E10F30>
        <function <lambda> at 0x00DD14F0>
        2.81810517827
        >>>
        """

        Comment

        • boapython
          New Member
          • Feb 2007
          • 4

          #5
          Okay, sorry about that... so say if I put in:

          repeatedlyApply (lambda x: 2*x,3)("abc")

          I should get: 'abcabcabcabcab cabcabcabc'

          or repeatedlyApply (lambda x: x+1,10)(100)

          should yield 110

          The first argument I pass is a function with one argument of its own (In the first case "abc" and in the second "100") and the second is the number of times to repeat the operation on the variable and add to the total.

          Comment

          • bvdet
            Recognized Expert Specialist
            • Oct 2006
            • 2851

            #6
            Originally posted by boapython
            Okay, sorry about that... so say if I put in:

            repeatedlyApply (lambda x: 2*x,3)("abc")

            I should get: 'abcabcabcabcab cabcabcabc'

            or repeatedlyApply (lambda x: x+1,10)(100)

            should yield 110

            The first argument I pass is a function with one argument of its own (In the first case "abc" and in the second "100") and the second is the number of times to repeat the operation on the variable and add to the total.
            Something like this?
            Code:
            def repeatedlyApply (f, x, y, n):
                q = 0
                for i in range(n):
                    q += f(x,y)
                return q
            
            func = lambda x, y: x**2+y**2-8
            
            print repeatedlyApply(func, 6, 4, 6)
            
            >>> 264

            Comment

            • ghostdog74
              Recognized Expert Contributor
              • Apr 2006
              • 511

              #7
              Originally posted by boapython
              Okay, sorry about that... so say if I put in:

              repeatedlyApply (lambda x: 2*x,3)("abc")

              I should get: 'abcabcabcabcab cabcabcabc'

              or repeatedlyApply (lambda x: x+1,10)(100)

              should yield 110

              The first argument I pass is a function with one argument of its own (In the first case "abc" and in the second "100") and the second is the number of times to repeat the operation on the variable and add to the total.


              you might be better off using map() or list comprehension. just an example only
              Code:
              >>> map(lambda x: 3*x, ["abc"])
              ['abcabcabc']

              Comment

              • boapython
                New Member
                • Feb 2007
                • 4

                #8
                Thanks guys! Exactly what I needed, :-)

                Hopefully when I get better at this stuff, I can come back here and contribute.

                Comment

                • bartonc
                  Recognized Expert Expert
                  • Sep 2006
                  • 6478

                  #9
                  Originally posted by boapython
                  Thanks guys! Exactly what I needed, :-)

                  Hopefully when I get better at this stuff, I can come back here and contribute.
                  Come back any time with questions, too. Keep posting,
                  Barton

                  Comment

                  • markus314
                    New Member
                    • Oct 2008
                    • 2

                    #10
                    Hi!

                    I've just found this through google.

                    You can define:
                    Code:
                    from functools import partial
                    from itertools import repeat
                    
                    def compose(f, g): #  also in the functional module
                        def func(*args, **kwargs):
                            return f(g(*args, **kwargs))
                        return func
                    
                    mcompose = partial(reduce, compose)
                    apply_n_times = compose(mcompose, repeat)
                    Then you get:
                    Code:
                    >>> def f(x):
                    ...     return 2 * x
                    >>> apply_n_times(f, 3)("abc")
                    'abcabcabcabcabcabcabcabc'
                    >>> apply_n_times(f, 3)(2)
                    16

                    Comment

                    • boxfish
                      Recognized Expert Contributor
                      • Mar 2008
                      • 469

                      #11
                      It's great that you could answer this question, but it's really old and was answered already. If you want to answer any fairly recently asked questions, I'm sure your help will be appreciated. Please read the posting guidelines.
                      Thanks.

                      Comment

                      Working...