Re: Challenge: Make a data type for holding one of 8 directions allowing increment and overflow

2024-03-15 Thread Daniel N via Digitalmars-d-learn

On Tuesday, 12 March 2024 at 05:38:03 UTC, Liam McGillivray wrote:
I am in need of a data type for holding direction information; 
one of 8 directions on a single axis. They are named in terms 
of compass directions. If D had a 4-bit datatype, I would just 
use this and do `+=2` whenever I want to change the datatype, 
but it doesn't.


Perhaps this would be a good programming challenge for someone 
more experienced than me. Make a data type (probably a struct) 
for holding one of 8 directional values using 3 bits. It should 
accept the use of increment operators to change the angle.


Ideally (but optionally), it should work as an enum of the same 
name; "Direction".


Here's a unittest that it should ideally pass:


D actually supports both 3 and 4 bit integers. People will likely 
warn you of minor portability risks... but if you target a 
limited amount of platforms and prefer concise readable code, 
then it's a text book example for bitfields. The risk can easily 
be mitigated by having an unittest to catch the error directly(if 
you try to port to some exotic platform).


dmd -preview=bitfields

(Some lines stolen from Rikki)

```d
struct Direction
{
private uint value : 3;
alias this = value;

enum Direction N  = Direction(0);
enum Direction NE = Direction(1);
enum Direction E  = Direction(2);
enum Direction SE = Direction(3);
enum Direction S  = Direction(4);
enum Direction SW = Direction(5);
enum Direction W  = Direction(6);
enum Direction NW = Direction(7);
}
```


Re: gdc or ldc for faster programs?

2022-01-25 Thread Daniel N via Digitalmars-d-learn

On Tuesday, 25 January 2022 at 20:04:04 UTC, Adam D Ruppe wrote:

On Tuesday, 25 January 2022 at 19:52:17 UTC, Ali Çehreli wrote:

ldc: ~0.95 seconds
gdc: ~0.79 seconds
dmd: ~1.77 seconds




Maybe you can try --ffast-math on ldc.


Re: Trying to reduce memory usage

2021-02-13 Thread Daniel N via Digitalmars-d-learn

On Saturday, 13 February 2021 at 04:19:17 UTC, Ali Çehreli wrote:

On 2/11/21 6:22 PM, H. S. Teoh wrote:

>bool[size_t] hashes;

I would start with an even simpler solution until it's proven 
that there still is a memory issue:


import std.stdio;

void main() {
bool[string] lines;
foreach (line; stdin.byLine) {
if (line !in lines) {
stdout.writeln(line);
lines[line.idup] = true;
}
// else this line already seen before; just skip it
}
}

(Grr... Thanks for the tab characters! :p)

Ali


Try this Boost Licensed tool.
https://github.com/eBay/tsv-utils/tree/master/tsv-uniq




Re: Is garbage detection a thing?

2020-11-29 Thread Daniel N via Digitalmars-d-learn

On Sunday, 29 November 2020 at 16:35:26 UTC, Mark wrote:


Maybe I should just install Linux. But ... the drivers... My 
Thinkpad just doesn't like any Linux. I run out of ideas.


In the first place all I wanted to do is make some music.

Kind regards


You could try a linux image in VirtualBox or VMware, to more 
easily evaluate if linux + ASAN matches your expectations or if 
it's another dead-end.


Regards,
Daniel


Re: Is garbage detection a thing?

2020-11-29 Thread Daniel N via Digitalmars-d-learn

On Sunday, 29 November 2020 at 16:05:04 UTC, Mark wrote:


Thanks a lot for reading, and sorry for a lot of text that is 
off-topic and is not related to D.


Sounds like what you want is ASAN? You can use it with plain C or 
D(LDC).

https://clang.llvm.org/docs/AddressSanitizer.html



Re: Run code before dub dependency's `shared static this()`

2019-05-05 Thread Daniel N via Digitalmars-d-learn

On Sunday, 5 May 2019 at 08:24:29 UTC, Vladimirs Nordholm wrote:

Hello.

I have dub dependency which has a `shared static this()`.

In my project, can I run code code before the dependency's 
`shared static this()`?


This might work:

pragma(crt_constructor)
extern(C)
void early_init()
{
}


Re: bug in doc?

2019-03-14 Thread Daniel N via Digitalmars-d-learn

On Thursday, 14 March 2019 at 14:47:18 UTC, Adam D. Ruppe wrote:

On Thursday, 14 March 2019 at 14:22:52 UTC, spir wrote:

https://dlang.org/spec/hash-map.html#static_initialization:


