help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Defining functions within functions?


From: Michael Heerdegen
Subject: Re: Defining functions within functions?
Date: Mon, 23 May 2016 22:09:11 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/25.0.94 (gnu/linux)

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.

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.


Michael.




reply via email to

[Prev in Thread] Current Thread [Next in Thread]