4 ms·
I think the main concept here is not the exact syntatic construct used, but the idea of removing the behaviour from the data structure, so implementors of new s
by kroltan 6y ago
I think the main concept here is not the exact syntatic construct used, but the idea of removing the behaviour from the data structure, so implementors of new structures are not bound contractually to implement every behaviour.
In a way, the concept of monads attempts to solve this, since each specific data type would only need to implement its respective map/bind method, to be able to work for any other specific operation, since bind can be used to perform both transformation as well as reduction depending on the operation you give it.
(to purists, forgive my nomenclature, I am not a mathematician or fluent in FP lingo)
- layer8 6y agoThe misnamed Visitor pattern does exactly this: decouple the operations on a polymorphic object structure from the implementation of the structure. It can be used in a way similar to FP-style pattern matching (although with more boilerplate), but is more flexible because the implementations accepting a Visitor are not coupled to the constructors of a data structure (e.g., a DAG object could accept a tree visitor, or vise versa). It would be interesting to have a language that provides convenient syntactic sugar for the visitor pattern. EDIT: Just to clarify: The visitor pattern is usually presented in the context of "visiting" nodes in a recursive traversal. But the pattern is actually independent from the "traversal" use case. Its essence is really just the "switching", or case distinction, over an algebraic sum-like type (or over a type that provides a sum type view of itself).
- wulouis0511 6y agoAlso strategy pattern. IMO many patterns are kinds of strategy. :p