Re: Shortest way to allocate an array and initialize it with a specific value.

2015-06-11 Thread via Digitalmars-d-learn

On Thursday, 11 June 2015 at 07:57:47 UTC, Per Nordlöw wrote:

On Wednesday, 10 June 2015 at 22:03:52 UTC, Ali Çehreli wrote:

Another option:

void main()
{
   auto a2 = new ubyte[5];


But this causes an extra zero-initialization of a2.


   a2[] = 0xAA;// -- Assign to all elements


Is

auto a2 = value.repeat(size).array;

better in this regard?


Yes, it uses `uninitializedArray()` if the length is known (which 
it is for `repeat()`):

https://github.com/D-Programming-Language/phobos/blob/master/std/array.d#L111


Re: Encapsulate return value in scoped

2015-06-11 Thread Daniel Kozák via Digitalmars-d-learn

On Thu, 11 Jun 2015 09:01:04 +
Yuxuan Shui via Digitalmars-d-learn digitalmars-d-learn@puremagic.com
wrote:
   A x = scoped!A(10);

use auto x = scoped!A(10);


Re: What is D's minimum requirements on Mac?

2015-06-11 Thread Kagamin via Digitalmars-d-learn

Whee, $99/year.


Encapsulate return value in scoped

2015-06-11 Thread Yuxuan Shui via Digitalmars-d-learn

Is there a way to encapsulate return value into scoped?

Say I have a function that returns a new object:

X new_x(T t...) {
//Super complex input processing
return new X(something);
}

And I want to encapsulate the result using scoped, is that 
possible? Can I just do:

return scoped!X(something).
?

If I understand correctly, the data will be blitted, but the 
destructor will be called, so the returned object will be in 
invalid state.


Re: Encapsulate return value in scoped

2015-06-11 Thread Yuxuan Shui via Digitalmars-d-learn

On Thursday, 11 June 2015 at 08:48:22 UTC, Yuxuan Shui wrote:

Is there a way to encapsulate return value into scoped?

Say I have a function that returns a new object:

X new_x(T t...) {
//Super complex input processing
return new X(something);
}

And I want to encapsulate the result using scoped, is that 
possible? Can I just do:

return scoped!X(something).
?

If I understand correctly, the data will be blitted, but the 
destructor will be called, so the returned object will be in 
invalid state.

It's even weirder than I thought, this:

import std.stdio,
   std.typecons;
class A {
int b;
~this() {
writeln(Des);
}
this(int x) {
b=x;
writeln(Cons);
}
}
auto x() {
A x = scoped!A(10);
writeln(x.b);
writeln(Return x);
return x;
}
void main() {
auto tx = x();
writeln(Return main);
}

Produce output:
Cons
Des
0
Return x
Return main

Which I totally don't understand.


Re: Shortest way to allocate an array and initialize it with a specific value.

2015-06-11 Thread via Digitalmars-d-learn

On Thursday, 11 June 2015 at 08:33:46 UTC, Daniel Kozák wrote:

On Wed, 10 Jun 2015 20:22:17 +
Adel Mamin via Digitalmars-d-learn 
digitalmars-d-learn@puremagic.com

wrote:


ubyte[5] a = 0xAA; // Fine. Five 0xAA bytes.
auto a2 = new ubyte[5]; // Fine. Five 0 bytes.
Now, let's say, I want to allocate an array of a size, derived 
at run time, and initialize it to some non-zero value at the 
same time. What would be the shortest way of doing it?


import std.stdio;

struct Ubyte(ubyte defval) {
ubyte v = defval;
alias v this;
}

void main() {
auto a2 = new Ubyte!(0xAA)[5];
writeln(a2);
}


I like this one :-)


Re: What is D's minimum requirements on Mac?

2015-06-11 Thread Kagamin via Digitalmars-d-learn

On Wednesday, 10 June 2015 at 18:55:27 UTC, Adam D. Ruppe wrote:
I'm still tempted to grab a used Mac so I can port my display 
stuff to Cocoa and test it, but Macs are outrageously expensive 
and I hate them, so want to spend as little as possible.


What does dmd minimally require on a mac? If I got like a 10.5 
would that work?


i'm considering something like
http://www.amazon.com/Apple-MB138LL-Intel-Drive-Combo/dp/B0006HU49Y/ref=sr_1_5?ie=UTF8qid=1433962021sr=8-5keywords=used+mac+mini


You can try to register as a developer: 
https://developer.apple.com/programs/ and get beta versions of 
OSX and install them on virtual box. Not sure how much it costs.


Re: Shortest way to allocate an array and initialize it with a specific value.

2015-06-11 Thread Daniel Kozák via Digitalmars-d-learn

On Thu, 11 Jun 2015 11:43:25 +
via Digitalmars-d-learn digitalmars-d-learn@puremagic.com wrote:

 On Thursday, 11 June 2015 at 08:33:46 UTC, Daniel Kozák wrote:
  On Wed, 10 Jun 2015 20:22:17 +
  Adel Mamin via Digitalmars-d-learn 
  digitalmars-d-learn@puremagic.com
  wrote:
 
  ubyte[5] a = 0xAA; // Fine. Five 0xAA bytes.
  auto a2 = new ubyte[5]; // Fine. Five 0 bytes.
  Now, let's say, I want to allocate an array of a size, derived 
  at run time, and initialize it to some non-zero value at the 
  same time. What would be the shortest way of doing it?
 
  import std.stdio;
 
  struct Ubyte(ubyte defval) {
  ubyte v = defval;
  alias v this;
  }
 
  void main() {
  auto a2 = new Ubyte!(0xAA)[5];
  writeln(a2);
  }
 
 I like this one :-)

small enhancment:
struct Ubyte(ubyte defval = 0)




Re: Python's features, which requires D

2015-06-11 Thread Dennis Ritchie via Digitalmars-d-learn

On Saturday, 23 May 2015 at 10:58:33 UTC, Kagamin wrote:

On Saturday, 23 May 2015 at 02:36:14 UTC, Dennis Ritchie wrote:

For example, the code in Python looks quite natural:

a = [[int(j) for j in input().split()] for i in range(n)]

About D-code, I can not say:


auto a = stdin
.byLine
.map!(l = l.splitter.map!(to!int).array)
.take(n);


Well, list comprehension is built into language in python (and 
not in D), such level of support is definitely more streamlined.


Yes, but D is also possible to create a strong inclusion of list 
comprehensions. Here's the proof:

https://github.com/pplantinga/delight

Probably the coolest feature in Delight is list comprehensions. 
Delight uses functions in std.algorithm to generate an iterable 
range. The syntax is similar to Python's. See the last two 
lines of the code above for an example.


print { i * 2 for i in 0 .. 5 where i ^ 2 less than 5 }
# prints [0, 2, 4]


Re: Shortest way to allocate an array and initialize it with a specific value.

2015-06-11 Thread Steven Schveighoffer via Digitalmars-d-learn

On 6/11/15 7:51 AM, Daniel Kozák via Digitalmars-d-learn wrote:


On Thu, 11 Jun 2015 11:43:25 +
via Digitalmars-d-learn digitalmars-d-learn@puremagic.com wrote:


On Thursday, 11 June 2015 at 08:33:46 UTC, Daniel Kozák wrote:

On Wed, 10 Jun 2015 20:22:17 +
Adel Mamin via Digitalmars-d-learn
digitalmars-d-learn@puremagic.com
wrote:


ubyte[5] a = 0xAA; // Fine. Five 0xAA bytes.
auto a2 = new ubyte[5]; // Fine. Five 0 bytes.
Now, let's say, I want to allocate an array of a size, derived
at run time, and initialize it to some non-zero value at the
same time. What would be the shortest way of doing it?


import std.stdio;

struct Ubyte(ubyte defval) {
 ubyte v = defval;
 alias v this;
}

void main() {
auto a2 = new Ubyte!(0xAA)[5];
writeln(a2);
}


I like this one :-)


small enhancment:
struct Ubyte(ubyte defval = 0)


import std.typecons;

alias Ubyte(ubyte defval = 0) = Typedef!(ubyte, defval);

I personally like the range solution the best, it has the most flexibility.

-Steve


Re: Encapsulate return value in scoped

2015-06-11 Thread Yuxuan Shui via Digitalmars-d-learn

On Thursday, 11 June 2015 at 09:11:47 UTC, Daniel Kozák wrote:


On Thu, 11 Jun 2015 09:01:04 +
Yuxuan Shui via Digitalmars-d-learn 
digitalmars-d-learn@puremagic.com

wrote:

A x = scoped!A(10);


use auto x = scoped!A(10);


Thanks!

Curious question, why doesn't compiler reject this code?


Re: Encapsulate return value in scoped

2015-06-11 Thread Steven Schveighoffer via Digitalmars-d-learn

