小编alo*_*ols的帖子

Lisp中的顺序过程

当我尝试使用不可变对象编写函数样式时,顺序操作最终会从内到外编写,如下所示:

(thing-operation3
  (thing-operation2
    (thing-operation1 thing extra-arg1)
    extra-arg2)
  extra-arg3)
Run Code Online (Sandbox Code Playgroud)

我开始看到这个模式重复了我的代码,我发现它很难阅读.使用咖喱和撰写等高阶程序可以略微提高:

((compose1
  (curryr thing-operation3 extra-arg3)
  (curryr thing-operation2 extra-arg2)
  (curryr thing-operation1 extra-arg1))
 thing)
Run Code Online (Sandbox Code Playgroud)

也许更好,但它仍然是颠倒的,并且需要一些额外的认知负荷来弄清楚发生了什么.而且我不确定这是否是意识形态的Lisp代码.

面向对象的风格更容易阅读:

thing.operation1(extra-arg1).operation2(extra-arg2)
    .operation3(extra-arg3)
Run Code Online (Sandbox Code Playgroud)

它以自然顺序读取,也可以使用不可变对象实现.

在Lisp中编写这样的顺序操作的理想方法是什么,以便它们易于阅读?

lisp scheme functional-programming common-lisp racket

4
推荐指数
1
解决办法
248
查看次数