www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Templates args

reply Andrey <avraliov gmail.com> writes:
Hi guys!

Help a newbie please.

Playing with D and trying to understand some features.
Here is my try to carry out my code from C++ project to D

struct Sigmoid(T)
{
   const T Function(T value)
   {
    ...
   }
   const T DerivateFunction(const T value)
   {
    ...
   }
}

struct Neurons_layer(T = float, size_t neurons_num = 0, F = 
Sigmoid!T)
   if(isFloatingPoint!T && is(typeof(F.Function)))
     {
     private:
       static if(neurons_num > 0)
	T[neurons_num] _neurons_arr;
       else
	T[] _neurons_arr;

     private:
       alias Function = F.Function;
}

unittest
{
   Neurons_layer!(float,5,Sigmoid!float) nf;
}


The question is - How to make in pretty way this line:
  Neurons_layer!(float,5,Sigmoid!float) nf;
Jul 14 2016
parent reply Andrey <avraliov gmail.com> writes:
On Thursday, 14 July 2016 at 19:27:14 UTC, Andrey wrote:
 Hi guys!

 Help a newbie please.

 Playing with D and trying to understand some features.
 Here is my try to carry out my code from C++ project to D

 struct Sigmoid(T)
 {
   const T Function(T value)
   {
    ...
   }
   const T DerivateFunction(const T value)
   {
    ...
   }
 }

 struct Neurons_layer(T = float, size_t neurons_num = 0, F = 
 Sigmoid!T)
   if(isFloatingPoint!T && is(typeof(F.Function)))
     {
     private:
       static if(neurons_num > 0)
 	T[neurons_num] _neurons_arr;
       else
 	T[] _neurons_arr;

     private:
       alias Function = F.Function;
 }

 unittest
 {
   Neurons_layer!(float,5,Sigmoid!float) nf;
 }


 The question is - How to make in pretty way this line:
  Neurons_layer!(float,5,Sigmoid!float) nf;
to something like - Neurons_layer!(float,5,Sigmoid) nf;
Jul 14 2016
next sibling parent reply Lodovico Giaretta <lodovico giaretart.net> writes:
On Thursday, 14 July 2016 at 19:28:23 UTC, Andrey wrote:
 On Thursday, 14 July 2016 at 19:27:14 UTC, Andrey wrote:
 Hi guys!

 Help a newbie please.

 Playing with D and trying to understand some features.
 Here is my try to carry out my code from C++ project to D

 struct Sigmoid(T)
 {
   const T Function(T value)
   {
    ...
   }
   const T DerivateFunction(const T value)
   {
    ...
   }
 }

 struct Neurons_layer(T = float, size_t neurons_num = 0, F = 
 Sigmoid!T)
   if(isFloatingPoint!T && is(typeof(F.Function)))
     {
     private:
       static if(neurons_num > 0)
 	T[neurons_num] _neurons_arr;
       else
 	T[] _neurons_arr;

     private:
       alias Function = F.Function;
 }

 unittest
 {
   Neurons_layer!(float,5,Sigmoid!float) nf;
 }


 The question is - How to make in pretty way this line:
  Neurons_layer!(float,5,Sigmoid!float) nf;
to something like - Neurons_layer!(float,5,Sigmoid) nf;
You don't need Sigmoid!float at all. This will work: Neurons_layer!(float, 5) nf; as you provided a default value for the third argument.
Jul 14 2016
parent Andrey <avraliov gmail.com> writes:
On Thursday, 14 July 2016 at 19:48:20 UTC, Lodovico Giaretta 
wrote:

 You don't need Sigmoid!float at all. This will work:

 Neurons_layer!(float, 5) nf;

 as you provided a default value for the third argument.
Yes, default init is present, but double, real types are desirable
Jul 14 2016
prev sibling next sibling parent reply ag0aep6g <anonymous example.com> writes:
On Thursday, 14 July 2016 at 19:28:23 UTC, Andrey wrote:
 On Thursday, 14 July 2016 at 19:27:14 UTC, Andrey wrote:
[...]
 struct Sigmoid(T)
 {
[...]
 }

 struct Neurons_layer(T = float, size_t neurons_num = 0, F = 
 Sigmoid!T)
   if(isFloatingPoint!T && is(typeof(F.Function)))
     {
[...]
     private:
       alias Function = F.Function;
 }

 unittest
 {
   Neurons_layer!(float,5,Sigmoid!float) nf;
 }


 The question is - How to make in pretty way this line:
  Neurons_layer!(float,5,Sigmoid!float) nf;
to something like - Neurons_layer!(float,5,Sigmoid) nf;
Make F an alias parameter: ---- struct Neurons_layer(T = float, size_t neurons_num = 0, alias F = Sigmoid) if(isFloatingPoint!T && is(typeof(F!T.Function))) { ... private: alias Function = F!T.Function; } unittest { Neurons_layer!(float,5,Sigmoid) nf; } ----
Jul 14 2016
parent Andrey <avraliov gmail.com> writes:
On Thursday, 14 July 2016 at 19:48:38 UTC, ag0aep6g wrote:

 Make F an alias parameter:

 ----
 struct Neurons_layer(T = float, size_t neurons_num = 0, alias F 
 = Sigmoid)
   if(isFloatingPoint!T && is(typeof(F!T.Function)))
 {
     ...
     private:
       alias Function = F!T.Function;
 }

 unittest
 {
   Neurons_layer!(float,5,Sigmoid) nf;
 }
 ----
WOW! It's works. Thanks. I like D more and more )))
Jul 14 2016
prev sibling parent Steven Schveighoffer <schveiguy yahoo.com> writes:
On 7/14/16 3:28 PM, Andrey wrote:
 On Thursday, 14 July 2016 at 19:27:14 UTC, Andrey wrote:
 struct Neurons_layer(T = float, size_t neurons_num = 0, F = Sigmoid!T)
   if(isFloatingPoint!T && is(typeof(F.Function)))
     {
     private:
       static if(neurons_num > 0)
     T[neurons_num] _neurons_arr;
       else
     T[] _neurons_arr;

     private:
       alias Function = F.Function;
 }

 unittest
 {
   Neurons_layer!(float,5,Sigmoid!float) nf;
 }


 The question is - How to make in pretty way this line:
  Neurons_layer!(float,5,Sigmoid!float) nf;
to something like - Neurons_layer!(float,5,Sigmoid) nf;
You can't pass an uninstantiated template type as a type, but you can pass as an alias: struct Neurons_layer(T = float, size_t neurons_num = 0, alias FTemp = Sigmoid) { alias F = FTemp!T; ... -Steve
Jul 14 2016