简体   繁体   English

GNUmake:如何编写一个真正的lambda

[英]GNUmake: how to write a true lambda

The following is my take on a so-so emulation of a lambda expression in GNUmake : 以下是我对GNUmake中的lambda表达式进行的一般模拟:

space := $(strip) $(strip)
comma := ,

# convert a space-separated list in $1 into a comma-separated list:
list2param = $(subst $(space),$(comma),$(strip $1))

# param $1 = quoted GUNmake expression
# param $2 = parameters to the expression as space-separated list
lambda = $(eval _lambda=$1)$(eval _lambda:=$$(call _lambda,$(call list2param,$2)))$(_lambda)

Example: 例:

$(info $(call lambda,$$1 .. $$2 .. $$3,foo bar baz))

Output: 输出:

foo .. bar .. baz

But this is less than satisfying, as I have to use a variable "_lambda" behind the curtain and the mechanism isn't really like lambda as the evaluation isn't separated from the definition. 但这并不能令人满意,因为我必须在幕后使用变量“ _lambda”,并且该机制与lambda并不完全一样,因为评估没有与定义分开。 Is there a clean, direct way to implement a lambda? 有没有一种干净,直接的方法来实现lambda? Or is there already some mechanism implemented which my blind procedural programming eyes didn't catch? 还是已经实现了某种我的盲目的程序编程所无法捕捉到的机制?

As in any primitive substitution language, you defer evaluation by quoting. 与任何原始替换语言一样,您可以通过引用来延迟评估。 The example below looks horrible and doesn't let you nest lambdas, but otherwise works: 下面的示例看起来很可怕,并且不允许您嵌套lambda,但可以正常工作:

define lambda
$(eval lambda-f=$1)lambda-f
endef

$(foreach n,1 2 3,$(call $(call lambda,$$(info $$(shell seq -s ' ' $$1))),$n))

Some readability can be (arguably) regained by amending the syntax like this: 通过修改如下语法,可以(可以说)重新获得一些可读性:

define lambda
$(eval lambda-f=$(subst ^,$$,$1))lambda-f
endef

$(foreach n,1 2 3,$(call $(call lambda,^(info ^(shell seq -s ' ' ^1))),$n))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM