Re: lazy variables

2018-10-18 Thread Steven Schveighoffer via Digitalmars-d-learn

On 10/18/18 10:08 AM, aliak wrote:

On Wednesday, 17 October 2018 at 23:34:55 UTC, Paul Backus wrote:

On Wednesday, 17 October 2018 at 07:32:37 UTC, aliak wrote:

lazy S x = () {
    // do some heavy stuff
}();

if (condition) {
  func(x.y); // heavy stuff evaluated here
}


auto x = () {
    // do some heavy stuff
};

if (condition) {
    func(x().y); // heavy stuff evaluated here
}


That would do heavy stuff everytime i wanted to get y though right?


Yes, but that's what lazy variables do.

-Steve


Re: lazy variables

2018-10-18 Thread aliak via Digitalmars-d-learn

On Thursday, 18 October 2018 at 16:10:04 UTC, aliak wrote:
On Thursday, 18 October 2018 at 14:16:56 UTC, Simen Kjærås 
wrote:

On Wednesday, 17 October 2018 at 07:32:37 UTC, aliak wrote:

Hi,

Is there any notion of lazy vars in D (i see that there're 
parameters)?


What the language doesn't provide, it generally provides the 
tools to make:


struct Lazy(T) {
T delegate() _payload;
this(lazy T t) {
_payload = () => t;
}
T get() {
return _payload();
}
alias get this;
}

int fun() {
n++;
return 2;
}

int n;

unittest {
Lazy!int a = 1 + fun();
assert(n == 0); // Ensure fun hasn't been called.
auto b = a + 2;
assert(b == 5); // Ensure calculation is correct.
assert(n == 1); // Ensure fun has been called.
}

--
  Simen


yes! perfect! Thank you


With single eval:

struct Lazy(T) {
private T delegate() _payload;
private T _value;
private bool set = false;
this(lazy T t) {
_payload = () {
writeln("evaled");
return t;
};
}
@property T value() {
if (!set)
_value = _payload();
set = true;
return _value;
}

@property void value(T newValue) {
if (!set)
_value = _payload();
set = true;
_value = newValue;
}

alias value this;
}


Re: lazy variables

2018-10-18 Thread Simen Kjærås via Digitalmars-d-learn

On Wednesday, 17 October 2018 at 07:32:37 UTC, aliak wrote:

Hi,

Is there any notion of lazy vars in D (i see that there're 
parameters)?


What the language doesn't provide, it generally provides the 
tools to make:


struct Lazy(T) {
T delegate() _payload;
this(lazy T t) {
_payload = () => t;
}
T get() {
return _payload();
}
alias get this;
}

int fun() {
n++;
return 2;
}

int n;

unittest {
Lazy!int a = 1 + fun();
assert(n == 0); // Ensure fun hasn't been called.
auto b = a + 2;
assert(b == 5); // Ensure calculation is correct.
assert(n == 1); // Ensure fun has been called.
}

--
  Simen


Re: lazy variables

2018-10-18 Thread aliak via Digitalmars-d-learn

On Wednesday, 17 October 2018 at 20:32:40 UTC, Ali Çehreli wrote:

On 10/17/2018 12:32 AM, aliak wrote:

[...]


Not very clean but something like this:

import std.stdio;

struct LazyVar(alias exp) {
alias T = typeof(exp());
T value() {
static bool initialized = false;
static T val;

if (!initialized) {
val = exp();
initialized = true;
}
return val;
}

alias value this;
}

LazyVar!(() {
writeln("Doing heavy stuff");
return 42;
}) a;

void main() {
auto b = a.value;// Must specify .value or
int c = a;   // must specify type of value (int).
 // Otherwise, b and c have type 
LazyVar!(...)


// Some more usage
b = c = a;

assert(b == 42);
assert(c == 42);
}

Ali


Well I guess that's certainly a way to go about it :) Not very 
clean indeed though.


Re: lazy variables

2018-10-18 Thread aliak via Digitalmars-d-learn

On Thursday, 18 October 2018 at 14:16:56 UTC, Simen Kjærås wrote:

On Wednesday, 17 October 2018 at 07:32:37 UTC, aliak wrote:

