Monday 28 March 2011

C# 2.0 - Functional composition and closures

Sadly, it seems that anonymous functions / delegates cannot hold state so although we can imitate closures if the delegate is defined inline, we cannot do the below:

int diff = 2;
Functional.filter2_fn<int, int> f = delegate(int a, int b) { return Math.Abs(a - b) > diff; };
diff = 3;
bool retval = Functional.forAll2(f, l, m);

because, by the time we come to use
f
, the value stored in
diff
has changed to 3.

C# 2.0 - Functional composition

Yes! It's possible, just! Get your thinking gear around this!

        public static filter_fn<A> not<A>(filter_fn<A> f)
        {
            return delegate(A a) { return !f(a); };
        }

        public static bool forAll<A>(filter_fn<A> f, IEnumerable<A> input)
        {
            return !exists(not(f), input);
        }


and an example:

        public void CompositionTest1()
        {
            int[] i = { 1, 2, 3, 45, 56, 6 };

            bool allOdd = Functional.forAll(Functional.dIsOdd, i);
            bool notAllOdd = Functional.exists(Functional.not(Functional.dIsOdd), i);

            Assert.IsFalse(allOdd);
            Assert.IsTrue(notAllOdd);
        }

Thursday 24 March 2011

C# is soooo verbose

I have this:
List<ReportItem2> value = new List<ReportItem2>();
IEnumerable<MComponentDetails> components = SwapUtils.GetBasketComponents(swap);
foreach (MComponentDetails detail in components)
{
    string sedol = InstrumentUtils.GetSedol(detail.fSicovam);
    string instrName = InstrumentUtils.GetInstrName(detail.fSicovam);
    value.Add(new ReportItem2(sedol, instrName, columnConfig));
}

and I'd like to have this:
List<ReportItem2> value = new List<ReportItem2>();
IEnumerable<MComponentDetails> components = SwapUtils.GetBasketComponents(swap);
value.AddRange(Functional.map(sec(columnConfig), components));

but there are two problems. One is that the code required in order to build
sec
and enable it to take a parameter (instead of simply being a delegate) is far too much; the second is that, unbelievably, the compiler will not implicitly cast from
IEnumerable<MComponentDetails>
to
IEnumerable<object>
Unbelievable!

By way of illustrating the verbosity of C#, here is my preliminary version of the loop innards:

private class sec
{
    private static ExportColumnConfiguration _config;
    public static Functional.fn f(ExportColumnConfiguration config)
    {
        _config = config;
        return sec_func;
    }

    private static Functional.fn sec_func = CreateReportItem2FromSecurity;
    private static ReportItem2 CreateReportItem2FromSecurity(MComponentDetails detail)
    {
        string sedol = InstrumentUtils.GetSedol(detail.fSicovam);
        string instrName = InstrumentUtils.GetInstrName(detail.fSicovam);
        return new ReportItem2(sedol, instrName, _config);
    }
}


Aha, but I discover that C# 2.0 supports anonymous delegates which are, effectively, lambdas and closures.

Splendid!

Wednesday 16 March 2011

CSMInstrument creation

Although it's obvious really, you should avoid doing this:

CSMInstrument i = new CSMInstrument(sicovam);
CSMSwap s = i;

and you should instead do this:

CSMInstrument i = CSMInstrument.GetInstance(sicovam);
CSMSwap s = i;

The CSMInstrument constructor should not be used. It should not be public; it really ought to be protected at the very least.

What the first example does is create an instance of the base class. Now, clearly, even if the sicovam is that of a derived class, the additional fields will not be populated. So use the factory. That's what it's there for! This is particularly pertinent given that one tends to pass around CSMInstrument references and utilise the implicit cast, as shown above, which will unaccountably fail.

In fact, as I think about it, CSMInstrument ought to be an abstract class. Ho hum!

Thanks for listening.

Tuesday 15 March 2011

CSMTransactionVector and Flat View Positions

Ack! I forgot that Flat View positions do not exist per se in the database. Therefore the code which queries histomvts using mvtident returns a different dataset compared to CSMPosition.GetTransactions()

Interestingly, CSMTransactionVector(string sql) can be used for Flat View positions as well. Beware!

Thursday 3 March 2011

CSMPortfolio loading and the corruption of already loaded CSMPositions

You know, of course, that CSMPortfolio objects are cached because they are expensive to load and compute. You also know, of course, that a user preference is 'Do not load portfolios'. Well, did you know that if you load some CSMPositions (and put them in a container for safe keeping) and then load portfolios, the CSMPositions will almost certainly be corrupted! And this is doubly surprising since, obviously, the portfolio corresponding to the position object has to be loaded already in order to build said position object.

All of which is a long-winded way of saying not to load portfolios in the middle of your app! You have been warned.

Wednesday 2 March 2011

CMString - lifetime and ownership

So, you have a CMString and you've noticed that it is IDisposable. But you're passing it in and out of functions. What is the lifetime of your CMString? Which reference has ownership, especially as Sophis have implemented their own reference counting class?

Here we have it then:

use CMString.ToString, CMString.GetString or the implicit cast operator to System.String and testing would suggest that this is safe. These three functions appear to perform copies.

assign another CMString to your first CMString and then Dispose the first one. Miraculously this works too! Even though it can be clearly seen that both CMStrings are in fact referring to the selfsame underlying data! Hurrah!

Nevertheless, don't rely on the CMString because of the unmanaged aspect. Always convert to System.String!