Monday, 6 August 2012

"Spreadsheet programming" in C++

In a previous post I explained that Streamulus is not just-a-spreadsheet because beyond managing dependencies between scalar values, it defines a programming language for computations over ordered event streams. As such, it can be used as the backbone of a CEP system.

Then it occurred to me that it would be fun to write a spreadsheet library (a la the Lisp Cells library) and that this could be a good way of demonstrating some of the capabilities of Streamulus.  So I did, here it is.  I don't know of another C++ library that does this, so please let me know if there is one.

Usage is like so:

#include "spreadsheet.hpp"

int main (int argc, const char * argv[])
    using namespace spreadsheet;

    Spreadsheet sheet;

    // Create some cells
    Cell<double> a = sheet.NewCell<double>(); 
    Cell<double> b = sheet.NewCell<double>(); 
    Cell<double> c = sheet.NewCell<double>(); 

    // Assign expressions to them:
    c.Set(SQRT(a()*a() + b()*b()));

    // print the values:
    std::cout << " a=" << a.Value() 
              << " b=" << b.Value() 
              << " c=" << c.Value() << std::endl;

    // change a and see it propagate:

    std::cout << " a=" << a.Value() 
              << " b=" << b.Value() 
              << " c=" << c.Value() << std::endl;

This program will print:
 a=3 b=4 c=5 
 a=4 b=4 c=5.65685

There are a few limitations in the first version, which can be helped with some more work (such as the need to write a() and b() rather than simply a and b in the expression above). See the README file and the example code for more information. 

1 comment:

  1. The alternative would still be a flexible Excel spreadsheet as e.g. you can find on eFinancialModels