How to sort a range

2016-03-08 Thread rcorre via Digitalmars-d-learn
I was in a situation where I wanted to remove duplicates from an 
OnlyResult.
To do this with uniq, I needed to sort it. OnlyResult doesn't 
satisfy the template constraints of sort, but this seems easy 
enough to fix. I made front, back, and opIndex return by ref. 
With this, the following compiles:


assert(only(3,1,2).sort.equal(only(1,2,3)));

However, it fails with:

core.exception.AssertError@std/algorithm/sorting.d(1052): 
Failed to sort range of type OnlyResult!(int, 3LU)


So, if you have a range for which sort compiles, what does it 
take to make sorting actually work?


For reference, my two attempts were:

https://github.com/rcorre/phobos/commit/d89b3cfab7a0938e178a506b4ceb8faae6ecbfe2

https://github.com/rcorre/phobos/commit/512d9b8db6f311db6a9b6ccb077a691cec66ce70


D and devkitARM

2016-03-08 Thread TheGag96 via Digitalmars-d-learn
Hi guys, for a possibly-in-over-my-head project I'd like to get 
working a simple "Hello World" type program in which I call a D 
function from C in a 3DS homebrew app (or maybe even have it all 
in plain D with bindings to libctru). The thing is, I'm not 
skilled with Makefiles so I don't have a clue on how to correctly 
set up the toolchain for linking everything. I can compile some 
D-calling C code just fine with dmd and gcc on Linux (though I 
can't get anything working with gdc for whatever reason). Would 
anyone be able to point me in the right direction?


This is the example Makefile for a 3DS homebrew app: 
http://pastebin.com/fcJrrMg9 It seems like there would be issues 
differentiating the .d build definition files from D source 
files...


Thanks!




Re: Is it safe to use 'is' to compare types?

2016-03-08 Thread Anon via Digitalmars-d-learn

On Tuesday, 8 March 2016 at 20:26:04 UTC, Yuxuan Shui wrote:
On Monday, 7 March 2016 at 16:13:45 UTC, Steven Schveighoffer 
wrote:

On 3/4/16 4:30 PM, Yuxuan Shui wrote:
On Friday, 4 March 2016 at 15:18:55 UTC, Steven Schveighoffer 
wrote:

[...]


Thanks for answering. But I still don't understand why 
TypeInfo would
need to be allocated. Aren't typeid() just returning 
references to the

__DxxTypeInfo___initZ symbol?


You misunderstood, I meant the typeinfo *for* an allocated 
object, not that the typeinfo was allocated.


In some cases, 2 different objects allocated from different 
libraries (usually DLL-land) may reference TypeInfo from 
different segments, even though the TypeInfo is identical.


-Steve


Hmm... Does that mean each DLL will have their own TypeInfo 
symbols for the same type?


[Note: I phrase my answer in terms of Linux shared libraries 
(*.so) because D doesn't actually have proper Windows DLL support 
yet. The same would apply to DLLs, it just feels wrong describing 
functionality that doesn't exist.]


They can, mostly due to templated types. Consider modules 
`common`, `foo`, and `bar` (all built as shared libraries), and 
`main` (built as an executable).


module common; // => common.so
struct List(T)
{
// ...
}

module foo; // => foo.so, links to common.so
import common;

List!int getList()
{
// ...
}

module bar; // => bar.so, links to common.so
import common

void processList(List!int a)
{
// ...
}

module main; // => main, links to foo.so, bar.so, and common.so
import foo, bar;

void main()
{
processList(getList());
}

No part of List!int is instantiated in common, so no part of it 
is actually present in common.so. Instead, it is instantiated in 
foo and bar, and thus separate copies of List!int are present in 
foo.so and bar.so, along with TypeInfo for List!int.


If you were to statically link instead (using .a or .lib files), 
the linker would keep only one copy of List!int and its TypeInfo, 
but the linker can't eliminate either of them when dealing with 
shared libraries.


So, yes, I think the string comparison is needed, as awkward as 
it may seem in many circumstances.


Re: Needed return type in static method? bug or feature?

2016-03-08 Thread Jonathan M Davis via Digitalmars-d-learn
On Tuesday, March 08, 2016 14:56:06 Antonio Corbi via Digitalmars-d-learn 
wrote:
> On Tuesday, 8 March 2016 at 14:13:17 UTC, Adam D. Ruppe wrote:
> > On Tuesday, 8 March 2016 at 13:40:06 UTC, Antonio Corbi wrote:
> >> Is it a feature or a bug?
> >
> > It is allowed because the "auto" keyword doesn't actually
> > required for auto functions (or variables), what you need is
> > any one of the storage classes.
> >
> > Those include static, auto, const, immutable, even pure.
> >
> > If any of them are present, the compiler knows you are writing
> > a function or declaring a variable and will infer the type.
>
> Thank's Adam!.
>
> I had figured out something like this but I couldn't find
> anything in the docs
> (http://dlang.org/spec/attribute.html#static), moreover, the
> example there:
> --8><-
> class Foo
> {
>  static int bar() { return 6; }
> ...
> --8><-
>
> does mention the return type, that's what confused me.

The return type is optional so long as one of the keywords that indicates
that it's a variable or a function is there, so you can choose to put it or
not. In most cases, I think that folks put the return type on functions or
use auto, but it's up to you. Where it usually comes up is enums and
variable declarations.

- Jonathan M Davis



Re: Is it safe to use 'is' to compare types?

2016-03-08 Thread Steven Schveighoffer via Digitalmars-d-learn

On 3/8/16 3:26 PM, Yuxuan Shui wrote:

On Monday, 7 March 2016 at 16:13:45 UTC, Steven Schveighoffer wrote:

On 3/4/16 4:30 PM, Yuxuan Shui wrote:

On Friday, 4 March 2016 at 15:18:55 UTC, Steven Schveighoffer wrote:

[...]


Thanks for answering. But I still don't understand why TypeInfo would
need to be allocated. Aren't typeid() just returning references to the
__DxxTypeInfo___initZ symbol?


You misunderstood, I meant the typeinfo *for* an allocated object, not
that the typeinfo was allocated.

In some cases, 2 different objects allocated from different libraries
(usually DLL-land) may reference TypeInfo from different segments,
even though the TypeInfo is identical.


Hmm... Does that mean each DLL will have their own TypeInfo symbols for
the same type?


I don't know the exact circumstances. I think the answer is both yes and 
no, depending on the situation :)


But I know this is the reason for the string comparison.

-Steve


Re: Speed up `dub`.

2016-03-08 Thread Luis via Digitalmars-d-learn

On Tuesday, 8 March 2016 at 15:43:28 UTC, rcorre wrote:

On Monday, 7 March 2016 at 22:47:13 UTC, Luis wrote:



I try to grab dstep with dub fetch step (dub version 0.9.24, 
built on Aug 19 2015, on Ubuntu), and try to run dub build 
step. I can confirm that on my machine takes around 5 seconds 
to check if the dependencies are update.


I had this same experience. Thought dub would be a convenient 
way to install dstep, but dub run took so long I ended up just 
creating an alias to run the binary directly.


dub not should have something to install executables without the 
need of using dub run to execute it ? Like pip install does a 
true install of the package.


Re: Is it safe to use 'is' to compare types?

2016-03-08 Thread Yuxuan Shui via Digitalmars-d-learn
On Monday, 7 March 2016 at 16:13:45 UTC, Steven Schveighoffer 
wrote:

On 3/4/16 4:30 PM, Yuxuan Shui wrote:
On Friday, 4 March 2016 at 15:18:55 UTC, Steven Schveighoffer 
wrote:

[...]


Thanks for answering. But I still don't understand why 
TypeInfo would
need to be allocated. Aren't typeid() just returning 
references to the

__DxxTypeInfo___initZ symbol?


You misunderstood, I meant the typeinfo *for* an allocated 
object, not that the typeinfo was allocated.


In some cases, 2 different objects allocated from different 
libraries (usually DLL-land) may reference TypeInfo from 
different segments, even though the TypeInfo is identical.


-Steve


Hmm... Does that mean each DLL will have their own TypeInfo 
symbols for the same type?


Trying to build a Scheme interpreter in D

2016-03-08 Thread John via Digitalmars-d-learn

Hi,

I'm currently reading "Programming in D" and in order to get 
accustomed to D and it's syntax, I've decided to implement 
(actually port) a simple (and naive) Scheme interpreter from C to 
D. The original interpreter (in C) is described in a series of 
posts here:


http://peter.michaux.ca/articles/scheme-from-scratch-introduction

I've followed the tutorials and implemented various versions of 
the interpreter in different languages, and now I've decided to 
make a port to D. My source code resides in:


https://bitbucket.org/jfourkiotis/deimos

The basic object in my (naive, I repeat again) implementation, is 
the following:


alias Object = Algebraic!(
   long  , /* numbers   */
   bool  , /* #t or #f  */
   char  , /* characters*/
   string, /* symbols   */
   char[], /* strings   */
   EmptyList , /* Nil   */
   ConsCell  ,
   void function(This*, This*), /* primitive procedures */
   CompoundProc);   /* compound procedures  */

where the following type definitions hold:

struct EmptyList {}

class ConsCell {
Object car;
Object cdr;

this(Object car, Object cdr)
{
this.car = car;
this.cdr = cdr;
}
}

So, I have the following questions:

* For this kind of implementation, is the Algebraic type a good 
choice ? Is a simple union perhaps better ?
* I've defined the (recursive) Fibonacci function, for which DMD 
takes 30sec to calculate Fibonacci(30) and LDC takes 10sec. Is 
this a reasonable difference between the two compilers?
* I find it very difficult (actually impossible) to profile code 
in Mac OS X. There is no output for options -profile. Are there 
any other profiling/debugging tools for the Mac OS X ? My other 
ports (C++, Scala) run interpret the same example in under 2sec, 
so I would like to detect where my bottlenecks are.


Thanks.

ps: Any advice to make my code "better" (and more D-compliant) 
are appreciated.





Re: Speed up `dub`.

2016-03-08 Thread WebFreak001 via Digitalmars-d-learn

On Monday, 7 March 2016 at 21:49:59 UTC, xcvn wrote:
Yes but if he wants to build then `--describe` will not help 
that much !


dub uses that internally for finding the files for compiling, so 
thats the cause of the slowdown


Re: Needed return type in static method? bug or feature?

2016-03-08 Thread Antonio Corbi via Digitalmars-d-learn

On Tuesday, 8 March 2016 at 14:13:17 UTC, Adam D. Ruppe wrote:

On Tuesday, 8 March 2016 at 13:40:06 UTC, Antonio Corbi wrote:

Is it a feature or a bug?


It is allowed because the "auto" keyword doesn't actually 
required for auto functions (or variables), what you need is 
any one of the storage classes.


Those include static, auto, const, immutable, even pure.

If any of them are present, the compiler knows you are writing 
a function or declaring a variable and will infer the type.


Thank's Adam!.

I had figured out something like this but I couldn't find 
anything in the docs 
(http://dlang.org/spec/attribute.html#static), moreover, the 
example there:

--8><-
class Foo
{
static int bar() { return 6; }
...
--8><-

does mention the return type, that's what confused me.



Re: Needed return type in static method? bug or feature?

2016-03-08 Thread Adam D. Ruppe via Digitalmars-d-learn

On Tuesday, 8 March 2016 at 13:40:06 UTC, Antonio Corbi wrote:

Is it a feature or a bug?


It is allowed because the "auto" keyword doesn't actually 
required for auto functions (or variables), what you need is any 
one of the storage classes.


Those include static, auto, const, immutable, even pure.

If any of them are present, the compiler knows you are writing a 
function or declaring a variable and will infer the type.


Needed return type in static method? bug or feature?

2016-03-08 Thread Antonio Corbi via Digitalmars-d-learn

Hi all!

The following code compiles and works, but the static methods do 
not
have a return type. It also compiles and works if the appropiate 
(or auto)

return type is added to them.

-8><
import std.stdio;

class B {
int foo () { return 1; }

static sbar () { return "hi!"; }
static ibar () { return 0; }
}

void main () {
auto b = new B;
writeln (B.sbar);
writeln (B.ibar);
}
-8><
Is it a feature or a bug?

I've seen it being used in
https://github.com/gecko0307/dlib/blob/master/dlib/math/matrix.d

Thank's for your help!




Re: Memory Efficient HashSet

2016-03-08 Thread Edwin van Leeuwen via Digitalmars-d-learn

On Tuesday, 8 March 2016 at 08:12:04 UTC, Nordlöw wrote:
Has anybody put together a memory-efficient D-implementation of 
a HashSet


Something like

sparse_hash_set<> contained in

https://github.com/sparsehash/sparsehash

but in D.


There is an implementation in:
code.dlang.org/packages/emsi_containers

But to be honest I got stuck trying to use it (copy constructor 
disabled), so I used this very minimal wrapper around associative 
array:


private struct HashSet(E) {
// TODO switch to proper implementation (not using AA)
bool put( E el )
{
if ( el in set )
return false;
set[el] = set.length;
return true;
}
size_t[E] set;
}

(I only needed put, since I used it to check whether we already 
encountered a value before in a lazy/non sorted implementation of 
uniq)


Re: Missing Symbol Accessing Templated Function Through Interface

2016-03-08 Thread Peter via Digitalmars-d-learn

On Tuesday, 8 March 2016 at 12:24:06 UTC, crimaniak wrote:

On Tuesday, 8 March 2016 at 11:50:32 UTC, Peter wrote:

Hi,

Can anyone explain to me what's causing the following code to 
generate a missing symbol error...


Relevant comment: 
https://issues.dlang.org/show_bug.cgi?id=8553#c1


Thanks for link, makes sense.


Re: Memory Efficient HashSet

2016-03-08 Thread Ola Fosheim Grøstad via Digitalmars-d-learn

On Tuesday, 8 March 2016 at 08:12:04 UTC, Nordlöw wrote:

sparse_hash_set<> contained in

https://github.com/sparsehash/sparsehash


It appears to be very slow?
What do you need it for?



Re: Missing Symbol Accessing Templated Function Through Interface

2016-03-08 Thread crimaniak via Digitalmars-d-learn

On Tuesday, 8 March 2016 at 11:50:32 UTC, Peter wrote:

Hi,

Can anyone explain to me what's causing the following code to 
generate a missing symbol error...


Relevant comment: https://issues.dlang.org/show_bug.cgi?id=8553#c1



Re: Missing Symbol Accessing Templated Function Through Interface

2016-03-08 Thread Nicholas Wilson via Digitalmars-d-learn

On Tuesday, 8 March 2016 at 11:50:32 UTC, Peter wrote:

Hi,

Can anyone explain to me what's causing the following code to 
generate a missing symbol error...


import std.stdio;

interface IProblem {
   void writeln(T...)(T arguments);
}

class Problem : IProblem {
   void writeln(T...)(T arguments) {
  // This is just here to have code in an implementation.
  stdout.writeln("The implementation function was called.");
   }
}

IProblem getProblem() {
   return(new Problem());
}

void main() {
   auto problem = getProblem();

   problem.writeln("Some text and a number - ", 1234);
}

Now obviously this codes a bit contrived but its my attempt to 
reduce an issue I'm seeing to its more concise form. Note that 
if I cast the return value from the call to getProblem() to a 
Problem instance this rectifies the issue but I'm unclear as to 
why this might be. The error I get when I compile this is...


   Undefined symbols for architecture x86_64:
 "_D3app8IProblem18__T7writelnTAyaTiZ7writelnMFAyaiZv", 
referenced from:

 __Dmain in template_issue.o
   ld: symbol(s) not found for architecture x86_64
   clang: error: linker command failed with exit code 1 (use -v 
to see invocation)

   --- errorlevel 1
   dmd failed with exit code 1.

Apologies is the cause is blatantly obvious to more experienced 
D coders.


Regards,

Peter


Probably because the idea of a template function in an interface 
is

wrong, and I'm surprised that dmd accepts that. The reason is that
declaring a (non final)template function in an interface is like 
declaring a

function pointer to an uninstansiated(sp?) template function. Also
how many copies should appear in the vtbl for different template
argument combinations?
 Calling a templates on a class is fine, which explains why 
casting

solves the issue.

Solutions: if the function in the interface must take template 
args
parameterise the interface not the function (not sure if this 
works)

OR
if the function must take a variable number of args just use 
regular

runtime varadic arguments.

Nic


Missing Symbol Accessing Templated Function Through Interface

2016-03-08 Thread Peter via Digitalmars-d-learn

Hi,

Can anyone explain to me what's causing the following code to 
generate a missing symbol error...


import std.stdio;

interface IProblem {
   void writeln(T...)(T arguments);
}

class Problem : IProblem {
   void writeln(T...)(T arguments) {
  // This is just here to have code in an implementation.
  stdout.writeln("The implementation function was called.");
   }
}

IProblem getProblem() {
   return(new Problem());
}

void main() {
   auto problem = getProblem();

   problem.writeln("Some text and a number - ", 1234);
}

Now obviously this codes a bit contrived but its my attempt to 
reduce an issue I'm seeing to its more concise form. Note that if 
I cast the return value from the call to getProblem() to a 
Problem instance this rectifies the issue but I'm unclear as to 
why this might be. The error I get when I compile this is...


   Undefined symbols for architecture x86_64:
 "_D3app8IProblem18__T7writelnTAyaTiZ7writelnMFAyaiZv", 
referenced from:

 __Dmain in template_issue.o
   ld: symbol(s) not found for architecture x86_64
   clang: error: linker command failed with exit code 1 (use -v 
to see invocation)

   --- errorlevel 1
   dmd failed with exit code 1.

Apologies is the cause is blatantly obvious to more experienced D 
coders.


Regards,

Peter


Memory Efficient HashSet

2016-03-08 Thread Nordlöw via Digitalmars-d-learn
Has anybody put together a memory-efficient D-implementation of a 
HashSet


Something like

sparse_hash_set<> contained in

https://github.com/sparsehash/sparsehash

but in D.