Re: How would you solve this 'interview' question in D?

2013-06-26 Thread Diggory
On Wednesday, 26 June 2013 at 22:43:05 UTC, David wrote: Am 26.06.2013 22:51, schrieb Gary Willoughby: Just for a bit of fun, I saw this question posted on reddit the other day and wondered how *you* would solve this in D? http://stackoverflow.com/questions/731832/interview-question-ffn-n I

Re: Passing Appender by value

2013-06-22 Thread Diggory
On Saturday, 22 June 2013 at 13:48:53 UTC, Andrej Mitrovic wrote: On 6/22/13, Andrej Mitrovic wrote: Appender!(int[]) buffer; call(buffer); writeln(buffer.data); // writes [], it's empty Apparently it's the same thing as the old AA problem. Essentially the buffer would have to

Re: randomShuffle

2013-06-21 Thread Diggory
On Thursday, 20 June 2013 at 21:48:49 UTC, Joseph Rushton Wakeling wrote: On 06/04/2013 01:03 PM, Diggory wrote: Still a few places to optimise, and I think the compiler optimisation should be able to give a decent speed up as well. Would be interested to see how it compares in your benchmark

Re: Is the -property compiler flag broken/bad idea?

2013-06-06 Thread Diggory
On Thursday, 6 June 2013 at 10:38:27 UTC, Martin Primer wrote: What would be good is: @property int foo { get() { _val = value; } set() { value = _val; } } usage: foo = 12; int bar = foo; my 2 cents. That's both much more limited and longer than the current

Re: Is the -property compiler flag broken/bad idea?

2013-06-06 Thread Diggory
On Thursday, 6 June 2013 at 00:56:22 UTC, Adam D. Ruppe wrote: On Thursday, 6 June 2013 at 00:08:31 UTC, Jonathan M Davis wrote: Which is essentially the position of weak property enforcement. Yes, though I wish we would stop calling it 'enforcement' because nothing actually needs to be espec

Re: randomShuffle

2013-06-04 Thread Diggory
Here's the fixed one: uint[] randomSample(uint N, uint M) { uint[] result = new uint[N]; struct hashPair { uint key; uint index; } size_t tableSize = N*4; if (tableSize > M) tableSize = M; hashPair[

Re: randomShuffle

2013-06-04 Thread Diggory
On Tuesday, 4 June 2013 at 08:30:58 UTC, Diggory wrote: On Monday, 3 June 2013 at 21:24:50 UTC, Joseph Rushton Wakeling wrote: On 06/03/2013 08:28 PM, Diggory wrote: I'd guess that the heavy use of floating point arithmetic to calculate the step sizes means that algorithm has a fairly

Re: randomShuffle

2013-06-04 Thread Diggory
On Monday, 3 June 2013 at 21:24:50 UTC, Joseph Rushton Wakeling wrote: On 06/03/2013 08:28 PM, Diggory wrote: I'd guess that the heavy use of floating point arithmetic to calculate the step sizes means that algorithm has a fairly large constant overhead even though the complexity is sm

Re: randomShuffle

2013-06-03 Thread Diggory
On Monday, 3 June 2013 at 17:35:22 UTC, Joseph Rushton Wakeling wrote: On 06/03/2013 07:00 PM, Diggory wrote: Thanks for testing before dismissing completely :P The way it returns results can be improved a lot by pre-allocating a range of the necessary size/using a range passed in. Surely

Re: randomShuffle

