static array of pointers to dynamic arrays of ints problem...

2018-04-22 Thread WhatMeForget via Digitalmars-d-learn
Surely a stupid mistake on my part, but why is the first array 
repeated?



import std.stdio;

void main()
{
int[]*[2] a;  // a static arrray holding pointers to dynamic 
arrays


static int unique = 0;

foreach(i, elem; a)
{
int[] temp = new int[](5);
foreach(ref element; temp)
{
element = unique;
unique++;
}
writeln("temp = ", temp);
a[i] = 
}

foreach(i, elem; a)
{
writeln(a[i].length);
writeln("[", i, "][]", *a[i]);
}
}

temp = [0, 1, 2, 3, 4]
temp = [5, 6, 7, 8, 9]
5
[0][][5, 6, 7, 8, 9]
5
[1][][5, 6, 7, 8, 9]


why use string for this example of appender?

2018-04-16 Thread WhatMeForget via Digitalmars-d-learn


I think I got a handle on D's static and dynamic arrays, till I 
come to std.array and see all the shiny new tools. I can 
understand all the replace.. functions, but the appender function 
gave me pause. The documentation says appender "Returns a new 
Appender or RefAppender initialized with a given array."


My first thought that doesn't D's built in arrays already allow 
appending? (at least for dynamic arrays)  And the example shows 
the use of appender with string.  Isn't string an immutable array 
or characters?  Wouldn't this be the last data type you would 
want to be appending to?  Another thing that had me wondering is 
the use of put() down below; doesn't the append syntax (~=) give 
you the same exact functionality; so why bother?


void main()
{
import std.array;
import std.stdio: write, writeln, writef, writefln;
auto w = appender!string;
// pre-allocate space (this avoids costly reallocations)
w.reserve(10);
assert(w.capacity >= 10);

w.put('a'); // single elements
w.put("bc"); // multiple elements

// use the append syntax
w ~= 'd';
w ~= "ef";

writeln(w.data); // "abcdef"


Re: Proposed Phobos equivalent of wcswidth()

2018-01-15 Thread WhatMeForget via Digitalmars-d

On Monday, 15 January 2018 at 13:34:09 UTC, Jack Stouffer wrote:

On Saturday, 13 January 2018 at 17:26:52 UTC, H. S. Teoh wrote:

...


Thanks for taking the time to do this.

And now the obligatory bikeshed: what should the Phobos 
equivalent of wcswidth be called?


std.utf.displayWidth


std.utf.bikeshed

Never heard that phrase before. Nice one :)


Re: Infuriating DUB/DMD build bug.

2017-10-07 Thread WhatMeForget via Digitalmars-d-learn

On Friday, 6 October 2017 at 23:02:56 UTC, Laeeth Isharc wrote:

On Thursday, 5 October 2017 at 21:48:20 UTC, WhatMeWorry wrote:


I've got a github project and using DUB with DMD and I keep 
running into this problem. I've tried deleting the entire 
...\AppData\Roaming\dub\packages folder, but the

problem repeats the very next build attempt.

[...]


See my post in learn on dmd path.  The dmd path code was 
written in 1987 and could do with an update to process longer 
windows paths properly.  We are working on this and I guess a 
chance a pull request on Monday but it depends on what else 
comes up.  In any case it's a trivial fix.


Presuming I am right about it being a path length problem.


I did! But i didn't say anything because i wasn't sure if they 
were related. I'm pretty sure it is path related because the 
exact same dub.sdl files work fine on Linux and MacOS. (It's a 
cross platform project)


Glad it is a trivial fix. Curious what it involves.  Let me know 
if I can help out in any way.  Mike Parker was kind enough to 
show me a manual dub local workaround for this issue. But I'll 
hold off now and see if your change does the fix.


If it does, it will be the best timed bug fix ever :)


Real beginner traits question

2017-09-24 Thread WhatMeForget via Digitalmars-d-learn


This is taken exactly from the traits documentation.



25 Traits

25.21 identifier

Takes one argument, a symbol. Returns the identifier for that 
symbol as a string literal.





There are no examples. My naive brain keeps thinking that a 
symbol and an identifier are the same things.  Can someone give 
me good definitions of "symbol" and "identifier".   And maybe an 
example if it is not too much trouble.


Re: What's the best D programming book

2017-08-25 Thread WhatMeForget via Digitalmars-d

On Friday, 25 August 2017 at 21:08:59 UTC, Macdonal wrote:

