Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #80386

Re: How do i reduce this to a single function - the code is largely similar, just a direction of search toggle.

References <m3i9lp$8fp$1@dont-email.me>
Date 2014-11-08 09:31 +1100
Subject Re: How do i reduce this to a single function - the code is largely similar, just a direction of search toggle.
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.15577.1415399494.18130.python-list@python.org> (permalink)

Show all headers | View raw


On Fri, Nov 7, 2014 at 9:16 PM, Veek M <vek.m1234@gmail.com> wrote:
>     def jump_to_blockD(self):
>         end = len(self.b)
>         row, col = self.w.cursor
>         while row <= end:
>             try:
>                 new_col = self.b[row].index('def')
>                 self.w.cursor = row, new_col
>                 break
>             except ValueError:
>                 pass
>             row += 1
>
>     def jump_to_blockU(self):
>         end = 0
>         row, col = self.w.cursor
>         while row >= end:
>             try:
>                 new_col = self.b[row].rindex('def')
>                 self.w.cursor = row, new_col
>                 break
>             except ValueError:
>                 pass
>             row -= 1

Start by translating this into for loops, rather than while, and then
it'll be much easier to put all the differences in the signature. Tell
me if this translation is faithful:

    def jump_to_blockD(self):
        for row in range(self.w.cursor[0], len(self.b)+1, 1):
            try:
                new_col = self.b[row].index('def')
                self.w.cursor = row, new_col
                break
            except ValueError:
                pass

    def jump_to_blockU(self):
        for row in range(self.w.cursor[0], -1, -1):
            try:
                new_col = self.b[row].rindex('def')
                self.w.cursor = row, new_col
                break
            except ValueError:
                pass

Try those two, see if they function the same as your original code. If
they do, you should be able to figure out how to merge them, in their
new form.

ChrisA

Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

How do i reduce this to a single function - the code is largely similar, just a direction of search toggle. Veek M <vek.m1234@gmail.com> - 2014-11-07 16:46 +0630
  Re: How do i reduce this to a single function - the code is largely similar, just a direction of search toggle. Joel Goldstick <joel.goldstick@gmail.com> - 2014-11-07 16:18 -0500
  Re: How do i reduce this to a single function - the code is largely similar, just a direction of search toggle. Denis McMahon <denismfmcmahon@gmail.com> - 2014-11-07 22:22 +0000
  Re: How do i reduce this to a single function - the code is largely similar, just a direction of search toggle. Chris Angelico <rosuav@gmail.com> - 2014-11-08 09:31 +1100
  Re: How do i reduce this to a single function - the code is largely similar, just a direction of search toggle. Veek M <vek.m1234@gmail.com> - 2014-11-07 21:22 +0630
    Re: How do i reduce this to a single function - the code is largely similar, just a direction of search toggle. Ned Batchelder <ned@nedbatchelder.com> - 2014-11-08 07:56 -0500
      Re: How do i reduce this to a single function - the code is largely similar, just a direction of search toggle. Veek M <vek.m1234@gmail.com> - 2014-11-09 15:15 +0630
  Re: How do i reduce this to a single function - the code is largely similar, just a direction of search toggle. Denis McMahon <denismfmcmahon@gmail.com> - 2014-11-07 15:18 +0000

csiph-web