Well, bug in implementation. That is *supposed* to work, but 
the compiler never implemented it.


The docs really should point out this fact explicitly, though.


Especially since it's hard to workaround inside function scope.



Re: Benchmarking sigmoid function between C and D

2018-04-07 Thread Daniel N via Digitalmars-d-learn
On Saturday, 7 April 2018 at 18:53:57 UTC, Arun Chandrasekaran 
wrote:
What am I doing wrong here that makes the D equivalent 2.5 
times slower than it's C equivalent?


Compilers used:

LDC2: LDC - the LLVM D compiler (1.8.0)
GCC: gcc (Ubuntu 5.4.0-6ubuntu1~16.04.9) 5.4.0 20160609

11:36:39 ~/code/c/test2$ ldc2 sigmoid.d -O5 && ./sigmoid


When benchmarking against C you need to add more switches to 
ldc2, like:

-release -boundscheck=off



Re: Looking for an equivalent to C++ std::getline in D

2017-05-08 Thread Daniel N via Digitalmars-d-learn

On Monday, 8 May 2017 at 10:51:52 UTC, Ola Fosheim Grøstad wrote:

On Sunday, 7 May 2017 at 20:50:10 UTC, Patrick Schluter wrote:
If you look on TIOBE [1] newest stats, D does not look so bad 
after all. It's ranked 23 with a 1.38% share. The so


Tiobe is a "hoax".

Stack overflow counts for alternative languages:

"swift": 146,374
"scala": 65,594
"go": 22,212
"rust": 6,596
"d": 2,211
"nim": 167


Stack-Overflow usage is clearly not representative of language 
usage.

1) Our forum is flourishing, why would any D developer use SO?
2) The number of questions is directly proportional with the 
difficulty of the language.(D is quite easy to learn, especially 
compared to rust).




Re: Mixin template confusion / compiler error.

2017-01-19 Thread Daniel N via Digitalmars-d-learn

On Thursday, 19 January 2017 at 08:41:53 UTC, Chris Katko wrote:

Thank you!

So:

1 - Is there any way TO get the output 64,64?


Would this work for you?

import std.meta;

alias sizer1D = AliasSeq!(64);
alias sizer2D = AliasSeq!(64,64);

array_t!sizer2D caseX;
array2_t!sizer1D caseY;



Re: Using Dub

2017-01-15 Thread Daniel N via Digitalmars-d-learn

On Sunday, 15 January 2017 at 13:23:25 UTC, Russel Winder wrote:
Is there any way of setting dub to default to ldc2 rather than 
dmd as the compiler of use? (I do not want to have to put 
--compiler ldc2 on every dub command.)


I have never used dub, but I know it's now also bundled with ldc2.

