Monads

Overview

The monads interface is useful in all kinds of ways, since monads are so fundamental to Emdros.

C++ interface


#include <emdros/monads.h>

class BadMonadsException {};

class Monad_Set_Element {
public:
    Monad_Set_Element(monad_m first, monad_m last);
    Monad_Set_Element(monad_m monad);
    monad_m first(void) const; // Gets the member variable
    monad_m last(void) const;  // Gets the member variable
    void PrintConsole(CEMdFOutput *pOut) const;
    void PrintXML(CEMdFOutput *pOut) const;

    // Returns true on this and b having the same first_m and
    // this and b having the same last_m.
    bool equals(const Monad_Set_Element{PREAMP} b) const;

    // Returns true on this object being 
    // "lexicographically" before other object.
    // That is, true if and only if 
    // this->first_m < other.first_m 
    // or (this->first_m == other.first_m 
    //     and
    //     this->last_m < other.last_m)
    bool isBefore(const Monad_Set_Element{PREAMP} other) const;
};


class SOMConstIterator {
public:
  SOMConstIterator(const CSet_of_monad_ms *pMotherSOM);
  SOMConstIterator();
  SOMConstIterator(const SOMConstIterator{PREAMP} other);
  ~SOMConstIterator();
  bool hasNext() const; // Is the iterator == end iterator?  Doesn't alter iterator
  Monad_Set_Element next(); // Gets current and advances iterator afterwards
  Monad_Set_Element previous(); // Regresses iterator and then gets current
  Monad_Set_Element current(); // Gets current without altering iterator
};


class CSet_of_monad_ms {
public:
  class NoElementsException {};
  monad_m first() const;  // First in set
  monad_m last() const;   // Last in set
  SOMConstIterator const_iterator() const;  
  std::ostream{PREAMP} putme(std::ostream{PREAMP} s) const;
  std::string ToString(void) const; // Get string representation
  void PrintConsole(CEMdFOutput *pOut) const;
  void PrintXML(CEMdFOutput *pOut) const;
  CSet_of_monad_ms() {};
  CSet_of_monad_ms(monad_m m) { add(m); };
  virtual ~CSet_of_monad_ms();
  CSet_of_monad_ms{PREAMP} operator=(const CSet_of_monad_ms{PREAMP} som); // Not SWIG-wrapped
  bool part_of(const CSet_of_monad_ms{PREAMP} other) const;
  void union_with(const CSet_of_monad_ms{PREAMP} other);
  void difference(const CSet_of_monad_ms{PREAMP} other);
  static CSet_of_monad_ms intersect(const CSet_of_monad_ms{PREAMP} Aset, const CSet_of_monad_ms{PREAMP} Bset);
  void AddMSE(Monad_Set_Element mse);
  void add(monad_m monad);
  void add(monad_m first, monad_m last);
  bool equals(CSet_of_monad_ms{PREAMP} other) const; // A proxy for operator==
  bool operator==(CSet_of_monad_ms{PREAMP} other) const; // Not SWIG-wrapped
  bool is_member_of(monad_m m) const;
  bool is_empty(void) const;
  void RemoveMSE(const Monad_Set_Element{PREAMP} mse);
  bool gap_exists(monad_m Sm, monad_m{PREAMP} m) const; // Gap exists in set, starting at Sm, finishing at m
  void offset(monad_m m); // Add m to all mse's in set
  void clear(); // Make empty
};

Examples

A CSet_of_monad_ms can be used like this:


  // Declare and fill som with monads
  CSet_of_monad_ms som;
  som.add(1);      // Now is { 1 }
  som.add(3,6);    // Now is { 1, 3-6 }
  som.add(10,13) ; // Now is { 1, 3-6, 10-13 }

  // Get string representation
  std::string stringRepresentation = som.ToString();

  // Declare and fill som2 with monads
  CSet_of_monad_ms som2;
  som2.add(2,7);  // Now is { 2-7 }

  // Declare and fill som3 with monads
  CSet_of_monad_ms som3;
  som3.add(2,4);  // Now is { 2-4 }
  

  // Add the monads of som2 to som
  som.union_with(som2) // som is now { 1-7, 10-13 }

  // Get set intersection of som and som3
  CSet_of_monad_ms som4;
  som4 = CSet_of_monad_ms::intersect(som, som3); // som4 is now { 2-4 }

  // Subtract the monads of som2 from som
  som.difference(som2); // som is now { 10-13 }

The SOMConstIterator can be used like this:


  CSet_of_monad_ms som; // Assumed to be initialized from somewhere
  SOMConstIterator sci = som.const_iterator();
  while (sci.hasNext()) {
    Monad_Set_Element mse = sci.current();
   
    // Process mse ...

    // Are there any left?
    if (sci.hasNext()) {
       // Do something if the just-processed mse is not the last
    }

    sci.next();
  }

  // Or like this:
  CSet_of_monad_ms som; // Assumed to be initialized from somewhere
  SOMConstIterator sci = som.const_iterator();
  while (sci.hasNext()) {
    Monad_Set_Element mse = sci.next();
   
    // Process mse ...

    // Note how there is no si.next() again, since next() 
    // first gets current element and then advances iterator.

  }


Previous:MQL language
Up:Part II: APIs
Next:Statement