4 ms·
> Why compose classes, abstract base classes, etc. when you can compose simple functions! Because they allow you to compose higher level entities? (Incidental
by mpweiher 10y ago
> Why compose classes, abstract base classes, etc. when you can compose simple functions!
Because they allow you to compose higher level entities?
(Incidentally, one would generally compose objects).
"When programming a component, the right computation model for the component is the least expressive model that results in a natural program." -- Concepts Techniques and Models of Computer Programming
So when a less expressive model (such as FP) is sufficient for a "natural" program, definitely use that. And of course that has been the general recommendation. Switch to a more expressive model once this is no longer the case. In my experience, FP tends to be good for "computation", at least if performance isn't a primary concern. However, most of what "computers" (badly named) do today isn't computation, and modeling communication and storage over time tend to get awkward (though obviously possible).
- haskellandchill 10y ago> So when a less expressive model (such as FP) You can express object models in FP so what you are saying is very confusing to me.
- mpweiher 10y agoModels in "Concepts Techniques and Models of Computer Programming" Note that they are talking about general models of computation, in particular including at least: - Declarative sequential model (strict functional programming, deterministic logic programming, dataflow/logic variables, higher-order procedures). Component behavior is independent of rest of program. - Declarative concurrent model (explicit threads, by-need/lazy computation, data-driven concurrency, demand-driven concurrency). Almost as simple as declarative sequential. Declarative model with exceptions: allows nondeterminism to be visible. - Message-passing concurrent model (declarative plus ports/communication channels). Allows nondeterminism, and allows nondeterminism to be restricted to particular components. - Stateful model (declarative model extended with explicit state). Allows components to keep history. Typical of sequential OO programming. - Shared-state concurrent model (declarative extended with both explicit state and threads). Typical of concurrent OO programming. - Relational model (declarative extended with search). Search space explored by testing choices until result is satisfactory. Encompasses Prolog-style logic programming. - Constraint programming and still other models are discussed elsewhere in the book. http://wiki.c2.com/?PrincipleOfLeastPower http://wiki.c2.com/?PrincipleOfLeastPower
- louthy 10y ago> Because they allow you to compose higher level entities? That isn't true. FP still uses data records, they're just not tightly coupled to the functionality like OO. Pure functions + immutable types is a more powerful programming paradigm than data and functionality encapsulation via classes and methods. Also more advanced techniques like combinators can encode more complex and extensible patterns in a significantly more robust way than any OO class hierarchy (see 'parser combinators' for an example).
- novembermike 10y agoYou do realize that the object model is just data bundled with functions, right? If I'm using Erlang I can pass in a map that has something like #{ data => [(data)], methods => [(functions)] }. The object style approach is orthogonal to FP. EDIT: Just to be clear, I don't think this is a good idea. It's usually better to pass in one or more data structures and one or more first class functions separately. I'm just saying that it's not too different.