Victus Spiritus

home

Folding in c++

30 Apr 2011

Today's post is short and sweet, like a peach.

On the odd chance you're morbidly curious how to fold an array with a function in c++ like myself, you're in for a pleasant surprise (unless you're intimately familiar with boost's lambdas, in which case you will be appalled at how little thought was put into this whimsical hack).

This morning I cleaned up my proj (fork it) utility library and added an Array structure with a foldRight member, and refactored curry to take in either a va_arg parameter list or a mArray of functions.

I make no claims about its stability or memory non-leakiness of the data structure although a hundred million delete and allocation calls had a stable memory footprint.

It all starts with Fptr.h:

the new hotness of mArray.h (I pronounce it emory as in memory)

the test program for folding, sort.cpp (naming fail now it sorts too):

the test program for curry:

Side note: I had some trouble embedding javascript directly in this post but I hacked together a gist which makes gists wider, and in a metatastic way it makes itself wider:

Here's the scripts I included below:

Related posts:



<script type="text/javascript"
src="https://gist.github.com/raw/949945/1468755b2659aa0206ef4b0060100b152f44a8d3/growingdivs.js"></script>