Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'subject:code': 0.07; 'try:': 0.09; 'valueerror:': 0.09; 'subject:How': 0.10; 'cc:addr :python-list': 0.11; 'def': 0.12; 'translation': 0.12; '-1):': 0.16; '-1,': 0.16; '1):': 0.16; 'col': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'subject: \n ': 0.16; 'subject:search': 0.16; 'two,': 0.16; 'do,': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'cc:addr:python.org': 0.22; 'merge': 0.24; 'cc:2**0': 0.24; 'pass': 0.26; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'message-id:@mail.gmail.com': 0.30; 'easier': 0.31; 'figure': 0.32; 'fri,': 0.33; 'subject:the': 0.34; 'except': 0.35; 'form.': 0.35; 'received:google.com': 0.35; 'skip:j 20': 0.36; 'should': 0.36; 'nov': 0.38; 'pm,': 0.38; 'rather': 0.38; 'how': 0.40; 'tell': 0.60; 'break': 0.61; 'new': 0.61; 'subject:this': 0.83; 'to:none': 0.92; 'differences': 0.93 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=c2bzqMQdxOpCZ1eCSWERP6sk7cKGMeNGW36tSFWwj3c=; b=cbMVn+hpcjgVskMeVIukFSJsYsSutNerqXFoQWsblFc0r7jzSW74MdT8+38R+EMSim 6kJ/xJvX6Hbg/nG/fAPcrQljYqN0b+WFcjec6Okf7WXlwAV9ymty6s+MfvTVpj/UAtBS TxFvvBLVOEauawrductP9cbpZSjZSWPW2VLibj6+gDvnzUi61g+vmFI7ZMDWTGguN7aV C87vFun44q5q4YfGoY97rGSEkxdYjIyIYDCEd7mxnoO4xhf2yq+wRt0pYoNQMHHvQSud paySnqMfqak7UG8Kdz4CyoTjn/hX3btZt5i0ic9h/Da3UiYxlO08wRfxhYFxu6gx18Fn sFCg== MIME-Version: 1.0 X-Received: by 10.42.64.143 with SMTP id g15mr19104083ici.59.1415399486261; Fri, 07 Nov 2014 14:31:26 -0800 (PST) In-Reply-To: References: Date: Sat, 8 Nov 2014 09:31:26 +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 Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 52 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1415399494 news.xs4all.nl 2895 [2001:888:2000:d::a6]:54199 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:80386 On Fri, Nov 7, 2014 at 9:16 PM, Veek M 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