Posted: Mar 10, 2012 12:04 pm
by VazScep
mizvekov wrote:By the way, since we were talking about cool new c++ features, here is another one of my favorites:
Example code of the new random number library, much more powerful and modular:
Code: Select all
// 1.0 is the lambda parameter of the exponential distribution
auto random = std::bind(std::exponential_distribution<double>(1.0), std::default_random_engine());
for (;;)
      std::cout << random() << std::endl;
Okay, so exponential_distribution is a first-class function? And one thing we're seeing here is the use of templates to parameterise a first-class function? And presumably, there is also partial specialisation going on, to tailor the implementation to doubles as opposed to some other type?

Can you use bind with any function, or does it have to be declared in a special way? And how does this interact with the object system? Can I get a virtual function of a class as a first-class function, and have it close over its instance?

Will generate numbers like:
Code: Select all
0.330217
3.44907
0.208902
1.28924
0.838482
0.549105
0.0226059

Pretty hard to beat that in elegance :grin:
The modular design means that you can use other generators, like the other pseudo-random ones included in the standard lib, or roll your own, be that another pseudogen, or even one based on reading /dev/random (for unix systems) or which communicates directly with a hardware RNG.
And you can combine any of those with any probability distribution, such as the ones included in the standard (there are several others, like uniform, normal, binomial), or again, create your own.

Pretty neat huh? :grin:
That is very sweet. Have you compared it much to the random generators in Haskell? It looks pretty competitive in terms of expressive power (speedwise, forget it).