【发布时间】:2015-10-13 13:57:39
【问题描述】:
当我尝试使用不可变对象以函数式风格进行编程时,顺序操作最终会被由内而外地编写,如下所示:
(thing-operation3
(thing-operation2
(thing-operation1 thing extra-arg1)
extra-arg2)
extra-arg3)
我开始看到这种模式在我的代码中重复出现,我发现它很难阅读。使用 curry 和 compose 等高阶程序可以稍微改善这一点:
((compose1
(curryr thing-operation3 extra-arg3)
(curryr thing-operation2 extra-arg2)
(curryr thing-operation1 extra-arg1))
thing)
也许更好,但它仍然是颠倒的,并且需要一些额外的认知负担才能弄清楚发生了什么。而且我不确定这是否是理想的 Lisp 代码。
面向对象的风格更容易阅读:
thing.operation1(extra-arg1).operation2(extra-arg2)
.operation3(extra-arg3)
它以自然顺序读取,也可以用不可变对象来实现。
ideomatic 在 Lisp 中编写这种顺序操作以使其易于阅读的方式是什么?
【问题讨论】:
标签: functional-programming scheme lisp common-lisp racket