Esoteric How-to's, Essays, and Rand();

Behind the Scenes of the C++ STL Algorithm Model

Ever wonder why the C++ STL uses an external, non-member function model for implementing algorithms for containers? The following article sums it up. Although a few of the “no less efficient” examples are debatable, the underlying principles definitely have merit.

One downside to the functional approach outlined in the article–that I didn’t see mentioned–is the fact that the interface is actually being split in a few cases, which is actually detrimental to cohesion. This happens when you have multiple overloaded forms of a function, and you keep one “flexible” one as a member function, but make the others external functions. Why not keep those as members for the sake of consistency? You can still implement them in terms of the one “flexible” member function, thereby continuing to ensure good encapsulation.

A few thoughts on applying the spirit of the law discussed in the article to other languages:

C# – Use extension methods, but the “this” type should be as generic as possible. You still get the benefits of DRY but in a more object-oriented fashion that works well with Intellisense and cohesion. I like the extension method approach better than the old static-methods approach (e.g., string.Join) because it still enforces good encapsulation, and is less verbose and more cohesive than the alternative.

Ruby – Use mix-ins that use duct-type delegation to other methods. These can be automatically mixed into all applicable classes when the mix-in is “required”.

Lisp – Brilliant! All you have are functions!

It’s also important to note the power you derive from combining this functional approach with lambda functions, function objects, and covariance.

Share this:

Like this:

Related

One Response

usually is defined to be the set of all real numbers. The function f does not have the real numbers − 1, − 2, − 3 as outputs, thus they are in its codomain but not in its image. Codomains are useful for function composition . Composition (g followed by f) is defined if the codomain of g is the same as the domain of f. Thus the codomain of g defines what functions may follow g. The word range is used in some texts to refer to the image and in others to the codomain, in particular in computing it often refers to the codomain. The domain and codomain are often “understood.” Thus for the example given above, f(x) = 2x, the domain and codomain were not stated explicitly. They might both be the set of all real numbers , but they might also be the set of integers . If the domain is the set of integers, then image consists of just the even integers.

RT @notmyname: How does the internet not have a top-ten list for "9 reasons for zero-based counting"? 1 day ago

Ironic: When money is tight, you often avoid even those investments that would otherwise make better use of the resources you already have. 1 day ago

Disclaimer

The authors of this website are in no way (that you or your lawyer can think of) responsible for any harm you might cause to yourself, other people, small animals, or even your computer after reading or following any content or directions posted on our site.