2013-06-03 Thread Diggory
On Monday, 3 June 2013 at 13:18:30 UTC, Joseph Rushton Wakeling wrote: On 06/03/2013 02:30 PM, Joseph Rushton Wakeling wrote: On 06/03/2013 01:29 PM, Diggory wrote: For small samples from very large ranges an efficient algorithm would be: int[] randomGen(int N, int M) { if (N == 0

Re: Structs should not contain pointers to internal data

2013-06-03 Thread Diggory
On Monday, 3 June 2013 at 16:00:58 UTC, Ali Çehreli wrote: On 06/03/2013 05:26 AM, Saurabh Das wrote: > Thank you @Ali and @Jonothan! > > So essentially since I will be storing a pointer, Telemetry!(T) is NOT safe > to use only with structs in general. > > If I have something like: > > struct Us

Re: randomShuffle

2013-06-03 Thread Diggory
On Monday, 3 June 2013 at 10:10:15 UTC, Yann wrote: Thanks a lot for the suggestions! Cheers, Yann On Monday, 3 June 2013 at 10:06:30 UTC, bearophile wrote: Yann: Is there a better way to accomplish this? Naively, I would expect something like "return iota(1, 1000).randomShuffle.take(10).so

Re: Are heap objects never moved by the garbage collector?

2013-06-01 Thread Diggory
On Saturday, 1 June 2013 at 08:11:05 UTC, sclytrack wrote: On Friday, 31 May 2013 at 16:31:39 UTC, Carl Sturtivant wrote: "The D Programming Language" (TDPL) p.178 asserts the following. "The objects themselves stay put, that is their locations in memory never change after creation." I ta

Re: Are heap objects never moved by the garbage collector?

2013-05-31 Thread Diggory
On Friday, 31 May 2013 at 17:14:52 UTC, Jonathan M Davis wrote: On Friday, May 31, 2013 18:31:38 Carl Sturtivant wrote: "The D Programming Language" (TDPL) p.178 asserts the following. "The objects themselves stay put, that is their locations in memory never change after creation." I take thi

Re: double vs real

2013-05-30 Thread Diggory
On Thursday, 30 May 2013 at 16:18:44 UTC, Shriramana Sharma wrote: Hello. I like that D exposes to me the real type to maximally utilize the machine's numerical precision. Since I am writing a program (currently in C++ but I am thinking of moving to D) that has to handle lots of fractional numb

Re: regex with literal (ie automatically replace '(' with '\(', etc) )

2013-05-30 Thread Diggory
Your suggestion does not work; try for yourself by replacing the $$ by \$ in my code. Is that a bug in std.regex' doc? eg: replace("",regex(``),`\$`); => invalid format string in regex replace However everything works fine with $$, see my code above. Either the doc or the code should probably

Re: regex with literal (ie automatically replace '(' with '\(', etc) )

2013-05-30 Thread Diggory
On Thursday, 30 May 2013 at 06:50:06 UTC, Timothee Cour wrote: ok, here it is: https://github.com/timotheecour/dtools/blob/master/dtools/util/util.d#L78 simplified implementation and added missing escape symbols. Any symbol missing? I was basing myself based on http://dlang.org/phobos/std_reg

Re: Consume an entire range

2013-05-29 Thread Diggory
On Thursday, 30 May 2013 at 04:18:14 UTC, Jonathan M Davis wrote: On Thursday, May 30, 2013 06:12:51 Diggory wrote: On Thursday, 30 May 2013 at 03:53:06 UTC, Brad Anderson wrote: > On Thursday, 30 May 2013 at 03:50:52 UTC, Brad Anderson > wrote: >> Is there a simple way to con

Re: Consume an entire range

2013-05-29 Thread Diggory
On Thursday, 30 May 2013 at 03:53:06 UTC, Brad Anderson wrote: On Thursday, 30 May 2013 at 03:50:52 UTC, Brad Anderson wrote: Is there a simple way to consume a range apart from std.array.array? I don't need to result of the range stored in an array, I just need a lazy map to evaluate complete

Re: regex with literal (ie automatically replace '(' with '\(', etc) )

