[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Defining functions within functions?
From: |
Marcin Borkowski |
Subject: |
Re: Defining functions within functions? |
Date: |
Tue, 24 May 2016 23:31:23 +0200 |
User-agent: |
mu4e 0.9.16; emacs 25.1.50.1 |
On 2016-05-23, at 22:09, Michael Heerdegen <michael_heerdegen@web.de> wrote:
> Marcin Borkowski <mbork@mbork.pl> writes:
>
>> I have a long function with quite a few (even nested) lambdas inside.
>> I decided to refactor it so that it becomes more readable. Since I use
>> lexical scoping, simply changing the lambdas into defuns defined
>> elsewhere won't work.
>>
>> In Scheme, one would probably use define inside another define. Is it
>> a good idea to use a (cl-)defun within a defun in Elisp to obtain
>> a closure in this situation? If not, what is a better way?
>
> There is no simple answer to that question I think.
>
> Sure, you could use `cl-labels', but that makes the code not much more
> readable than `let' with lambdas.
True (at least to some extent - it just relocates the "unreadability"
elsewhere).
> Personally, I use `let' with lambdas most of the time for local function
> definitions. When things get too complicated (seems you are at that
> point), instead of using lexical closures as local functions, you can
> use top-level defuns accepting additional arguments, or higher-level
> functions (also as top-level defuns) that take the essential values as
> arguments and return a closure.
And I wanted to use the former variant, but it struck me as not very
elegant. As for the latter, I'm not sure I understand it exactly, but
I'll give it some thought.
For now, I decided to go with lambdas, but also to sprinkle the code
with comments. Old-fashioned, but should do the jon in my case.
> Michael.
Thanks,
--
Marcin Borkowski
http://octd.wmi.amu.edu.pl/en/Marcin_Borkowski
Faculty of Mathematics and Computer Science
Adam Mickiewicz University