4 ms·
The thing is, how would a 'verb' oriented language deal with a very specific kind of noun, one which both reacting to general verbs differently and required it'
by arohner 17y ago
The thing is, how would a 'verb' oriented language deal with a very specific kind of noun, one which both reacting to general verbs differently and required it's own special verbs. It seems like this special noun would 'smeared' around the verbs to a worse extent. If Baltic Avenue has some weird behavior, it seems cleaner to have it as separate subclass than having it's behavior taken into by various conditionals.
Think of each individual community chest card. Think of "can I mortgage this property?". Think of "if the reading railroad is not owned, take possession of it." There are a lot of rules in monopoly, and some of them have to do with the combination of several pieces of state: the state of the player, the state of a piece of property, whose turn it is, etc. Trying to handle certain behavior inside the Baltic Avenue subclass might be problematic because you might not have access to some necessary state. And even if you have access to it, getting that state from the baltic avenue class might be a serious violation of concerns.
A helpful oversimplification is to say OO "prefers dumb code and smart data", while FP prefers "smart code and dumb data".
In an FP monopoly, the board is a set of dumb datastructures. The rules are functions that modify the board.
Each community chest card can be a single function, that takes the state of the old board, and returns the state of the new board. The function is simple, it just reaches in and mucks with the single state of the world datastructure. Now your property objects are not weighed down with how to handle community chest card #35. The code is also simpler, because no single class has functions for how to respond to each rule in monopoly.
Another extremely powerful aspect of FP is first class functions. The actions a player is allowed to take can just be a list of functions. When players change turns, you can run a function on the list of allowed actions that adds or removes functions as appropriate.
- amalcon 17y agoA helpful oversimplification is to say OO "prefers dumb code and smart data", while FP prefers "smart code and dumb data". This makes a lot of sense, but it seems backwards in a way: FP is as much about the realization that code is data as it is about any specific way of doing things. Your list of functions for "actions a player is allowed to take" certainly seems like "smart data" to me.
- lmkg 17y ago"Code is data" is orthogonal to smart/dumb code vs dumb/smart data, as it's a property of reflective (introspective?) programming languages, not functional ones. You could conceivably use imperative or OOP techniques in JavaScript to construct a string to eval(), but then Dijkstra would shamble out of his grave to give you a very stern glare. An array of functions to invoke is really more like a cute functional way of expressing a complicated switch. "Code is data" refers more to meta-programing, which would be having the language write the functions for you based on a higher-level description.
- amalcon 17y agoI was under the impression that it was neither "macros" nor "first-class functions", but rather something that encompassed both of them: basically, the idea that the programmer shouldn't need to treat one part of the data differently just because the processor does.