I would assume that if your PATH to ldc2 comes before that of 
dmd, it would find the ldc2 bundled version of dub and it would 
do the smart thing(if not, that's a bug).




Re: BetterC classes

2016-12-16 Thread Daniel N via Digitalmars-d-learn
On Friday, 16 December 2016 at 15:17:15 UTC, Ilya Yaroshenko 
wrote:

Hi

Is it possible to use classes, which do not have monitor and 
other DRuntime stuff?


Object can be allocated/deallocated using allocators, but they 
are very complex for betterC mode (monitor, mutex, object.d 
dependency).


Can we have something more primitive?

Ilya


extern(c++)
class

Works pretty good in my experience, at least it gets rid of the 
monitor, yay!




struct to json/yaml/xml/whatever codegen

2016-09-29 Thread Daniel N via Digitalmars-d-learn
Is anyone aware of a tool which does something akin to the 
following:


Given a C-like definition, automatically generate pure C code 
with no dependencies.


Input c-struct:
  struct Person
  {
int   id;
char* name;
  } 

Output minimal c-code:
  void dumpPerson(Person* p)
  {
  	printf("%d%s", p->id, 
p->name);	

  }

It's easy to write in D... just want to avoid reinventing the 
wheel.





Re: Is there a way to make a class variable visible but constant to outsiders, but changeable (mutable) to the class itself?

2016-05-22 Thread Daniel N via Digitalmars-d-learn

On Saturday, 21 May 2016 at 17:32:47 UTC, dan wrote:
Is it possible to have a class which has a variable which can 
be seen from the outside, but which can only be modified from 
the inside?


Something like:

class C {
  int my_var = 3; // semi_const??
  void do_something() { my_var = 4; }
}



Yes, I prefer this idiom:

class C
{
  union
  {
private  int  var_rw;
public const(int) var_ro;
  }
}



Re: Is this template constraint a bug?

2016-05-12 Thread Daniel N via Digitalmars-d-learn

On Thursday, 12 May 2016 at 15:33:24 UTC, Eric wrote:


is(T : A!T) tells if T can automatically be converted to A!T.  
The
last line below is doing just that, yet the template constraint 
does not work.


class A(T) if (is(T : A!T))
{
}



Yes, it's a bug. Please file an issue.

Meanwhile try this workaround:
class A(T)
{
  static assert(is(T : A!T), "...");
}



Re: mixed-in ctor not on par with explicit one?

2016-01-13 Thread Daniel N via Digitalmars-d-learn
On Wednesday, 13 January 2016 at 12:39:36 UTC, Jacob Carlborg 
wrote:

On 2016-01-13 10:48, Shriramana Sharma wrote:


This is not what alias <> this is supposed to do, right?


No.


So how am I supposed to get the mixed in ctors work?


Looks like a limitation in the language.


This works:

mixin template myCtors()
{
this(int i) {}
this(char c) {}
}
struct Test
{
this(string s) {}

mixin myCtors  mixed;
alias __ctor = mixed.__ctor;
}
void main()
{
auto a = Test("hello");
auto b = Test(1);
auto c = Test('c');
}



Re: Capturing __FILE__ and __LINE in a variadic templated function

2015-11-02 Thread Daniel N via Digitalmars-d-learn

On Monday, 2 November 2015 at 09:54:50 UTC, John Colvin wrote:

Why can't I make Args a sequence of aliases?


Works for me on multiple compilers. To be precise, this worked:



Except it prints Arg instead of x, try:
debug write(Args[i].stringof, " is ", Arg);



Re: Capturing __FILE__ and __LINE in a variadic templated function

2015-11-02 Thread Daniel N via Digitalmars-d-learn

On Monday, 2 November 2015 at 11:36:27 UTC, Nordlöw wrote:

I want it to print the name of Arg in the closing as

x is 11


See my previous comment:
Arg -> Args[i].stringof



Re: friends with phobos, workaround?

2015-09-11 Thread Daniel N via Digitalmars-d-learn
On Thursday, 10 September 2015 at 13:19:08 UTC, Adam D. Ruppe 
wrote:

On Thursday, 10 September 2015 at 08:22:29 UTC, Daniel N wrote:
  this(string caller = __MODULE__)(int val) if(caller == 
"std.conv") // Use scoped!Awesome


That's disgustingly genius. I'm a bit jealous I didn't 
think of it myself!


One slight problem though: you couldn't call super() from a 
derived class, since the constructor wouldn't even exist due to 
the constraint.




super():
Hmm, yes that's a slight nuisance but manageable.

Thanks, coming from you that means a lot. :) Hmm, maybe I'll wait 
a week or two before I share my next trick to give you ample time 
to think of it also, meanwhile I'll just scribble it down in the 
margin of my copy of "D Cookbook". ;) ... just hope I didn't jinx 
myself now ...




Re: Huge output size for simple programs

2015-09-11 Thread Daniel N via Digitalmars-d-learn

On Friday, 11 September 2015 at 11:15:33 UTC, NX wrote:

I compile a simple hello world program in C and the results:

hello_world.o -> 1.5 KB
hello_world (linux executable) -> 8.5 KB




If you care about binary sizes, use ldc2:
ldc 225544 bytes (stripped + writeln)
ldc 175736 bytes (stripped + puts)

with tricks you can get lower, but that's out of the box...


Re: friends with phobos, workaround?

2015-09-10 Thread Daniel N via Digitalmars-d-learn

On Thursday, 10 September 2015 at 08:22:29 UTC, Daniel N wrote:

import std.typecons;

class Awesome1
{
private:
  int val;
  this(string caller = __MODULE__)(int val) if(caller == 
"std.conv") // Use scoped!Awesome

  {
this.val = val;
  }
}

class Awesome2
{
private:
  int val;
  this(string caller = __MODULE__)(int val)
  {
  	static assert(caller == "std.conv", "Use 
scoped!Awesome(...)!");


this.val = val;
  }
}

void main()
{
  static assert(__traits(compiles, scoped!Awesome1(1)));
  static assert(__traits(compiles, scoped!Awesome2(1)));
  static assert(!__traits(compiles, new Awesome1(1)));
  static assert(!__traits(compiles, new Awesome2(1)));
}


PS 'private:' should of course be 'public:' when using this idiom.



Re: friends with phobos, workaround?

2015-09-10 Thread Daniel N via Digitalmars-d-learn

On Wednesday, 9 September 2015 at 23:44:14 UTC, Idan Arye wrote:
How about using a mixin 
template(http://dlang.org/template-mixin.html)?




Thanks, it's a good solution. My only reservation is I would 
prefer to find a way to directly invoke a symbol in std.* as 
otherwise different frameworks might invent their own 
conventions(well they might do that anyway). After a nights 
sleep, I actually found such a solution!


This opens the door to a new class of meta-programming; 
introspect the caller! :)


import std.typecons;

class Awesome1
{
private:
  int val;
  this(string caller = __MODULE__)(int val) if(caller == 
"std.conv") // Use scoped!Awesome