Hi,

Is there any notion of lazy vars in D (i see that there're 
parameters)?


What the language doesn't provide, it generally provides the 
tools to make:


struct Lazy(T) {
T delegate() _payload;
this(lazy T t) {
_payload = () => t;
}
T get() {
return _payload();
}
alias get this;
}

int fun() {
n++;
return 2;
}

int n;

unittest {
Lazy!int a = 1 + fun();
assert(n == 0); // Ensure fun hasn't been called.
auto b = a + 2;
assert(b == 5); // Ensure calculation is correct.
assert(n == 1); // Ensure fun has been called.
}

--
  Simen


yes! perfect! Thank you


Re: lazy variables

2018-10-18 Thread aliak via Digitalmars-d-learn
On Thursday, 18 October 2018 at 14:11:36 UTC, Steven 
Schveighoffer wrote:


Yes, but that's what lazy variables do.

-Steve


Not in Swift at least...


Re: lazy variables

2018-10-18 Thread Paul Backus via Digitalmars-d-learn

On Thursday, 18 October 2018 at 14:08:11 UTC, aliak wrote:
On Wednesday, 17 October 2018 at 23:34:55 UTC, Paul Backus 
wrote:


auto x = () {
// do some heavy stuff
};

if (condition) {
func(x().y); // heavy stuff evaluated here
}


That would do heavy stuff everytime i wanted to get y though 
right?


Yes. If that's a problem, you could use `std.functional.memoize`, 
or Ali's solution.


Re: lazy variables

2018-10-18 Thread Steven Schveighoffer via Digitalmars-d-learn

On 10/18/18 12:11 PM, aliak wrote:

On Thursday, 18 October 2018 at 14:11:36 UTC, Steven Schveighoffer wrote:


Yes, but that's what lazy variables do.



Not in Swift at least...


Apparently so (I have not used them before), but this is D! So you 
should be aware that lazy parameters work that way (the expression is 
evaluated each time the variable is used).


In any case, you can certainly create a Swift-like lazy variable and I 
think the other responses probably show you the way.


-Steve


Re: How to store unique values of array in another array

2018-10-18 Thread Adam D. Ruppe via Digitalmars-d-learn

On Thursday, 18 October 2018 at 18:39:18 UTC, Samir wrote:
which leads me to believe that the output of `uniq` is not 
necessarily another integer array.


Right, it is actually a "range" - an object that generates the 
result on-demand, so it doesn't do work you don't actually need. 
If you don't need an array per se, you can just foreach over it 
and print them or further work on it without the cost of creating 
a new array.


But, if you need to copy it into a new array, use `.array` at the 
end.


Why is dynamic array length required here?

2018-10-18 Thread Samir via Digitalmars-d-learn
I am working my way through the exercises in the "Programming in 
D" tutorial (http://ddili.org/ders/d.en/arrays.html).  Why is the 
line assigning the length of the dynamic array required?


/*
Write a program that asks the user how many values will be 
entered and then
reads all of them. Have the program sort the elements using 
sort() and then

reverse the sorted elements using reverse().
*/

import std.stdio;
import std.algorithm;

void main() {
int noValues, i;
int[] myArray;

write("how many values would you like to enter? ");
readf(" %s", );
myArray.length = noValues; // I get a run-time error if I 
comment this out


while (i < noValues) {
write("enter value #", i+1, " ");
readf(" %s", [i]);
++i;
}

sort(myArray);
writeln(myArray);
reverse(myArray);
writeln(myArray);

}

Without the line:

myArray.length = noValues;

I get the run-time error:

$ ./exArrays1_1
how many values would you like to enter? 5
core.exception.RangeError@exArrays1_1.d(12): Range violation

??:? _d_arrayboundsp [0x461772]
??:? _Dmain [0x44c284]

I would have thought that since this is a dynamic array, I don't 
need to pre-assign its length.


Thanks




Re: Why is dynamic array length required here?

2018-10-18 Thread Stanislav Blinov via Digitalmars-d-learn

On Friday, 19 October 2018 at 02:04:37 UTC, Samir wrote:
I am working my way through the exercises in the "Programming 
in D" tutorial (http://ddili.org/ders/d.en/arrays.html).  Why 
is the line assigning the length of the dynamic array required?