What is the best D-Programming Book?


In my opinion, if you are a beginner programmer I'd go with 
Ali's.  If intermediate, I would suggest Learning D by Mike 
Parker.  And if you want some cool idioms and advanced 
techniques, I'd go with Adam Ruppe's book.


Hope the authors agree with my assessment :)  They are all good 
in their own way.






Re: Mixed up over mixins.

2017-08-21 Thread WhatMeForget via Digitalmars-d-learn

On Sunday, 20 August 2017 at 22:50:40 UTC, Johnson Jones wrote:

On Sunday, 20 August 2017 at 19:27:43 UTC, WhatMeWorry wrote:

[...]



It's not difficult, it's just new. It's not that you are a poor 
programmer, but you simply have not learned how to think about 
mixins correctly. Stop whining about it and focus that energy 
on working with them.


[...]


Thank you.  You have rejuvenated my quest for mixin mastery :)


Re: Mixed up over mixins.

2017-08-21 Thread WhatMeForget via Digitalmars-d-learn

On Sunday, 20 August 2017 at 19:41:14 UTC, Ali Çehreli wrote:

On 08/20/2017 12:27 PM, WhatMeWorry wrote:

> // Mixins are for mixing in generated code into the
source code.
> // The mixed in code may be generated as a template
instance
> // or a string.

Yes, it means that the string must be legal D code.

> mixin(`writeln(` ~ `Hello`  ~ `);` );

Yes, that's a D string but the string itself is not legal D 
code because it would be mixing in the following:


writeln(Hello);

The problem is, there is no Hello defined in the program.

You need to make sure that Hello is a string itself:

writeln("Hello");

So, you need to use the following mixin:

mixin(`writeln(` ~ `"Hello"`  ~ `);` );

Ali


Of course, why didn't I "see" that before. I should have slept on 
it and tried again with fresh eyes.  I'm keeping a "beginners 
journal" on code generation.  Maybe write a 101 introduction with 
lots of samples and exercises.


Thanks. Don't know if you noticed, but i used some code from your 
book. Hope you take that as a complement.


real simple delegate question.

2017-08-17 Thread WhatMeForget via Digitalmars-d-learn


Can someone explain what is the difference between the two? 
Thanks.


module gates;
import std.stdio;
import std.random;

alias Calculator = int delegate(int);

Calculator makeCalculator()
{
static int context = 0;
int randy = uniform(1, 7);
context++;
writeln("context = ", context);
writeln("randy = ", randy);
return value => context + randy + value;
}

void main()
{
for (int i = 0; i < 3; i++)
{
auto calculator = makeCalculator();
writeln("The result of the calculation: ", calculator(0));
}
}
returns:
context = 1
randy = 5
The result of the calculation: 6
context = 2
randy = 2
The result of the calculation: 4
context = 3
randy = 6
The result of the calculation: 9

while the following

void main()
{
auto calculator = makeCalculator();  // thought just one 
would work

for (int i = 0; i < 3; i++)
{
writeln("The result of the calculation: ", calculator(0));
}
}
returns:
The result of the calculation: 3
The result of the calculation: 3
The result of the calculation: 3




Re: I'm the new package maintainer for D on ArchLinux

2017-08-13 Thread WhatMeForget via Digitalmars-d-announce

On Wednesday, 9 August 2017 at 20:50:36 UTC, Stefan Koch wrote:

On Wednesday, 9 August 2017 at 20:42:48 UTC, Wild wrote:


I hope I can maintain ArchLinux as a great environment to use 
D.




You are not only the new package mainainer but also my new Hero 
:)


+10


Real naive template question

2017-08-13 Thread WhatMeForget via Digitalmars-d-learn

module block_template;

void main()
{
template BluePrint(T, U)
{
T integer;
U floatingPoint;
}

BluePrint!(int, float);
}

// DMD returns
// template.d(13): Error: BluePrint!(int, float) has no effect

// I was expecting something like the following to be created 
after compilation:


module block_template;

void main()
{
{
int integer;
float floatingPoint;
}
}


I realize this is a corner case, but shouldn't the 
BluePrint!(int, float); statement blindly create a block of code?




I feel the dynamic array .sizeof property is kind of a bait and switch

2017-07-25 Thread WhatMeForget via Digitalmars-d-learn

Static Arrays have property
.sizeof which returns the array length multiplied by the number 
of bytes per array element.


