gpt4 book ai didi

elisp - 在 emacs lisp 中柯里化(Currying)一个函数

转载 作者:行者123 更新时间:2023-12-04 11:26:51 26 4
gpt4 key购买 nike

我有许多方便的函数可以在当前单词或区域上运行,并且由于懒惰等原因。我已经用模板构建了它们......

例如

(defun lower-camelcase-at-point-or-region ()
"lowerCamelCaseTheCurrent dashed or snake_case word or any words in text selection."
(interactive)
(let (pos1 pos2 meat)
(if (and transient-mark-mode mark-active)
(setq pos1 (region-beginning)
pos2 (region-end))
(setq pos1 (car (bounds-of-thing-at-point 'symbol))
pos2 (cdr (bounds-of-thing-at-point 'symbol))))
(setq meat (s-lower-camel-case (buffer-substring-no-properties pos1 pos2)))
(delete-region pos1 pos2)
(insert meat)
)
)

实际上,这都是样板,除了这条线......
(setq meat (s-lower-camel-case (buffer-substring-no-properties pos1 pos2)))

我在哪里打电话 s-lower-camel-case在缓冲区子字符串上。我想重用 at point 或 region 的东西,但不要在任何地方复制它,(因为这很愚蠢,而且维护起来很头疼。)

所以我真正想知道的是,我可以在 Emacs Lisp 中 curry 函数吗?

当我尝试这个...
(defun do-stuff-on-point-or-region ()
"Do stuff."
(interactive)
(operate-on-point-or-region 's-lower-camel-case)
)

operate-on-point-or-region定义为...:
(defun operate-on-point-or-region (fn)
"Pick the substring at point, or region
and replace it with the output of fn"
(let (pos1 pos2 meat)
(if (and transient-mark-mode mark-active)
(setq pos1 (region-beginning)
pos2 (region-end))
(setq pos1 (car (bounds-of-thing-at-point 'symbol))
pos2 (cdr (bounds-of-thing-at-point 'symbol))))
(setq meat (fn (buffer-substring-no-properties pos1 pos2)))
(delete-region pos1 pos2)
(insert meat)
)
)

我得到: Symbol's function definition is void: fn
我是否愚蠢地假设在 Emacs Lisp 中可以进行柯里化(Currying)!?还是我只是做错了?

最佳答案

我想补充一点,在 Emacs lisp 中无法进行柯里化(Currying)——函数应用程序没有柯里化(Currying),因为它不遵循柯里的公式。

柯里化(Currying)意味着将一个函数应用于部分参数,并返回另一个函数。由于动态范围,这在 Elisp 中是不可能的。

编辑:更新

现在 emacs-gnu 有闭包。

关于elisp - 在 emacs lisp 中柯里化(Currying)一个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14598664/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com