30 karma · joined December 12, 2011
I've been dabbling in the creation of a functional library that provides a modest subset of Prelude-inspired features from Haskell. https://github.com/jdduke/fpcpp Many of the algorithms are simply wrappers around stl algorithms, hiding some of the syntactic awkwardness attendant with the heavy use of iterators. Some day I'll get around to playing with monadic devices, and integrating the list comprehension work that I've done recently.
As for "original specification", I'm not sure why you even bring that up. complex is certainly in the C++11 specification, against which my snippet is compliant.
template< typename T >
auto findRoots(T a, T b, T c) -> pair<maybe<complex<T>>, maybe<complex<T>>> {
typedef complex<T> C;
typedef maybe<C> Root;
typedef pair<Root, Root> Roots;
if (a == 0) {
if (b == 0)
return Roots(nothing, nothing);
else
return Roots(Root(-c / b), nothing);
} else {
auto d = C(b*b - a*c*4);
auto two_a = a*2;
return Roots(Root((-b + sqrt(d)) / two_a),
Root((-b - sqrt(d)) / two_a));
}
}
maybe is simply a templatized std::pair wrapper that lets you check if the returned value is valid (either via maybe.valid() or if(maybe)). All imaginary results are handled automatically via std::complex. nothing decomposes into an invalid maybe value of the appropriate type.I'm knee deep in the development of several header-based, fully templated libraries, so I still certainly feel the pains of compilation times. And while the tests/samples I deal with tend to be somewhat extreme in their (ab)use of templates, I think you'll still find that the average Boost user would give a lot for yet speedier compilation.
I'm not sure what point it serves to compare debug mode executable sizes, but that should be clearly stated in the article. As other people stated, the final example really ought to lose the wrapping class in the spirit of C++11 brevity and functional decomposition.
As for size, you're absolutely right, though for many scenarios that appears to be less and less a pressing concern. Perhaps the biggest drawback is compilation time.
To the point that, I've started writing several libraries that enable in C++ several of the features I miss most in Haskell. In particular, I've missed the natural syntax of higher order functions, e.g., currying and functional composition, as well as the pithy expressiveness of Prelude vs the STL. With C++11, we can have these things, and while Boost gives us a wealth of functionality, it's hardly the most natural (and lightweight) library.
fc (functional composition) (https://github.com/jdduke/fc) and fpcpp (functional programming with C++)(https://github.com/jdduke/fpcpp) are my recent (very much work-in-progress) efforts towards this end.
With fc, one can naturally compose functions, lambdas and function objects, e.g., given composable functions f, g, h, write auto fgh = f + g + h; or, auto fgh = compose(f, compose (h,g));, and then call it as fgh(a,b,c);
fpcpp enables syntax like
let pi = [](unsigned samples) -> double {
typedef std::pair<double,double> point;
let dxs = map([](const point& p) { return p.first*p.first + p.second*p.second; },
zip(take(samples, rand_range(-1.0,1.0)),
take(samples, rand_range(-1.0,1.0))));
return 4.0 * filter([](double d) { return d <= 1.0; }, dxs).size() / dxs.size();
}
EXPECT_NEAR(pi(10000), 3.14);
In writing fc and fpcpp, I've actually become less and less concerned for the future of C++; having used a good number of the C++11 features available, I've regained a good deal of my passion for C++ programming. Really, C++ needs a more expansive and natural standard library, iterators are powerful but are NOT the answer.Also, please inform me as to how this statement relates to Lord Byron. Nothing I've read of Byron would favor him as the statement's originator over Newton. I can't help but think of this scene from Good Will Hunting when I read your replies http://www.youtube.com/watch?v=ymsHLkB8u3s&t=1m56s
In that light, would you also doubt these verified Newtonian quotes that suggest similar thinking? "If I have seen further it is only by standing on the shoulders of giants." "Truth is ever to be found in simplicity, and not in the multiplicity and confusion of things." "If I am anything, which I highly doubt, I have made myself so by hard work."
Newton was in a world of his own; he, along with a few select others (Lock, Descartes and others), are in large part responsible for the Enlightenment. The very Enlightenment this statement characterizes so well. Even if he didn't say it (I cannot find the source, but I have read this quote a number of times in various papers), it is very much the kind of statement he would make.