2013-05-29 Thread Diggory
On Wednesday, 29 May 2013 at 23:33:30 UTC, timotheecour wrote: something like this, which we should have in std.regex: string escapeRegex(string a){ import std.string; enum transTable = ['[' : `\[`, '|' : `\|`, '*': `\*`, '+': `\+`, '?': `\?`, '(': `\(`, ')': `\)`]; return tra

Re: iteration over a string

2013-05-28 Thread Diggory
Most algorithms for strings need the offset rather than the character index, so: foreach (i; dchar c; str) Gives the offset into the string for "i" If you really need the character index just count it: int charIndex = 0; foreach (dchar c; str) { // ... ++charIndex; } If strings were t

Re: Immutability vs reference types

2013-05-27 Thread Diggory
On Tuesday, 28 May 2013 at 00:24:32 UTC, Francois Chabot wrote: Hello, I'm trying to get back into D again. This time around, I'm playing mostly with concurency and parallism, all the while trying to get my code writen in "the D way" as much as possible. I've run into a rather major road block

Re: lookup fields struct

2013-05-26 Thread Diggory
On Sunday, 26 May 2013 at 23:37:01 UTC, mimi wrote: Wow! thanks! "offsetof" will automatically distribute over the arguments so to get the offsets of a tuple you can just do: auto tmp = TP.offsetof; And "tmp" will be a tuple of the offsets.

Re: Mixin Templates Over-restricted?

2013-05-25 Thread Diggory
On Saturday, 25 May 2013 at 18:05:01 UTC, yaz wrote: Is there a reason for restricting mixin templates to only include declarations? For example, the following code doesn't work. (http://dpaste.dzfl.pl/1582a25e) Looking at the language specification (http://dlang.org/template-mixin.html) this d

Re: WindowProc in a class - function and pointer problem

2013-05-22 Thread Diggory
On Wednesday, 22 May 2013 at 20:25:40 UTC, Simen Kjaeraas wrote: On 2013-05-22, 21:30, D-sturbed wrote: Hello, is there a way to wrap a WindowProc (so "LRESULT WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) nothrow") in a class and to link it to a WindowClass without puting

Re: static analysis: how to test code reachability at compile time

2013-05-22 Thread Diggory
On Tuesday, 21 May 2013 at 21:15:58 UTC, Timothee Cour wrote: I'd like to have the following tools to help with static analysis & code coverage: 1) test whether code is reachable or not. Of course it only works for executables and may have some caveats (eg: goto), but its still useful diagn

Re: Enum of types

2013-05-20 Thread Diggory
On Tuesday, 21 May 2013 at 05:48:31 UTC, Ali Çehreli wrote: On 05/20/2013 09:58 PM, Diggory wrote: > On Tuesday, 21 May 2013 at 04:36:57 UTC, Ali Çehreli wrote: >> Sounds like std.variant.Algebraic: >> >> http://dlang.org/phobos/std_variant.html#.Algebraic >> &

Re: Enum of types

2013-05-20 Thread Diggory
On Tuesday, 21 May 2013 at 04:36:57 UTC, Ali Çehreli wrote: Sounds like std.variant.Algebraic: http://dlang.org/phobos/std_variant.html#.Algebraic Ali I don't see how I could use that. Algebraic is for storing a value, I'm trying to store just a type.

Enum of types

2013-05-20 Thread Diggory
I want to create an enum of some number of types, eg. { ubyte, ushort, uint, float } It should be straightforward to get a member of the enum from the type itself. Basically I have an object that stores some typed data, but the type may not be known at compile time, only the set of possible t

Re: Cross product template

2013-05-14 Thread Diggory
On Wednesday, 15 May 2013 at 01:31:43 UTC, Diggory wrote: I have a vector struct, Vector(T, uint N) templated on the type T and number of components, N. I'm trying to write a function "cross" which will calculate the cross product of a number of vectors. For a given number o

Cross product template

2013-05-14 Thread Diggory
I have a vector struct, Vector(T, uint N) templated on the type T and number of components, N. I'm trying to write a function "cross" which will calculate the cross product of a number of vectors. For a given number of components, N, the cross function should take N-1 arguments, each one a Ve

Re: Interface vs pure abstract class - speed.

2013-05-12 Thread Diggory
On Sunday, 12 May 2013 at 17:31:22 UTC, SundayMorningRunner wrote: Hello, let's say I have the choice between using an abstract class or an interface to declare a "plan", a "template" for the descendants. From the dmd compiler point of view, should I use the abstract class version (so I guess

Re: How to reserve memory for a slice in a struct

2013-05-07 Thread Diggory
You could allocate space inside a class itself with something like this: class Base { int[] slice; } template Derived(size_t N) { class Derived : Base { int[N] array; this() { slice = array; } } } Base b = new Derived!32(); A bit pointless thoug

Re: C Memory

2013-05-05 Thread Diggory
On Sunday, 5 May 2013 at 07:23:25 UTC, Namespace wrote: On Sunday, 5 May 2013 at 06:43:17 UTC, Diggory wrote: On Sunday, 5 May 2013 at 06:35:38 UTC, Namespace wrote: Quick question: I have a SDL_Surface in one of my classes and the SDL_Surface contains (obviously) memory to the pixel data

Re: C Memory

2013-05-04 Thread Diggory
On Sunday, 5 May 2013 at 06:35:38 UTC, Namespace wrote: Quick question: I have a SDL_Surface in one of my classes and the SDL_Surface contains (obviously) memory to the pixel data. Since I cannot free this memory with the DTor: what will happen? AFAIK this cannot be freed by the GC because it w

Re: Check if tuple contains value at compile time

2013-05-04 Thread Diggory
On Sunday, 5 May 2013 at 01:44:19 UTC, bearophile wrote: Diggory: The documentation seems too say that "[mytuple]" will make an array, Nope. You have to extract the inherent typetuple first. And this is what the [] syntax does (tested): import std.stdio, std.typecons, std.algor

Re: Check if tuple contains value at compile time

2013-05-04 Thread Diggory
On Sunday, 5 May 2013 at 00:33:34 UTC, bearophile wrote: Diggory: It's not a TypeTuple, it's a tuple of strings. Then one simple way to do it is to convert it into an array of strings, and then use canFind: [mytuple[]].canFind(needle) Bye, bearophile OK, that makes sense b

Re: Check if tuple contains value at compile time

2013-05-04 Thread Diggory
On Sunday, 5 May 2013 at 00:10:27 UTC, Simen Kjaeraas wrote: On 2013-05-05, 01:42, Diggory wrote: I'm trying to test using a "static if" statement if a tuple of strings contains a particular string. What's the easiest/best way to do this? http://dlang.org/phobos/std_type

Check if tuple contains value at compile time

2013-05-04 Thread Diggory
I'm trying to test using a "static if" statement if a tuple of strings contains a particular string. What's the easiest/best way to do this?

Re: D is totally useless

2013-05-02 Thread Diggory
The wgl*** functions and "SwapBuffers" ARE part of the windows api even though they are implemented in opengl32.dll (they are declared in wingdi.h IIRC)

Re: Win32 + OpenGL bindings

2013-04-22 Thread Diggory
you are doing it wrong. first you will need mingw32-make from mingw default setup, second fix win32 bindings makefile around line 25 so it will looks like this(supported file don't have uuid.di outdated interface file included) Yeah I realise what I was doing wrong now. It only coincidentally w

Re: Win32 + OpenGL bindings

2013-04-21 Thread Diggory
OK, thanks I think I've got the hang of the way linking works now - I was confused about how some things didn't seem to require libs to work, but now I see it's because those things were compiled into "druntime.lib" which was compiled into "phobos.lib". So is this correct, assuming I'm only ta

Re: Win32 + OpenGL bindings

2013-04-20 Thread Diggory
On Saturday, 20 April 2013 at 10:09:37 UTC, Jacob Carlborg wrote: On 2013-04-20 05:26, Diggory wrote: - derelict.opengl.gl: Has all of opengl plus wgl* functions needed for windows, but has more dependencies than an ideal opengl binding would have. Dependencies, like what? Well the opengl

Win32 + OpenGL bindings

2013-04-19 Thread Diggory
I've been trying to write a win32 app using opengl in D, but none of the bindings I've found have been sufficient: - core.sys.windows.windows: Missing large parts of wingdi.h, including PFD_* constants, ChoosePixelFormat(), SwapBuffers(), wgl*() functions. - win32.windows: Apparantly out of d