  {
this.val = val;
  }
}

class Awesome2
{
private:
  int val;
  this(string caller = __MODULE__)(int val)
  {
  	static assert(caller == "std.conv", "Use 
scoped!Awesome(...)!");


this.val = val;
  }
}

void main()
{
  static assert(__traits(compiles, scoped!Awesome1(1)));
  static assert(__traits(compiles, scoped!Awesome2(1)));
  static assert(!__traits(compiles, new Awesome1(1)));
  static assert(!__traits(compiles, new Awesome2(1)));
}





friends with phobos, workaround?

2015-09-09 Thread Daniel N via Digitalmars-d-learn
For the record, I think D made the right decision... omitting 
friends.


However there's one case in particular which I find useful, 
anyone see a good workaround for this?


#include 

class Friendly
{
private:
  int val;
  Friendly(int&& val) : val(val) {}
  friend std::unique_ptr 
std::make_unique(int&& val);

};

int main()
{
  auto yay = std::make_unique(1);
  auto nay = new Friendly(1);
}



Re: Array initialization with Struct templates

2015-08-31 Thread Daniel N via Digitalmars-d-learn

On Monday, 31 August 2015 at 05:38:54 UTC, Jonathan M Davis wrote:
So, you're going to need to pass it a Chameleon!(float, 
purpose.POSITIONAL) and a Chameleon!(float, purpose.COLOR_ONLY 
color), not 6 doubles - either that, or you're going to need to 
declare a constructor for VertexData which takes 6 doubles or 
floats and converts them to what's require to assign to its 
member variables.


- Jonathan M Davis


Or turn Chameleon into a mixin template.

enum Purpose { POSITIONAL, COLOR_ONLY, COLOR_AND_ALPHA, 
GENERIC_TRIPLE, GENERIC_QUAD }

Purpose purpose;

mixin template Chameleon(T, Purpose p) // mixin template
{
static if (p == Purpose.POSITIONAL) {  // <-- NOT is 
expression

T  x, y, z;
} else static if (p == Purpose.COLOR_ONLY) {
T  r, g, b;
} else static if (p == Purpose.COLOR_AND_ALPHA) {
T  r, g, b, a;
}  else static if (p == Purpose.GENERIC_TRIPLE) {
T  a, b, c;
} else static if (p == Purpose.GENERIC_QUAD) {
T  a, b, c, d;
}
};

struct VertexData
{
  mixin Chameleon!(float, purpose.POSITIONAL) position;
  mixin Chameleon!(float, purpose.COLOR_ONLY) color;
}

alias Vert = VertexData;

VertexData[] vertices =
[
Vert(1.0f, 1.0f, 1.0f, 0.0, 0.0, 0.0),
];

void main()
{}


Re: [Rosettacode] sum of powers conjecture

2015-07-26 Thread Daniel N via Digitalmars-d-learn

On Sunday, 26 July 2015 at 18:40:59 UTC, bearophile wrote:
I've translated the C++ entry to D as third D entry, but it's 
not a good translation, I've just converted iterators to 
pointers instead of using ranges (the resulting speed is 
acceptable). You're welcome to improve it:


http://rosettacode.org/wiki/Euler%27s_sum_of_powers_conjecture#Third_version

Bye,
bearophile


Bear, welcome back! You and all your inspirational ideas have 
been missed! Good to see you.


Re: Mixin template functions are ignored in struct

2014-10-14 Thread Daniel N via Digitalmars-d-learn

On Tuesday, 14 October 2014 at 20:58:19 UTC, tcak wrote:
I have written a struct and a mixin template, and that mixin 
template is mixed into that struct as follows.




Use a normal mixin + token strings(q{}).

enum TestCommonMethods = q{
public bool apply( int d, int e ){
return false;
}
};

public struct Test{
mixin(TestCommonMethods);

public bool apply(char c){
return true;
}
}

void main(){
Test t;
t.apply( 5, 3 );
}