News for hackers. Stuff that matters.
Re:Defunctionalisation: An underappreciated tool for writing... by edflsafoiewq on Monday March 09, 2020 @10:40AM (#22525718)
Tell them it's like the command pattern in OO.
Re:Defunctionalisation: An underappreciated tool for writing... by richard_todd on Monday March 09, 2020 @01:00PM (#22527031)
It's really more like the Interpreter pattern (another one of the original GoF patterns)[0]. Instead of doing something directly, you create a description of it, and then provide an interpreter to do the work elsewhere in the program. That leaves you free to serialize/optimize/test/etc. the description prior to or instead of actually executing the work.

[0]: https://en.wikipedia.org/wiki/Interpreter_pattern

Re:Defunctionalisation: An underappreciated tool for writing... by bogdanoff_2 on Monday March 09, 2020 @11:50AM (#22526370)
That's an interesting comparison.

On one hand, it feels similar, especially if the Command class is "sealed".

On the other hand, it feels like completely the opposite since the command classes contain the code they execute and act like lambdas.

I guess you could apply a visitor pattern on top of the command pattern to make it truly like defunctionalization. I hope nobody actually does this.

Re:Defunctionalisation: An underappreciated tool for writing... by lidHanteyk on Monday March 09, 2020 @12:18PM (#22526599)
You're just describing an AST interpreter. Not only do people do this, but it's a reasonable thing to do.
Re:Defunctionalisation: An underappreciated tool for writing... by choeger on Monday March 09, 2020 @12:13PM (#22526560)
In fact, the command pattern is the opposite in a certain way. Defunctionalization corresponds to deeply embedded DSLs, whereas the command pattern corresponds to shallowly embedded DSLs.

In a proper functional language, you do not need objects for the latter, as you can directly compose functions.

That being said, the ability to add more operations than just "execute" to your commands allows you to emulate the features of deep embedding (e.g., serialization). But you have to extend every command for a single new operation (read up on the expression problem). That style is known as tagless in the FPL community, btw.