Dynamic Arrays have property
.sizeof which returns the size of the dynamic array reference, 
which is 8 in 32-bit builds and 16 on 64-bit builds.


Why not have dynamic arrays with a .sizeof identical to static 
arrays and say another property called .sizeref which handles the 
32 or 64 bit references.


Maybe Phobos has something that I'm not aware of?

I've hand rolled a function which is working for me currently, 
but with my coding ability, I'd feel much safer with something 
official :)


It just seems like something this basic regarding dynamic arrays 
should just be built-in.















Re: Is it possible to generate a pool of random D or D inline assembler programs, run them safely?

2017-07-19 Thread WhatMeForget via Digitalmars-d-learn

On Tuesday, 18 July 2017 at 17:35:17 UTC, Enjoys Math wrote:


The purpose of it is to make a real-time, short-lived function 
predictor.


I'm genuinely curious. What is a function predictor used for?


Why doesn't this work...Struct std.regex.Captures

2017-07-09 Thread WhatMeForget via Digitalmars-d-learn

I was playing around with regex

import std.regex;
import std.stdio;

void main()
{
string[] fileNames = [ "011_bad.txt", "01_01_pass", 
"90_pass", "_bad" ];


auto reg = regex(r"^\d{2}_");

foreach (name; fileNames)
{
auto c = matchFirst(name, reg);

//writeln(c.hit);  // Slice of matched portion of input.
if (c.empty)
writeln(name, " is not the form we are looking for");
else
writeln(name, " is a winner");
}
}

All is good:
011_bad.txt is not the form we are looking for
01_01_pass is a winner
90_pass is a winner
_bad is not the form we are looking for


but then I uncommented the writeln(c.hit); above and all hell 
breaks loose:



core.exception.AssertError@/usr/include/dmd/phobos/std/regex/package.d(559): 
Assertion failure

??:? _d_assertp [0x8124936]
??:? pure nothrow @property @nogc @trusted immutable(char)[] 
std.regex.Captures!(immutable(char)[], uint).Captures.hit() 
[0x81190d6]

??:? _Dmain [0x80ff351]
??:? 
_D2rt6dmain211_d_run_mainUiPPaPUAAaZiZ6runAllMFZ9__lambda1MFNlZv 
[0x8125bbe]
??:? scope void rt.dmain2._d_run_main(int, char**, extern (C) int 
function(char[][])*).tryExec(scope void delegate()) [0x8125b10]
??:? scope void rt.dmain2._d_run_main(int, char**, extern (C) int 
function(char[][])*).runAll() [0x8125b7e]
??:? scope void rt.dmain2._d_run_main(int, char**, extern (C) int 
function(char[][])*).tryExec(scope void delegate()) [0x8125b10]

??:? _d_run_main [0x8125aa8]
??:? main [0x8122ff3]
??:? __libc_start_main [0xb74dc275]



I got inspiration from the following "live" code at at 
.../captures.html. I inserted a writeln(c.hit) and here it worked!


import std.range.primitives : popFrontN;

auto c = matchFirst("@abc#", regex(`(\w)(\w)(\w)`));
assert(c.pre == "@"); // Part of input preceding match
assert(c.post == "#"); // Immediately after match
assert(c.hit == c[0] && c.hit == "abc"); // The whole match
writeln("My Addition ",c.hit);
writeln(c[2]); // "b"
writeln(c.front); // "abc"
c.popFront();
writeln(c.front); // "a"
writeln(c.back); // "c"
c.popBack();
writeln(c.back); // "b"


and it worked fine:
My Addition abc
b
abc
a
c
b


What gives?  Thanks.




First time user of LDC and getting newbie problems.

2017-06-11 Thread WhatMeForget via Digitalmars-d-learn


Just trying to compile a "Hello World" using dub and ldc2. The 32 
bit works fine:


generic@generic-ThinkPad-T61:~/Desktop/test$ dub run 
--compiler=ldc2 --arch=x86

Performing "debug" build using ldc2 for x86.
test ~master: target for configuration "application" is up to 
date.

To force a rebuild of up-to-date targets, run again with --force.
Running ./test
hello there

But all hell breaks out when I try a 64 bit build. I've got a 64 
bit cpu and 64 bit Xubuntu so I thought, if I was going to get 
errors, it would be the previous case.



generic@generic-ThinkPad-T61:~/Desktop/test$ dub run 
--compiler=ldc2 --arch=x86_64