[...]



Without the line:

myArray.length = noValues;

I get the run-time error...


I would have thought that since this is a dynamic array, I 
don't need to pre-assign its length.


Even though the array is dynamic, it doesn't have infinite 
storage. So you either:


- preallocate required storage, like in that example code (note 
that it's not the same as making a fixed-size ('static') array: 
in your case you only learn what the required length is at 
run-time, as opposed to static arrays where you must know it at 
compile time)


- don't preallocate, but instead append new elements like this:

myArray ~= newValue;

The latter isn't a universally "good" advice: it may cause 
reallocation every time you do that. That's why it's almost 
always best to preallocate in advance if you do know the length, 
or use something like std.array.appender that attempts to reduce 
memory reallocation. I'm not familiar with Ali's tutorials, maybe 
he talks about the appender in some future examples.




Re: Why is dynamic array length required here?

2018-10-18 Thread Ali Çehreli via Digitalmars-d-learn

On 10/18/2018 07:04 PM, Samir wrote:

>  myArray.length = noValues; // I get a run-time error if I comment
> this out

It's because the expression that reads the elements below is readf, 
which reads on top of an existing element.


>  while (i < noValues) {
>  write("enter value #", i+1, " ");
>  readf(" %s", [i]);

Aside: It may be possible to read like the following without taking the 
address, as readf has been improved to take references since that code 
was written:


 readf(" %s", myArray[i]);  // (not tested)

So, the answer to your question is, we need to set the length of the 
array so that there are elements to read on top of. If D had function 
templates like getResponse() that I use in the Templates chapter,


  http://ddili.org/ders/d.en/templates.html

then the best thing to do would be to append the elements directly to 
the array:


// No need to set .length beforehand
myArray ~= getResponse!int(/* ... */);

Ali



Re: How to store unique values of array in another array

2018-10-18 Thread Samir via Digitalmars-d-learn

On Thursday, 18 October 2018 at 18:53:06 UTC, Adam D. Ruppe wrote:
But, if you need to copy it into a new array, use `.array` at 
the end.


Thanks.  That did the trick.  But if I may, what is the 
difference between


uniqueArray = uniq(sort(unsortedArray)).array;

and

uniqueArray = unsortedArray.sort().uniq.array;

They both seem to work.  Is one preferred over the other?



Re: Error: non-shared method core.sync.condition.Condition.notify is not callable using a shared object

2018-10-18 Thread Jonathan M Davis via Digitalmars-d-learn
On Thursday, October 18, 2018 4:50:18 AM MDT Paolo Invernizzi via 
Digitalmars-d-learn wrote:
> There's a rational behind the fact that there's not a 'shared'
> version of notify/wait method in Condition?
>
> Thanks,
> Paolo

The original author of the stuff in core.sync didn't want to update it for
shared when shared was first introduced, because he didn't think that it was
well enough defined yet (e.g. there was discussion over whether it should
have write barriers or not). Some work has been done to improve shared
support in there since then but not enough. shared _does_ still need need
some rough edges sorted out (it's supposed to guarantee that you can't do
any operations on it that isn't thread-safe, but while many operations are
illegal, some which should be aren't, and some of the finer details likely
need to be cleaner up). The basics of shared have been in place and working
fine for nearly a decade, but because we've been waiting for the finer
details to be finished, and it's never been a high enough priority for that
to happen, stuff like core.sync has suffered. It doesn't help that the vast
majority of D programs don't need to do much with shared, and those that D
use higher level mechanisms such as std.concurrency or use something like
the framework that vibe.d provides.

However, as annoying as it is, with some extra casting, the pieces of
core.sync that haven't been updated properly _can_ be used with shared.
shared in general requires unsafe casting and always will. There really
isn't any way around that, but the parts of core.sync that don't use shared
properly will require that much more of it, unfortunately.

There's no question though that the situation with core.sync needs to be
fixed. Some of it has been worked on, but nowhere near enough.

- Jonathan M Davis





Path.GetDirectoryName for D?

2018-10-18 Thread Dr.No via Digitalmars-d-learn
Are there a function like C#'s Path.GetDirectoryName() 
(https://docs.microsoft.com/en-us/dotnet/api/system.io.path.getdirectoryname?redirectedfrom=MSDN=netframework-4.7.2#System_IO_Path_GetDirectoryName_System_String_) in D standard library or some dub package?
just checking if there's one, so that I don't reinvente the 
wheel...


Re: Path.GetDirectoryName for D?

2018-10-18 Thread Adam D. Ruppe via Digitalmars-d-learn

On Thursday, 18 October 2018 at 19:54:59 UTC, Dr.No wrote:

Are there a function like C#'s Path.GetDirectoryName()


Looks the same as "dirName" from "import std.path"

http://dpldocs.info/experimental-docs/std.path.dirName.1.html


How to store unique values of array in another array

2018-10-18 Thread Samir via Digitalmars-d-learn
What is the proper way to find the unique values of an array and 
store them in another array?  When I try:


import std.stdio: writeln;
import std.conv;
import std.algorithm;

void main() {
int[] unsortedArray = [5, 3, 8, 5, 2, 3, 0, 8];
int[] uniqueArray;

uniqueArray = uniq(sort(unsortedArray));
}

I get the compilation error:
sortSetUnique2.d(9): Error: cannot implicitly convert expression 
uniq(sort(unsortedArray)) of type UniqResult!(binaryFun, 
SortedRange!(int[], "a < b")) to int[]


which leads me to believe that the output of `uniq` is not 
necessarily another integer array.


Thanks


Re: How to store unique values of array in another array

2018-10-18 Thread Samir via Digitalmars-d-learn

On Thursday, 18 October 2018 at 19:25:26 UTC, Adam D. Ruppe wrote:

Which is better simply depends on which one reads better to you.


Thanks again, Adam.


Re: How to store unique values of array in another array

2018-10-18 Thread Adam D. Ruppe via Digitalmars-d-learn

On Thursday, 18 October 2018 at 19:19:53 UTC, Samir wrote:

They both seem to work.  Is one preferred over the other?


No difference. The compile will just transform o.f into f(o) if 
it can. They both do the same thing and it is just a different 
way of writing it.


Which is better simply depends on which one reads better to you.


Re: Why is dynamic array length required here?

2018-10-18 Thread Mike Parker via Digitalmars-d-learn

On Friday, 19 October 2018 at 02:04:37 UTC, Samir wrote:


I would have thought that since this is a dynamic array, I 
don't need to pre-assign its length.


Thanks



Just to expand on the previous answers, a dynamic array 
declaration with no initializer is an empty array:


int[] arr;
assert(arr.length == 0);

No memory has been allocated to store any elements. Appending 
elements will allocate memory. In fact, the runtime will allocate 
more than needed so that it doesn't have to allocate on each 
append.


arr ~= 1;
assert(arr.length == 1);
writeln(arr.capacity);

When the capacity is 0, the next append will trigger another 
allocation. The reserve function can be used to allocate enough 
memory for a number of elements, but the array will still be 
empty:


int[] arr;
arr.reserve(100);
assert(arr.length == 0);
writeln(arr.capacity);

Now 100+ elements can be appended without triggering an 
allocation.


Setting the length directly as Ali's example does means that not 
only is the required memory allocated, but also that the array is 
not empty.


int[] arr;
arr.length = 100;
assert(arr.length == 100);
writeln(arr.capacity);

And now the array can elements can be written to directly via the 
index operator, and a pointer to each element can be referenced 
as it is in the call to readf.


Note that it's still possible to write elements directly to a 
dynamic array without the append operator after memory has been 
reserved, but it completely bypasses the runtime.


void main() {
int[] myArray;

myArray.reserve(10);
foreach(i; 0 .. 10) {
*(myArray.ptr + i) = i;
}

assert(myArray == []);
assert(myArray.length == 0);

foreach(i; 0 .. 10) {
writeln(*(myArray.ptr + i));
}
}

This is essentially treating myArray.ptr as a C array, with no 
bounds checking and no automatic memory management. Here be 
dragons.


Re: Documents about ddoc? and markdown in ddoc?

2018-10-18 Thread rikki cattermole via Digitalmars-d-learn

On 18/10/2018 6:38 PM, dangbinghoo wrote:

hi,

Is there any other documents related about ddoc usage? the only thing I 
can find is:


  https://dlang.org/spec/ddoc.html#using_ddoc_to_generate_examples

But I found it never mentioned something like $(LI a  list item), is 
there a full ddoc document available?


Default for html output: 
https://github.com/dlang/dmd/blob/master/res/default_ddoc_theme.ddoc



And, is there any info about using markdown in ddoc?


Not possible.



Re: Documents about ddoc? and markdown in ddoc?

2018-10-18 Thread dangbinghoo via Digitalmars-d-learn
On Thursday, 18 October 2018 at 05:59:36 UTC, rikki cattermole 
wrote:

On 18/10/2018 6:38 PM, dangbinghoo wrote:

hi,

Is there any other documents related about ddoc usage? the 
only thing I can find is:


 
 https://dlang.org/spec/ddoc.html#using_ddoc_to_generate_examples


But I found it never mentioned something like $(LI a  list 
item), is there a full ddoc document available?


Default for html output: 
https://github.com/dlang/dmd/blob/master/res/default_ddoc_theme.ddoc



And, is there any info about using markdown in ddoc?


Not possible.


thanks for your reply.

and sorry, I'm too careless for reading the documents
https://dlang.org/spec/ddoc.html#using_ddoc_to_generate_examples
the $(LI x y) is a macro which was mentioned.




Re: Who can stop it ? Help me,thank you.

2018-10-18 Thread Kagamin via Digitalmars-d-learn

On Wednesday, 17 October 2018 at 13:48:04 UTC, FrankLike wrote:

Hi,teacher:

 I like D lang,when I download the soft from  
http://www.360totalsecurity.com/en/


Contact their support: supp...@360safe.com


Error: non-shared method core.sync.condition.Condition.notify is not callable using a shared object

2018-10-18 Thread Paolo Invernizzi via Digitalmars-d-learn
There's a rational behind the fact that there's not a 'shared' 
version of notify/wait method in Condition?


Thanks,
Paolo


Re: Error: non-shared method core.sync.condition.Condition.notify is not callable using a shared object

2018-10-18 Thread Stanislav Blinov via Digitalmars-d-learn
On Thursday, 18 October 2018 at 10:50:18 UTC, Paolo Invernizzi 
wrote:


There's a rational behind the fact that there's not a 'shared' 
version of notify/wait method in Condition?


They were implemented before complete `shared` spec existed, 
which is any time between it's conception and now.


Re: Error: non-shared method core.sync.condition.Condition.notify is not callable using a shared object

2018-10-18 Thread Kagamin via Digitalmars-d-learn
On Thursday, 18 October 2018 at 10:50:18 UTC, Paolo Invernizzi 
wrote:
There's a rational behind the fact that there's not a 'shared' 
version of notify/wait method in Condition?


core.sync is pretty old, it was written in 2009 before default 
storage class for global variables became TLS.


Re: lazy variables

2018-10-18 Thread aliak via Digitalmars-d-learn

On Wednesday, 17 October 2018 at 23:34:55 UTC, Paul Backus wrote:

On Wednesday, 17 October 2018 at 07:32:37 UTC, aliak wrote:

lazy S x = () {
// do some heavy stuff
}();

if (condition) {
  func(x.y); // heavy stuff evaluated here
}


auto x = () {
// do some heavy stuff
};

if (condition) {
func(x().y); // heavy stuff evaluated here
}


That would do heavy stuff everytime i wanted to get y though 
right?






Re: lazy variables

2018-10-18 Thread Steven Schveighoffer via Digitalmars-d-learn

On 10/17/18 3:32 AM, aliak wrote:

Hi,

Is there any notion of lazy vars in D (i see that there're parameters)?

i.e:

struct S {
   //...
   int y;
   //...
}


/*

lazy S x = () {
     // do some heavy stuff
}();

*/

auto x() { // do some heavy stuff
}



if (condition) {
   func(x.y); // heavy stuff evaluated here
}



-Steve