3 ms·
> The strategy pattern is a hack that belongs in the past, and you should use lambdas instead In that case, your lambda object is your strategy. One of the goo
by quotemstr 10y ago
> The strategy pattern is a hack that belongs in the past, and you should use lambdas instead
In that case, your lambda object is your strategy. One of the good points that the "Design Patterns" authors make and that everyone else ignores is that these patterns are names for recurring structures that pop up independently of implementation language and environment. Some silly functor object is one way of implementing the more abstract "Strategy" pattern. Your lambda is a better one in many cases. It's the same high-level concept.
- qwertyuiop924 10y agoWell, yes, but if you every write a class for C#, modern Java, or pretty much any language save C++ or Python (and even then you should write functions) that has the word "strategy" in it, you're doing it wrong, and should be punished for reinventing language features using OO methodology. Was it Steve Yegge who menationed the Perl community calling Design Patterns "FP for Java"?
- quotemstr 10y agoI don't know, but this Yegge essay is one of my favorites ever: http://steve-yegge.blogspot.com/2006/03/execution-in-kingdom-of-nouns.html http://steve-yegge.blogspot.com/2006/03/execution-in-kingdom...
- qwertyuiop924 10y agoI hadn't read that one. The line about the meaning of lambda is hilarious. Anyways, yeah the line was in "Singleton Considered Stupid." https://sites.google.com/site/steveyegge2/singleton-considered-stupid https://sites.google.com/site/steveyegge2/singleton-consider...
- willtim 10y agoThere is a name for "strategy pattern" that existed long before the GoF book: higher-order function. But what's worse, in that it causes confusion, is when names are repurposed, "Functor" is a useful abstraction in programming, but is not related to your usage above.
- qwertyuiop924 10y agoWell, a Functor isn't an HOF, and an HOF needn't be an implementation of Strategy.
- willtim 10y agoI never said a Functor was a HOF, but it's not far wrong. The implementation of the morphism-mapping part of a functor is a higher order function. Show me an example of Strategy that is not essentially a HOF.
- qwertyuiop924 10y agoAll strategy is HOF, not all HOF is strategy: Observe: (define (make-counter n) (lambda (c) (set! n (+ n c)) n)) This is a HOF, but not an instance of Strategy.
- willtim 10y agoI asked for an example of Strategy that wasn't just a HOF, but I now see you do agree with this. Yes your example is technically a HOF as it returns a function. So I guess your point is that HOF is not specific enough, although one might argue that in general usage it usually does imply function-value arguments.
- qwertyuiop924 10y agoI mean, that's not the general use I see, so we clearly have different social circles. The thing is, a HOF is a mechanism, the Strategy pattern is an intent. I would in fact argue that there is at least one HOF that takes functions as arguments and isn't an instance of strategy: call/cc. call/cc does not actually use the passed in function to determine how any action should be done. All it does is provide a capture of the current continuation as an argument. That's it. So it's not a strategy, it's just a HOF.
- 10y ago