The Forum for Discussion about The Third Manifesto and Related Matters

You need to log in to create posts and topics.

A better sample of 'C# as D' Mark II

Here is the DTARM parts explosion implemented using 'C# as D', Mark II, heading only version.

Note that this uses the basic Codd-style RA plus 3 extensions: new value, aggregation, while (fixed point recursion).

This code (based on headings only) is easier to write than the last (based on declared types and generics). It mostly corresponds line for line with TD.

I'm yet to decide whether I like it enough to use directly, but it would make a solid basis for a simple pre-processor.

      // source data
      var mmqi = RelationNode.Import(SourceKind.Csv, ".", "MMQ", "MajorPNo:text,MinorPNo:text,Qty:number");

      // phase 1 - path generation and calculation
      WriteLine("While");
      var mmexp = mmqi.Rename("Qty,ExpQty")
        .While(new TupWhile(tw => tw
        .Rename("MinorPNo,zmatch")
        .Compose(mmqi.Rename("MajorPNo,zmatch"))
        .Extend("Qty,ExpQty,ExpQty", new TupExtend(t => (decimal)t[0] * (decimal)t[1]))
        .Remove("Qty")
      ));
      WriteLine(mmexp.Format());

While
MajorPNo:P4, MinorPNo:P6, ExpQty:8
MajorPNo:P3, MinorPNo:P5, ExpQty:4
MajorPNo:P2, MinorPNo:P4, ExpQty:7
MajorPNo:P2, MinorPNo:P6, ExpQty:56
MajorPNo:P2, MinorPNo:P3, ExpQty:2
MajorPNo:P2, MinorPNo:P5, ExpQty:8
MajorPNo:P1, MinorPNo:P3, ExpQty:3
MajorPNo:P1, MinorPNo:P5, ExpQty:12
MajorPNo:P1, MinorPNo:P2, ExpQty:5
MajorPNo:P1, MinorPNo:P4, ExpQty:35
MajorPNo:P1, MinorPNo:P6, ExpQty:280
MajorPNo:P1, MinorPNo:P3, ExpQty:10
MajorPNo:P1, MinorPNo:P5, ExpQty:40

      // phase 2 - aggregation of paths
      WriteLine("Aggregated");
      var mmagg = mmexp
        .Aggregate("ExpQty,TotQty", new TupAggregate((v, a) => (decimal)v + (decimal)a));
      WriteLine(mmagg.Format());

Aggregated
MajorPNo:P4, MinorPNo:P6, TotQty:8
MajorPNo:P3, MinorPNo:P5, TotQty:4
MajorPNo:P2, MinorPNo:P4, TotQty:7
MajorPNo:P2, MinorPNo:P6, TotQty:56
MajorPNo:P2, MinorPNo:P3, TotQty:2
MajorPNo:P2, MinorPNo:P5, TotQty:8
MajorPNo:P1, MinorPNo:P3, TotQty:13
MajorPNo:P1, MinorPNo:P5, TotQty:52  // <<=== this is the DTATRM example
MajorPNo:P1, MinorPNo:P2, TotQty:5
MajorPNo:P1, MinorPNo:P4, TotQty:35
MajorPNo:P1, MinorPNo:P6, TotQty:280

 

Andl - A New Database Language - andl.org