Performing "debug" build using ldc2 for x86_64.
test ~master: building configuration "application"...
/usr/bin/ld: skipping incompatible /usr/lib/libphobos2-ldc.so 
when searching for -lphobos2-ldc
/usr/bin/ld: skipping incompatible /usr/lib/libphobos2-ldc.a when 
searching for -lphobos2-ldc
/usr/bin/ld: skipping incompatible 
/usr/lib/gcc/i686-linux-gnu/6/../../../libphobos2-ldc.so when 
searching for -lphobos2-ldc
/usr/bin/ld: skipping incompatible 
/usr/lib/gcc/i686-linux-gnu/6/../../../libphobos2-ldc.a when 
searching for -lphobos2-ldc
/usr/bin/ld: skipping incompatible //usr/lib/libphobos2-ldc.so 
when searching for -lphobos2-ldc
/usr/bin/ld: skipping incompatible //usr/lib/libphobos2-ldc.a 
when searching for -lphobos2-ldc

/usr/bin/ld: cannot find -lphobos2-ldc
/usr/bin/ld: skipping incompatible /usr/lib/libdruntime-ldc.so 
when searching for -ldruntime-ldc
/usr/bin/ld: skipping incompatible /usr/lib/libdruntime-ldc.a 
when searching for -ldruntime-ldc
/usr/bin/ld: skipping incompatible 
/usr/lib/gcc/i686-linux-gnu/6/../../../libdruntime-ldc.so when 
searching for -ldruntime-ldc
/usr/bin/ld: skipping incompatible 
/usr/lib/gcc/i686-linux-gnu/6/../../../libdruntime-ldc.a when 
searching for -ldruntime-ldc
/usr/bin/ld: skipping incompatible //usr/lib/libdruntime-ldc.so 
when searching for -ldruntime-ldc
/usr/bin/ld: skipping incompatible //usr/lib/libdruntime-ldc.a 
when searching for -ldruntime-ldc

/usr/bin/ld: cannot find -ldruntime-ldc
collect2: error: ld returned 1 exit status
Error: /usr/bin/gcc failed with status: 1



Re: DCOnf 2017 videos online

2017-05-07 Thread WhatMeForget via Digitalmars-d-announce

On Sunday, 7 May 2017 at 18:11:47 UTC, Patrick Schluter wrote:
It looks like the good people of Sociomantic have already 
posted videos of the Dconf2017 on youtube.


https://www.youtube.com/channel/UC54uUlXuGhigMsdaNtP6THQ

Enjoy.


Wow. That was quick!


real simple manifest constant question probably regret asking...

2017-03-15 Thread WhatMeForget via Digitalmars-d-learn


One of my D books says: "an enum declared without any braces is 
called a manifest constant." The example shows,


enum string author = "Mike Parker";

Is this equivalent to
const string author = "Mike Parker";
or
immutable string author = "Mike Parker";

I guess what I'm asking is does enum give you some advantages 
over say non-enum constants?


Thanks.



Re: simple static if / traits question...

2017-02-22 Thread WhatMeForget via Digitalmars-d-learn
On Wednesday, 22 February 2017 at 22:37:25 UTC, Profile Anaysis 
wrote:
On Wednesday, 22 February 2017 at 21:27:47 UTC, WhatMeWorry 
wrote:



I'm doing conditional compilation using static ifs like so:

enum bool audio   = true;



// if audio flag is present and set to true, add to code build

static if ( (__traits(compiles, audio)) && audio)   

playSound(soundSys, BLEEP );


This works, but I thought there might be a simpler way. For 
instance,
after perusing std.traits, I thought I would find something 
like

isPresent(audio) or isSymbol(audio) templates.

Or am I being obtuse here?

Thanks.


You do realize that audio is a compile time constant? This 
means that in the binary, everything that depends on it is 
evaluated(as it can be, since it is known). This means that 
whatever app you are using will not be able to be able to 
"adapt" to the system changes. In this case, if the system has 
audio there is no way for the binary to use it because you 
compiled it out(if audio = false).


In such a case you do not want to use a static or compile time 
variable unless you plan on creating multiple binaries.


If your example above was just for demo then yes, you can do 
that but compiles is not what you want. Compiles only checks if 
the statement that follows is compilable as valid D code and it 
doesn't have anything to do with the value of the variables.




