MQL equivalent to Python's None type? - page 2

 
nicholi shen:

MQL's first anonymous functions. ;) 

// https://www.mql5.com/ru/forum/227526/page3#comment_6567279
#define ForEach(Index, array) for (int Index = 0, max##Index = ArraySize(array); Index < max##Index; Index++)

#define  map(lambda_func, array) {\
  class _Lambda{ public : template <typename T> static T func(const T &Item){ return (T)(lambda_func);}}; \
   ForEach(i, array) \
      array[i] = _Lambda::func(array[i]);\
}

void OnStart ()
{
   int nums[] = { 1 , 2 , 3 , 4 };
   map(pow(Item, 2 ), nums);   //square each num in array
   map(Item - 5, nums);       //subtract 5 from the resulting nums in array
   ArrayPrint (nums); //-4  -1  4  11
   
   string hellos[] = {
       "Hello" , "World!" ,
       "Hello" , "World!"
   };
   map(Item== "Hello" ? Item+ " World!" : "Hello " +Item, hellos)
   ArrayPrint (hellos); //"Hello World!" "Hello World!" "Hello World!" "Hello World!" 
}
 
fxsaber:

Romantically beautiful and useful to me, thnx Fxsaber

 
fxsaber:

I I like it!


What do you guys think about the implementation of NONE(type)?

 
nicholi shen:

What do you guys think about the implementation of NONE(type)?

I do not use this opportunity. But your implementation looks fine.

 
nicholi shen:

I I like it!


What do you guys think about the implementation of NONE(type)?

I try to avoid static classes, would rather the plain #define if any.
 
fxsaber:
Pretty nice. But is there a reason for using the merge operator in the macro?
#define ForEach(Index, array) for (int Index = 0, max##Index = ArraySize(array); Index < max##Index; Index++)

Instead of:

#define ForEach(index,array) for(int index=0,max=ArraySize(array);index<max;index++)
 
Petr Nosek:
Pretty nice. But is there a reason for using the merge operator in the macro?

Instead of:

Reason:

Форум по трейдингу, автоматическим торговым системам и тестированию торговых стратегий

Обсуждение статьи "LifeHack для трейдера: замешиваем ForEach на дефайнах (#define)"

fxsaber, 2018.02.15 08:40

void OnStart()
{
  string array[] = {"12", "23", "34", "45"};
  
  ForEach(i, array)
  {
   Print(array[i]);
   
   ForEach(j, array)
     Print(array[j]);
  }    
}
 
fxsaber:

Reason:

Thanks, I got it. The reason is nesting.
 
Amir Yacoby:
I try to avoid static classes, would rather the plain #define if any.

The problem with using plain #defines is that they won't work with templates like the class version does. For example, if we were to templatize the ring buffer class then all we'd have to do is the following (snippet)


template<typename T>
bool CArrayRing::Init(int size, T volue=WRONG_VALUE)
{
   volue = volue == WRONG_VALUE ? NONE(T) : volue;
   m_last_pos=0;                          // last element position
   m_filling=volue;                       // value for buffer filling
   m_size=ArraySize(m_data);              // get size of the buffer   
   bool result=Resize(size);              // create a buffer with the desired size
   ArrayFill(m_data,0,m_size,m_filling);  // fill the buffer with default values
   return(result);
}
 
nicholi shen:

The problem with using plain #defines is that they won't work with templates like the class version does. For example, if we were to templatize the ring buffer class then all we'd have to do is the following (snippet)


This is why I said the std lib is not holy:
class CObject
  {
protected:
   double   None(const double)   const {return(double)"nan";}
   char     None(const char)     const {return CHAR_MAX;}
   uchar    None(const uchar)    const {return UCHAR_MAX;}
   int      None(const int)      const {return INT_MAX;}
   uint     None(const uint)     const {return UINT_MAX;}
   long     None(const long)     const {return LONG_MAX;}
   ulong    None(const ulong)    const {return ULONG_MAX;}
   short    None(const short)    const {return SHORT_MAX;}
   ushort   None(const ushort)   const {return USHORT_MAX;}
   datetime None(const datetime) const {return WRONG_VALUE;}
   color    None(const color)    const {return clrNONE;}
   string   None(const string)   const {return "__NULL-*-*-STRING__";}
  };

template<typename T>
class CArrayRing : public CObject
  {
public:
//+------------------------------------------------------------------+
//| Buffer initialization method.                                    |
//+------------------------------------------------------------------+
template<typename T>
bool CArrayRing::Init(int size,T volue=WRONG_VALUE)
  {
   volue=volue==WRONG_VALUE ? None(volue) : volue;
   m_last_pos=0;                          // last element position
   m_filling=volue;                       // value for buffer filling
   m_size=ArraySize(m_data);              // get size of the buffer   
   bool result=Resize(size);              // create a buffer with the desired size
   ArrayFill(m_data,0,m_size,m_filling);  // fill the buffer with default values
   return(result);
  }
};
Reason: