all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* extracting serial repeats
@ 2010-01-23 16:01 Andreas Roehler
  0 siblings, 0 replies; 5+ messages in thread
From: Andreas Roehler @ 2010-01-23 16:01 UTC (permalink / raw
  To: help-gnu-emacs

Hi,

let's consider the following pseudo-code, assumed inefficient, because Functions 1..n  are identic.

Function-1
(Body-of-Function-1)
Follow-action-1 with Function-1
Follow-action-2 with Function-1

Function-2
(Body-of-Function-2)
Follow-action-1 with Function-2
Follow-action-2 with Function-2
...
Function-n
(Body-of-Function-n)
Follow-action-1 with Function-n
Follow-action-2 with Function-n

More efficient seems, having

Body-of-Functions

extracted into a subroutine

so we could write something like

(dolist (element (1..n))
   SUBROUTINE element
   Follow-action-1 with element
   Follow-action-2 with element

Question is: exists a known tool for the extraction resp. refactoring needed?

Thanks


Andreas

--
https://code.launchpad.net/s-x-emacs-werkstatt/
http://bazaar.launchpad.net/~a-roehler/python-mode/python-mode.el/






^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: extracting serial repeats
       [not found] <mailman.74.1264262438.14305.help-gnu-emacs@gnu.org>
@ 2010-01-23 20:00 ` Anselm Helbig
  2010-01-24  9:14   ` Andreas Röhler
       [not found]   ` <mailman.95.1264324380.14305.help-gnu-emacs@gnu.org>
  0 siblings, 2 replies; 5+ messages in thread
From: Anselm Helbig @ 2010-01-23 20:00 UTC (permalink / raw
  To: help-gnu-emacs

Hello Andreas!

At Sat, 23 Jan 2010 17:01:40 +0100,
Andreas Roehler wrote:
> 
> Hi,
> 
> let's consider the following pseudo-code, assumed inefficient, because Functions 1..n  are identic.
> 
> Function-1
> (Body-of-Function-1)
> Follow-action-1 with Function-1
> Follow-action-2 with Function-1
> 
> Function-2
> (Body-of-Function-2)
> Follow-action-1 with Function-2
> Follow-action-2 with Function-2
> ...
> Function-n
> (Body-of-Function-n)
> Follow-action-1 with Function-n
> Follow-action-2 with Function-n
> 
> More efficient seems, having
> 
> Body-of-Functions
> 
> extracted into a subroutine
> 
> so we could write something like
> 
> (dolist (element (1..n))
>    SUBROUTINE element
>    Follow-action-1 with element
>    Follow-action-2 with element
> 
> Question is: exists a known tool for the extraction resp. refactoring needed?

Sounds like a case for a macro:

  (defun foo-function (i)
    `(defun ,(intern (format "foo-function-%d" i)) (arg1 arg2)
       (message "this is the body of the function")))

  (defmacro define-foo-functions (n)
    `(progn 
       ,@(loop for i from 1 to n
               collect (foo-function i))))

The question is, however, why do you need functions with a number in
their name, shouldn't this be an argument to the function? 

HTH, 

Anselm


-- 
Anselm Helbig 
mailto:anselm.helbig+news2009@googlemail.com


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: extracting serial repeats
  2010-01-23 20:00 ` Anselm Helbig
@ 2010-01-24  9:14   ` Andreas Röhler
       [not found]   ` <mailman.95.1264324380.14305.help-gnu-emacs@gnu.org>
  1 sibling, 0 replies; 5+ messages in thread
From: Andreas Röhler @ 2010-01-24  9:14 UTC (permalink / raw
  To: Anselm Helbig; +Cc: help-gnu-emacs

Anselm Helbig wrote:
> Hello Andreas!
>
> At Sat, 23 Jan 2010 17:01:40 +0100,
> Andreas Roehler wrote:
>   
>> Hi,
>>
>> let's consider the following pseudo-code, assumed inefficient, because Functions 1..n  are identic.
>>
>> Function-1
>> (Body-of-Function-1)
>> Follow-action-1 with Function-1
>> Follow-action-2 with Function-1
>>
>> Function-2
>> (Body-of-Function-2)
>> Follow-action-1 with Function-2
>> Follow-action-2 with Function-2
>> ...
>> Function-n
>> (Body-of-Function-n)
>> Follow-action-1 with Function-n
>> Follow-action-2 with Function-n
>>
>> More efficient seems, having
>>
>> Body-of-Functions
>>
>> extracted into a subroutine
>>
>> so we could write something like
>>
>> (dolist (element (1..n))
>>    SUBROUTINE element
>>    Follow-action-1 with element
>>    Follow-action-2 with element
>>
>> Question is: exists a known tool for the extraction resp. refactoring needed?
>>     
>
> Sounds like a case for a macro:
>
>   (defun foo-function (i)
>     `(defun ,(intern (format "foo-function-%d" i)) (arg1 arg2)
>        (message "this is the body of the function")))
>
>   (defmacro define-foo-functions (n)
>     `(progn 
>        ,@(loop for i from 1 to n
>                collect (foo-function i))))
>
> The question is, however, why do you need functions with a number in
> their name, shouldn't this be an argument to the function? 
>
> HTH, 
>
> Anselm
>
>
>   

Hi Anselm,

thanks a lot.

Seems I didn't describe the problem well: it's something like: "How to
avoid parallel implementing?".
Or let's say it that way:

Given a serial or serials of lambda-forms which take arguments of the
same kind processing it the same way,
but differ in function-names, they are referring to.

Instead of implementing the same forms repeatedly under different names,
we could re-use one.

Can do that by hand, sure. But as the issue shows up occasionaly, a tool
doing these extractions might be useful.

Andreas




^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: extracting serial repeats
       [not found]   ` <mailman.95.1264324380.14305.help-gnu-emacs@gnu.org>
@ 2010-01-24 10:17     ` Anselm Helbig
  2010-01-24 11:42       ` Andreas Röhler
  0 siblings, 1 reply; 5+ messages in thread
From: Anselm Helbig @ 2010-01-24 10:17 UTC (permalink / raw
  To: help-gnu-emacs

Hi Andreas!

> thanks a lot.
> 
> Seems I didn't describe the problem well: it's something like: "How to
> avoid parallel implementing?".
> Or let's say it that way:
> 
> Given a serial or serials of lambda-forms which take arguments of the
> same kind processing it the same way,
> but differ in function-names, they are referring to.
> 
> Instead of implementing the same forms repeatedly under different names,
> we could re-use one.

So you don't like the macro approach because it's still creating a lot
of functions internally? If you'd like to share the implementation,
the code could look like this:

  (defun foo (n a b c)
    (message "n: %d; a: %s; b: %s; c: %s" n a b c))

  (defun make-foo-function (n)
    (lexical-let ((lexical-n n))
      (lambda (a b c)
        (foo lexical-n a b c))))

  (dotimes (i 22)
    (fset (intern (format "foo-function-%d" i))
          (make-foo-function i)))

  (foo-function-21 1 2 3)

I there's anything unclear about this code or the defmacro-approach I
gave you before, just ask. 

> Can do that by hand, sure. But as the issue shows up occasionaly, a tool
> doing these extractions might be useful.

Maybe I didn't get the issue right: you've got repetetive code to
start with and want to have it refactored automatically? It's
certainly possible to have such functionality, on the other hand, you
don't have to write repetetive code in lisp in the first place, that's
what macros are for. And, as shown above, you can also use functional
programming techniques to the same end. 

All the best, 

Anselm


-- 
Anselm Helbig 
mailto:anselm.helbig+news2009@googlemail.com


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: extracting serial repeats
  2010-01-24 10:17     ` Anselm Helbig
@ 2010-01-24 11:42       ` Andreas Röhler
  0 siblings, 0 replies; 5+ messages in thread
From: Andreas Röhler @ 2010-01-24 11:42 UTC (permalink / raw
  To: help-gnu-emacs

Anselm Helbig wrote:
> Hi Andreas!
>
>   
>> thanks a lot.
>>
>> Seems I didn't describe the problem well: it's something like: "How to
>> avoid parallel implementing?".
>> Or let's say it that way:
>>
>> Given a serial or serials of lambda-forms which take arguments of the
>> same kind processing it the same way,
>> but differ in function-names, they are referring to.
>>
>> Instead of implementing the same forms repeatedly under different names,
>> we could re-use one.
>>     
>
> So you don't like the macro approach because it's still creating a lot
> of functions internally? If you'd like to share the implementation,
> the code could look like this:
>
>   (defun foo (n a b c)
>     (message "n: %d; a: %s; b: %s; c: %s" n a b c))
>
>   (defun make-foo-function (n)
>     (lexical-let ((lexical-n n))
>       (lambda (a b c)
>         (foo lexical-n a b c))))
>
>   (dotimes (i 22)
>     (fset (intern (format "foo-function-%d" i))
>           (make-foo-function i)))
>
>   (foo-function-21 1 2 3)
>
> I there's anything unclear about this code or the defmacro-approach I
> gave you before, just ask. 
>
>   
>> Can do that by hand, sure. But as the issue shows up occasionaly, a tool
>> doing these extractions might be useful.
>>     
>
> Maybe I didn't get the issue right: you've got repetetive code to
> start with and want to have it refactored automatically?

yes
> It's
> certainly possible to have such functionality, on the other hand, you
> don't have to write repetetive code in lisp in the first place,

yes, rather avoid it. The scenario is: it just happened.
Precisely while writing some tests.

Wrote one form testing a patch,
another form testing another patch,
then remarked the parallels...

Thanks again

Andreas

>  that's
> what macros are for. And, as shown above, you can also use functional
> programming techniques to the same end. 
>
> All the best, 
>
> Anselm
>
>
>   





^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2010-01-24 11:42 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-01-23 16:01 extracting serial repeats Andreas Roehler
     [not found] <mailman.74.1264262438.14305.help-gnu-emacs@gnu.org>
2010-01-23 20:00 ` Anselm Helbig
2010-01-24  9:14   ` Andreas Röhler
     [not found]   ` <mailman.95.1264324380.14305.help-gnu-emacs@gnu.org>
2010-01-24 10:17     ` Anselm Helbig
2010-01-24 11:42       ` Andreas Röhler

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.