A few notes on choosing between Go and D for a quick project

Chris via Digitalmars-d digitalmars-d at puremagic.com
Wed Mar 18 05:19:54 PDT 2015


On Wednesday, 18 March 2015 at 01:52:00 UTC, Laeeth Isharc wrote:
> On Tuesday, 17 March 2015 at 21:00:11 UTC, bachmeier wrote:
>> On Tuesday, 17 March 2015 at 19:00:06 UTC, jmh530 wrote:
>>> In addition, further development of the ability to call D 
>>> from R or Python* or Julia (or vice-versa) would also be a 
>>> positive.
>>
>> What do you have in mind? I no longer work much with Python so 
>> my knowledge is limited, but calling D from R or Julia should 
>> be no different from calling C from those languages, as you 
>> normally compile your C code into a shared library anyway.
>>
>> I've done the R->D thing many times and in the process have 
>> worked with a big chunk of the R API. Things like allocating R 
>> data structures from a D function, adding assertions to your D 
>> code to allow for an easy exit when things don't work out, and 
>> calling functions in the R math library, among other things, 
>> are not difficult.
>
> PyD is pretty nice, although one wouldn't want to call it from 
> an inner loop.  And it's easy to go via cython also.  (I wonder 
> how well cython works with interfacing with D via the C++ 
> interface, because that way you could extend python with D 
> classes and have them be faster than going through PyD).
>
> PyD also works with numpy memoryviews I think.
>
> For Julia, easy to call D.  It would be nice to port julia.h to 
> D (I started, but got distracted) so D can call back Julia.

I once wrote a little test server with vibe.d and LuaD to have 
Lua server pages. It compiles the pages into Lua functions and 
executes them from memory. (Rikki gave me the idea). No use to 
parse every page when it's called.

It's not much code at all. It's only a test and very simple, not 
optimized or anything. The function in vibe.d that handled it 
looks like this:

/**
* Function that handles LSP using LuaD and a home made
* parser.
*/
void luaHandler(HTTPServerRequest req, HTTPServerResponse res) {

   auto url = URL(req.requestURL);

   // Set url arguments (note: should decode URL)
   lua["args"] = req.query;

   // If html/lsp page is not yet compiled, parse, compile and 
store it in _S
   if (!canFind(_S.keys, url.pathString[1..$])) {
     auto file = std.stdio.File("public/"~url.pathString[1..$], 
"r");
     string txt;
     foreach (line; file.byLine(KeepTerminator.yes)) {
       txt ~= to!string(line);  // to!string really necessary?
     }
     auto asLua = parseLua(txt);
     auto func = lua.loadString(asLua);
     _S[url.pathString[1..$]] = func;
   }
   // Call the compiled Lua function
   auto ret = _S[url.pathString[1..$]].call!(LuaTable)();
   string html;
   foreach(size_t i, string s; ret) {
     html ~= s;
   }
   res.writeBody(cast(ubyte[])html);
}


More information about the Digitalmars-d mailing list