On 6/11/15 1:28 PM, Yuxuan Shui wrote:

On Thursday, 11 June 2015 at 09:11:47 UTC, Daniel Kozák wrote:


On Thu, 11 Jun 2015 09:01:04 +
Yuxuan Shui via Digitalmars-d-learn digitalmars-d-learn@puremagic.com
wrote:

A x = scoped!A(10);


use auto x = scoped!A(10);


Thanks!

Curious question, why doesn't compiler reject this code?


Because scoped!A implicitly casts to A.

-Steve


Re: Encapsulate return value in scoped

2015-06-11 Thread Yuxuan Shui via Digitalmars-d-learn
On Thursday, 11 June 2015 at 17:34:56 UTC, Steven Schveighoffer 
wrote:

On 6/11/15 1:28 PM, Yuxuan Shui wrote:

On Thursday, 11 June 2015 at 09:11:47 UTC, Daniel Kozák wrote:


On Thu, 11 Jun 2015 09:01:04 +
Yuxuan Shui via Digitalmars-d-learn 
digitalmars-d-learn@puremagic.com

wrote:

   A x = scoped!A(10);


use auto x = scoped!A(10);


Thanks!

Curious question, why doesn't compiler reject this code?


Because scoped!A implicitly casts to A.

-Steve


Thanks!

I just found that out myself. Learned 'alias this' in the process.


Re: Encapsulate return value in scoped

2015-06-11 Thread Ali Çehreli via Digitalmars-d-learn

On 06/11/2015 11:43 AM, Yuxuan Shui wrote:

On Thursday, 11 June 2015 at 17:34:56 UTC, Steven Schveighoffer wrote:

On 6/11/15 1:28 PM, Yuxuan Shui wrote:

On Thursday, 11 June 2015 at 09:11:47 UTC, Daniel Kozák wrote:


On Thu, 11 Jun 2015 09:01:04 +
Yuxuan Shui via Digitalmars-d-learn digitalmars-d-learn@puremagic.com
wrote:

   A x = scoped!A(10);


use auto x = scoped!A(10);


Thanks!

Curious question, why doesn't compiler reject this code?


Because scoped!A implicitly casts to A.

-Steve


Thanks!

I just found that out myself. Learned 'alias this' in the process.


Shameless plug: :)

  http://ddili.org/ders/d.en/destroy.html#ix_destroy.scoped

This issue is explained at the end of that section.

Ali



Re: Encapsulate return value in scoped

2015-06-11 Thread Yuxuan Shui via Digitalmars-d-learn

On Thursday, 11 June 2015 at 19:23:49 UTC, Ali Çehreli wrote:

On 06/11/2015 11:43 AM, Yuxuan Shui wrote:
On Thursday, 11 June 2015 at 17:34:56 UTC, Steven 
Schveighoffer wrote:

On 6/11/15 1:28 PM, Yuxuan Shui wrote:
On Thursday, 11 June 2015 at 09:11:47 UTC, Daniel Kozák 
wrote:


On Thu, 11 Jun 2015 09:01:04 +
Yuxuan Shui via Digitalmars-d-learn 
digitalmars-d-learn@puremagic.com

wrote:

  A x = scoped!A(10);


use auto x = scoped!A(10);


Thanks!

Curious question, why doesn't compiler reject this code?


Because scoped!A implicitly casts to A.

-Steve


Thanks!

I just found that out myself. Learned 'alias this' in the 
process.


Shameless plug: :)

  http://ddili.org/ders/d.en/destroy.html#ix_destroy.scoped

This issue is explained at the end of that section.

Ali


Can you explain more about why the destructor is not called when 
returning a struct?


Can't seem to find it in the document.


Reading array of integers readln performance issues

2015-06-11 Thread kerdemdemir via Digitalmars-d-learn

Hi;

To learn D better and challanging myself I am tring code 
computation's with D.


There is a question which is about reading a line of integer 
which consist of 20 elements.


My solution fails because Time limit exceeded, I thought it is 
because of my algorithm first. I realize time limit is exceeded 
even before my algorithm starts while reading line of integers. I 
understand this by giving a wrong answer to question after readln 
statement. I did that to get a wrong answer error but my code 
still get a Time limit exceed error because readln takes very 
long time.


Can I achieve something faster than code below?

auto peopleMoney = stdin.readln().split().map!(a = 
to!int(a)).array();