Definitely overthought this one big time.  But there is so much 
meta goodness in std.traits that I felt compelled to use 
"compiles" :)




There are a few options:

1. static if(audio)
2. version(audio)
3. if (audio)

It looks like you are trying to create the version(audio) 
semantic(if exists then use, else don't).


Ultimately, though, if you are trying to make a binary that can 
either use audio or not depending on where it is ran, you'll 
have to stick to using actual run time variables and probably 
be a bit more organized about it.


option 1 is the one I was shooting for. does the static if 
(audio) just check for the existence of audio, or does it also 
check to see if audio is true as well?


I've got a game tutorial with 15 sequential projects.  Each 
project introduces a new concept by building on the previous 
project code.  But I soon had so much
code duplication that I decided to use common modules/functions. 
The specific code could be injected via flags in the apps.d 
Hence my original question.


Re: Array start index

2017-02-06 Thread WhatMeForget via Digitalmars-d-learn

On Saturday, 1 August 2015 at 09:35:53 UTC, DLearner wrote:
Does the D language set in stone that the first element of an 
array _has_ to be index zero?
Wouldn't starting array elements at one avoid the common 
'off-by-one' logic error, it does

seem more natural to begin a count at 1.

Actually, maybe even better to allow array definitions of form
int foo[x:y];
(y >= x) creating integer variables foo[x], foo[x+1],...,foo[y].

I think the (very old) IBM PL/I language was like this.


There is a good metaphor in multi-story buildings. Americans 
number the first or ground floor of a high rise as floor #1.  In 
the UK, their first floor is what Americans would call the 2nd 
floor.


But then how can you trust a group of people who drive on the 
wrong side of the road :)







Re: Should I brush up on my C before plunging fully into D?

2016-10-15 Thread WhatMeForget via Digitalmars-d-learn

On Saturday, 15 October 2016 at 01:46:52 UTC, Chris Nelson wrote:
I'm mainly a scripting language, .NET, and SQL programmer. I've 
been looking for a good programming language for Linux/BSD 
other than Python. I've surveyed the options and D appears to 
be a sane modern choice for me. (Thanks Ali Çehreli and others!)


The only hitch is that many of the projects and libraries I'm 
interested in using or maybe porting are mainly C based. (My 
overall C-fu is weak...) Should I review a good C book or 
tutorial before jumping in to fully learning D? Or should I 
just eschew any C exposure until I master D?


(As a side note, many of the C libraries I'm interested in seem 
to be confusing messes of header files and "organic" code. But 
who am I to judge?)


I would jump right into D and then veer off into C on a need to 
know basis. This link might be of interest:


https://dlang.org/ctod.html

Also, are you aware of the libraries at the DUB registry?

And finally, Chapter 9 of Mike Parker's "Learning D" has lots of 
info on libraries.





Re: Examples of dub use needed

2016-06-23 Thread WhatMeForget via Digitalmars-d

On Thursday, 23 June 2016 at 07:46:41 UTC, Mike Parker wrote:
FWIW, this thread has inspired me to begin work on a project 
I've titled 'The DUB Handbook'. I've been meaning to write some 
tutorials about DUB (among other things) for learningd.org, but 
I think a detailed guide would be a more worthwhile project to 
pursue.


My intention with the text is to provide a detailed description 
of every dub command and configuration directive, along with 
examples of how to use them in both JSON and SDLang formats. 
I've spent an hour getting it set up today (I'm using gitbook) 
and expect to be working on it over the next several weeks. 
When it's ready for feedback, I'll make it publicly available 
and announce it here in the forums. I plan to release it under 
a CC license.


I've been trying to get my head around DUB for a long time. And 
that is even after buying D Web Development and your Learning D. 
I would be very interested in providing feedback.  I believe it 
would be very helpful to provide comprehensive "walk throughs" of 
the most useful and important scenarios. Include windows and 
Linux since their path layouts differ. Maybe even seed DUB's D 
Package Registry with a little tutorial package that could be 
used them.


And also throw in some definitions: what is actually meant by 
"cache", local, dependencies (is this imported source files or 
libraries needed during linking, or something else)


For instance, I installed DUB and some packages on a laptop 
(running Linux) about a week ago and then left it alone.  I'm now 
back in a terminal and it's like I've completely forgotten 
everything: I see the directories under ~/.dub but I can't seem 
to get dub list, dub fetch, or dub run  to work?


Anyway, sorry to ramble. Thank you for Handbook.