On Saturday, 18 July 2015 at 13:56:36 UTC, Adam D. Ruppe wrote:
On Saturday, 18 July 2015 at 13:48:20 UTC, Clayton wrote:
There seems to be a lot of mutation happening here yet I have heard no mutation should take place in meta-programming as it subscribes to functional programming paradigm.

That's not true in D, you can just write a regular function and evaluate it in a compile time context, like initializing a static variable.

You usually don't need to write special code for compile time stuff in D.


Thanks , you were right . It seems there are some key words though which one has to use so that the code gets executed on compile-time .For example I had to change the second forloop to a foreach loop, and then put and enum to ensure that TableFromCompiler gets evaluated at compiletime. Having written the code this way though gives rise to some other question, D supports 2 approches to compiletime metaprogramming i.e. CTFE and Templates, it seems am not very sure which paradigm my code falls in.


import std.stdio;
import std.string;
import std.conv;


I[C]  computeAtCompileTime(S ,C,I)( const S  pattern ){
  I[C] table1;

const int size = to!int(pattern.length) ;//Length of the pattern to be matched

  foreach( c; ALPHABET){   //Initialise array
          table1[c] = size;
    }

  foreach(i; 0..size-1){
             table1[pattern[i]] = size -i-1;
  }
  return table1;
}

void main(){

enum TableFromCompiler = computeAtCompileTime!(const string ,char, int)(pattern);

     writeln(TableFromCompiler);
 }

Reply via email to