if (peopleMoney.length == 20)
 writeln(:();

Regards
Erdem


Ps: I do not want to bore you with long code, but I am sending 
link to whole program anyway if anyone need.

 http://codeforces.com/contest/549/submission/11537206


.sizeof dynamically allocated array

2015-06-11 Thread Adel Mamin via Digitalmars-d-learn

import std.stdio;

void main()
{
ubyte[] a1 = new ubyte[65];
ubyte[65] a2;

writeln(a1.sizeof = , a1.sizeof); // prints 16
writeln(a2.sizeof = , a2.sizeof); // prints 65
}

Why a1.sizeof is 16?


Re: .sizeof dynamically allocated array

2015-06-11 Thread Adam D. Ruppe via Digitalmars-d-learn

On Thursday, 11 June 2015 at 20:09:38 UTC, Adel Mamin wrote:

Why a1.sizeof is 16?


sizeof is tied to *type*, not a variable. (I kinda wish a1.sizeof 
was prohibited, forcing you to say typeof(a1).sizeof so it is 
clear but whatever).


A dynamic array's size is the length variable plus the pointer 
variable.


A static array's size is the content itself.


If you want the size of the content in bytes, best way is to do 
(cast(ubyte[]) a1[]).length or somethign like that - use the 
length property instead of sizeof.


Re: .sizeof dynamically allocated array

2015-06-11 Thread Meta via Digitalmars-d-learn

On Thursday, 11 June 2015 at 20:09:38 UTC, Adel Mamin wrote:

import std.stdio;

void main()
{
ubyte[] a1 = new ubyte[65];
ubyte[65] a2;

writeln(a1.sizeof = , a1.sizeof); // prints 16
writeln(a2.sizeof = , a2.sizeof); // prints 65
}

Why a1.sizeof is 16?


ubyte[] is a slice, which is actually a struct. It's more or less 
the same as:


struct Slice(T)
{
T* ptr;
size_t length;
}

So sizeof returns the size of the struct, not the size of the 
data that its ptr member points to.


ubyte[65] is a static array, which is just a big block of data on 
the stack. That's why it returns the expected value for sizeof. 
To create a slice of a static array, use the slice operator:


writeln(sizeof(a2[])); //Prints 16


Re: Shortest way to allocate an array and initialize it with a specific value.

2015-06-11 Thread weaselcat via Digitalmars-d-learn

On Thursday, 11 June 2015 at 07:57:47 UTC, Per Nordlöw wrote:

On Wednesday, 10 June 2015 at 22:03:52 UTC, Ali Çehreli wrote:

Another option:

void main()
{
   auto a2 = new ubyte[5];


But this causes an extra zero-initialization of a2.


just an fyi, gdc optimizes this away(looks like it overwrites the 
typeinfo,) ldc does not.


Re: Reading array of integers readln performance issues

2015-06-11 Thread anonymous via Digitalmars-d-learn

On Thursday, 11 June 2015 at 19:56:00 UTC, kerdemdemir wrote:

Can I achieve something faster than code below?

auto peopleMoney = stdin.readln().split().map!(a = 
to!int(a)).array();

if (peopleMoney.length == 20)
 writeln(:();


`std.array.split` is eager. It may be faster if you use the lazy 
`std.algorithm.splitter`:


auto peopleMoney = stdin.readln().splitter().map!(to!int).array();

[...]
Ps: I do not want to bore you with long code, but I am sending 
link to whole program anyway if anyone need.

 http://codeforces.com/contest/549/submission/11537206


Seeing that you get the number of elements beforehand, you can 
preallocate the array, avoiding relocations of the data as 
elements are appended:


peopleMoney = new int[peopleCount];
copy(stdin.readln().splitter().map!(to!int), peopleMoney);

But these are tweaks. They may improve performance a little, but 
it won't be drastic.


And anyway, I find it hard to believe that the original version 
takes more than a second to parse the input. Maybe try returning 
from the function in addition to printing :(. Otherwise the 
program goes on, and the site may not show the output if the 
program as a whole took too long.


Re: Reading array of integers readln performance issues

2015-06-11 Thread Dennis Ritchie via Digitalmars-d-learn

On Thursday, 11 June 2015 at 19:56:00 UTC, kerdemdemir wrote:

Hi;

To learn D better and challanging myself I am tring code 
computation's with D.


There is a question which is about reading a line of integer 
which consist of 20 elements.


My solution fails because Time limit exceeded, I thought it 
is because of my algorithm first. I realize time limit is 
exceeded even before my algorithm starts while reading line of 
integers. I understand this by giving a wrong answer to 
question after readln statement. I did that to get a wrong 
answer error but my code still get a Time limit exceed error 
because readln takes very long time.


Can I achieve something faster than code below?

auto peopleMoney = stdin.readln().split().map!(a = 
to!int(a)).array();

if (peopleMoney.length == 20)
 writeln(:();

Regards
Erdem


Ps: I do not want to bore you with long code, but I am sending 
link to whole program anyway if anyone need.

 http://codeforces.com/contest/549/submission/11537206


Your algorithm works for about quadratic time. For N = 20 
your algorithm will work terribly long. The function `readln()` 
nothing to do with:

http://codeforces.com/contest/549/submission/11476513

A faster way of `scanf`, but it will not help you, because your 
algorithm is slow.


Re: Json

2015-06-11 Thread Dennis Ritchie via Digitalmars-d-learn

On Thursday, 11 June 2015 at 23:58:33 UTC, Cassio Butrico wrote:

What does the .json file and how to use it?


In D a file with the extension *.json is used to describe packets 
that are included in your project, the dependency manager DUB.


For example, you can install Eclipse with DDT and create a 
project with DUB. The file dub.json write the following:

-
{
name : myDupApp,
description : Carbon - Test.,
dependencies : {
carbon: ~1.4.1
}
}
-

Save the file (Ctrl + S). Then in your project with DUB 
downloaded package `carbon`:

http://code.dlang.org/packages/carbon

After that you can safely use the package `carbon`:
-
import std.stdio, carbon.linear;;

void main() {
auto m = matrix!(2, 3, (i, j) = i * 3 + j);
writeln(m); // [[0, 1, 2], [3, 4, 5]]
}


Re: Json

2015-06-11 Thread Dennis Ritchie via Digitalmars-d-learn

On Friday, 12 June 2015 at 00:35:35 UTC, Cassio Butrico wrote:
Thank you for answering me so fast , where do I get the DUB for 
windows ?


http://code.dlang.org/download


Json

2015-06-11 Thread Cassio Butrico via Digitalmars-d-learn

What does the .json file and how to use it?


Re: Json

2015-06-11 Thread Cassio Butrico via Digitalmars-d-learn
Thank you for answering me so fast , where do I get the DUB for 
windows ?


Re: Json

2015-06-11 Thread Ali Çehreli via Digitalmars-d-learn

On 06/11/2015 04:58 PM, Cassio Butrico wrote:

What does the .json file and how to use it?


There is also the .json file that is produced by dmd's -X command line 
switch:


dmd -X foo.d

That outputs the members of the source code to foo.json in a way that 
may be useful for a tool.


Ali



Re: Json

2015-06-11 Thread Cassio Butrico via Digitalmars-d-learn

On Friday, 12 June 2015 at 00:56:45 UTC, Ali Çehreli wrote:

On 06/11/2015 04:58 PM, Cassio Butrico wrote:

What does the .json file and how to use it?


There is also the .json file that is produced by dmd's -X 
command line switch:


dmd -X foo.d

That outputs the members of the source code to foo.json in a 
way that may be useful for a tool.


Ali

Thanks Ali , I did not know that. :)

I really like your tutorial is great for goods .


Re: Shortest way to allocate an array and initialize it with a specific value.

2015-06-11 Thread via Digitalmars-d-learn

On Wednesday, 10 June 2015 at 22:03:52 UTC, Ali Çehreli wrote:

Another option:

void main()
{
auto a2 = new ubyte[5];


But this causes an extra zero-initialization of a2.


a2[] = 0xAA;// -- Assign to all elements


Is

auto a2 = value.repeat(size).array;

better in this regard?


Re: Shortest way to allocate an array and initialize it with a specific value.

2015-06-11 Thread Daniel Kozák via Digitalmars-d-learn
On Wed, 10 Jun 2015 20:22:17 +
Adel Mamin via Digitalmars-d-learn digitalmars-d-learn@puremagic.com
wrote:

 ubyte[5] a = 0xAA; // Fine. Five 0xAA bytes.
 auto a2 = new ubyte[5]; // Fine. Five 0 bytes.
 Now, let's say, I want to allocate an array of a size, derived at 
 run time, and initialize it to some non-zero value at the same 
 time. What would be the shortest way of doing it?

import std.stdio;

struct Ubyte(ubyte defval) {
ubyte v = defval;
alias v this;
}

void main() {
auto a2 = new Ubyte!(0xAA)[5];
writeln(a2);
}