Re: How to detect overflow

2015-11-03 Thread Namal via Digitalmars-d-learn

On Wednesday, 4 November 2015 at 04:22:03 UTC, BBasile wrote:

On Wednesday, 4 November 2015 at 03:55:13 UTC, Namal wrote:

Is there a way to detect overflow for example for:

int i = 2_000_000_000;

int a = i*i*i;

writeln(a);

-> 1073741824


You can use core.checkedint [1]

---

http://dlang.org/phobos/core_checkedint.html


It says:


"The overflow is sticky, meaning a sequence of operations can be 
done and overflow need only be checked at the end."


But how can I make multiple operations? I can only put 2 values 
in the function.





Re: How to detect overflow

2015-11-03 Thread BBasile via Digitalmars-d-learn

On Wednesday, 4 November 2015 at 07:19:09 UTC, Ali Çehreli wrote:

On 11/03/2015 10:34 PM, Namal wrote:


http://dlang.org/phobos/core_checkedint.html


Is it just an error in the documentation that the return value 
is stated

as sum for the multiplication functions?


Yeah, looks like classic copy-paste errors. :)

Ali


I take the token for this ddoc fix:

https://github.com/D-Programming-Language/druntime/pull/1429


Re: How to detect overflow

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

On 11/03/2015 10:34 PM, Namal wrote:


http://dlang.org/phobos/core_checkedint.html


Is it just an error in the documentation that the return value is stated
as sum for the multiplication functions?


Yeah, looks like classic copy-paste errors. :)

Ali



Re: How to detect overflow

2015-11-03 Thread Namal via Digitalmars-d-learn

On Wednesday, 4 November 2015 at 04:22:03 UTC, BBasile wrote:

On Wednesday, 4 November 2015 at 03:55:13 UTC, Namal wrote:

Is there a way to detect overflow for example for:

int i = 2_000_000_000;

int a = i*i*i;

writeln(a);

-> 1073741824


You can use core.checkedint [1]

---

http://dlang.org/phobos/core_checkedint.html


Is it just an error in the documentation that the return value is 
stated as sum for the multiplication functions?


Re: Is it possible to filter variadics?

2015-11-03 Thread Jakob Ovrum via Digitalmars-d-learn

On Tuesday, 3 November 2015 at 23:41:10 UTC, maik klein wrote:

Is it possible to filter variadics for example if I would call

void printSumIntFloats(Ts...)(Ts ts){...}

printSumIntFloats(1,1.0f,2,2.0f);

I want to print the sum of all integers and the sum of all 
floats.



//Pseudo code
void printSumIntFloats(Ts...)(Ts ts){
auto sumOfInts = ts
  .filter!(isInteger)
  .reduce(a => a + b);
writeln(sumOfInts);
...
}

Is something like this possible?


import std.algorithm.iteration : sum;
import std.meta : allSatisfy, Filter;
import std.traits;
import std.typecons : tuple;
import std.range : only;

// These two are necessary since the ones in std.traits
// don't accept non-types
enum isIntegral(alias i) = std.traits.isIntegral!(typeof(i));
enum isFloatingPoint(alias f) = 
std.traits.isFloatingPoint!(typeof(f));


auto separateSum(T...)(T args)
if(allSatisfy!(isNumeric, T))
{
	return tuple(only(Filter!(isIntegral, args)).sum(), 
only(Filter!(isFloatingPoint, args)).sum());

}

pure nothrow @safe unittest
{
assert(separateSum(2, 2.0) == tuple(2, 2.0));
assert(separateSum(3, 2.0, 5, 1.0, 1.0) == tuple(8, 4.0));
}



Re: How to detect overflow

2015-11-03 Thread BBasile via Digitalmars-d-learn

On Wednesday, 4 November 2015 at 03:55:13 UTC, Namal wrote:

Is there a way to detect overflow for example for:

int i = 2_000_000_000;

int a = i*i*i;

writeln(a);

-> 1073741824


You can use core.checkedint [1]

---

http://dlang.org/phobos/core_checkedint.html


How to detect overflow

2015-11-03 Thread Namal via Digitalmars-d-learn

Is there a way to detect overflow for example for:

int i = 2_000_000_000;

int a = i*i*i;

writeln(a);

-> 1073741824


Re: Help with Concurrency

2015-11-03 Thread bertg via Digitalmars-d-learn
On Wednesday, 4 November 2015 at 01:27:57 UTC, Nicholas Wilson 
wrote:

On Tuesday, 3 November 2015 at 23:16:59 UTC, bertg wrote:

[...]
Try replacing the following loop to have a receive that times 
out or while(true) to while(web socked.connected)

[...]


That didn't solve the problem. How would that solve the problem?

std.concurrency.receive does not have a timeout either.


Re: Help with Concurrency

2015-11-03 Thread Nicholas Wilson via Digitalmars-d-learn

On Tuesday, 3 November 2015 at 23:16:59 UTC, bertg wrote:

I am having trouble with a simple use of concurrency.

Running the following code I get 3 different tid's, multiple 
"sock in" messages printed, but no receives. I am supposed to 
get a "received!" for each "sock in", but I am getting hung up 
on "receiving...".


Am I misusing or misunderstanding the use of mailboxes?

===

class Connection {
Reactor reactor;
WebSocket webSocket;

this(Reactor r, WebSocket ws)
{
reactor = r;
webSocket = ws;

messageLoop();
}

void messageLoop()
{
std.concurrency.Tid tid = std.concurrency.thisTid();
writeln("tid 1 ~ " ~ 
to!string(std.concurrency.thisTid()));
writeln("tid 2 ~ " ~ 
to!string(std.concurrency.thisTid()));
writeln("tid 3 ~ " ~ 
to!string(std.concurrency.thisTid()));


// deal with websocket messages
spawn(&handleConnectionWebSocket, tid, cast(shared) 
webSocket);

// deal with pub/sub
//spawn();

Try replacing the following loop to have a receive that times out 
or while(true) to while(web socked.connected)

while (true) {
writeln("receiving...");
std.concurrency.receive(
(string msg) {
writeln("conn: received ws message: " ~ 
msg);

}
);
writeln("received!");
}
}
}
void handleConnectionWebSocket(std.concurrency.Tid caller, 
shared WebSocket ws)

{
auto sock = cast(WebSocket) ws;
while (sock.connected) {
writeln("sock in");
auto msgIn = sock.receiveText();
std.concurrency.send(caller, msgIn);
}
}




Re: Align a variable on the stack.

2015-11-03 Thread Nicholas Wilson via Digitalmars-d-learn
On Tuesday, 3 November 2015 at 23:29:45 UTC, TheFlyingFiddle 
wrote:

Is there a built in way to do this in dmd?

Basically I want to do this:

auto decode(T)(...)
{
   while(...)
   {
  T t = T.init; //I want this aligned to 64 bytes.
   }
}


Currently I am using:

align(64) struct Aligner(T)
{
   T value;
}

auto decode(T)(...)
{
   Aligner!T t = void;
   while(...)
   {
  t.value = T.init;
   }
}

But is there a less hacky way? From the documentation of align 
it seems i cannot use that for this kind of stuff. Also I don't 
want to have to use align(64) on my T struct type since for my 
usecase I am decoding arrays of T.


The reason that I want to do this in the first place is that if 
the variable is aligned i get about a 2.5x speedup (i don't 
really know why... found it by accident)


Note that there are two different alignments:
 to control padding between instances on the stack 
(arrays)

 to control padding between members of a struct

align(64) //arrays
struct foo
{
  align(16) short baz; //between members
  align (1) float quux;
}

your 2.5x speedup is due to aligned vs. unaligned loads and 
stores which for SIMD type stuff has a really big effect. 
Basically misaligned stuff is really slow. IIRC there was a 
(blog/paper?) of someone on a uC spending a vast amount of time 
in ONE misaligned integer assignment causing traps and getting 
the kernel involved. Not quite as bad on x86 but still with doing.


As to a less jacky solution I'm not sure there is one.




Re: foreach loop

2015-11-03 Thread TheFlyingFiddle via Digitalmars-d-learn

On Tuesday, 3 November 2015 at 15:29:31 UTC, Namal wrote:
well I tried this that way, but my count stays 0, same as if I 
do it in an int function with a return though I clearly have 
some false elements in the arr.


You could also use count: 
http://dlang.org/phobos/std_algorithm_searching.html#count


return arr.count!(x => !x);



Re: Is it possible to filter variadics?

2015-11-03 Thread TheFlyingFiddle via Digitalmars-d-learn

On Tuesday, 3 November 2015 at 23:41:10 UTC, maik klein wrote:

Is it possible to filter variadics for example if I would call

void printSumIntFloats(Ts...)(Ts ts){...}

printSumIntFloats(1,1.0f,2,2.0f);

I want to print the sum of all integers and the sum of all 
floats.



//Pseudo code
void printSumIntFloats(Ts...)(Ts ts){
auto sumOfInts = ts
  .filter!(isInteger)
  .reduce(a => a + b);
writeln(sumOfInts);
...
}

Is something like this possible?


It is possible: I don't think that reduce works on tuples but you 
could do something like this.


import std.traits, std.meta;
void printsumIntFloats(Ts...)(Ts ts)
{
   alias integers = Filter!(isInteger, Ts);
   alias floats   = Filter!(isFloatingPoint, Ts);
   alias int_t= CommonType!(integers);
   alias float_t  = CommonType!(floats);

   int_t intres = 0;
   float_t floatres = 0;
   foreach(i, arg; ts)
   {
  static if(isInteger!(Ts[i]))
  intres += arg;
  else
  floatres += arg;
   }
   writeln(intres);
   writeln(floatres);
}










Is it possible to filter variadics?

2015-11-03 Thread maik klein via Digitalmars-d-learn

Is it possible to filter variadics for example if I would call

void printSumIntFloats(Ts...)(Ts ts){...}

printSumIntFloats(1,1.0f,2,2.0f);

I want to print the sum of all integers and the sum of all floats.


//Pseudo code
void printSumIntFloats(Ts...)(Ts ts){
auto sumOfInts = ts
  .filter!(isInteger)
  .reduce(a => a + b);
writeln(sumOfInts);
...
}

Is something like this possible?



Re: good reasons not to use D?

2015-11-03 Thread Chris via Digitalmars-d-learn

On Friday, 30 October 2015 at 10:35:03 UTC, Laeeth Isharc wrote:

Interesting. Two points suggest that you should use D only for 
serious programming:


"cases where you want to write quick one-off scripts that need to 
use a bunch of different libraries not yet available in D and 
where it doesn't make sense to wrap or port them" - quick and 
dirty


"where you have many inexperienced programmers and they need to 
be productive very quickly." - quick and awkward


[what does this tell us about financial programming ...]

This reason is true of any other language:

"where you have a lot of code in another language (especially non 
C, non Python) and defining an interface is not so easy;"


In fact, the reasons you give (apart from the point about GUI) 
are true of C++, C#, Java etc. too. It's a bit generic. I was 
thinking of D specific reasons like lack of support for mobile 
platforms (not 100% yet). So your average stock broker couldn't 
calculate some bogus numbers on his iPad while having a latte and 
a Pelegrino in a posh cafe off Wallstreet. He he he.


GC and lack of mobile are real reasons not to use D.


Align a variable on the stack.

2015-11-03 Thread TheFlyingFiddle via Digitalmars-d-learn

Is there a built in way to do this in dmd?

Basically I want to do this:

auto decode(T)(...)
{
   while(...)
   {
  T t = T.init; //I want this aligned to 64 bytes.
   }
}


Currently I am using:

align(64) struct Aligner(T)
{
   T value;
}

auto decode(T)(...)
{
   Aligner!T t = void;
   while(...)
   {
  t.value = T.init;
   }
}

But is there a less hacky way? From the documentation of align it 
seems i cannot use that for this kind of stuff. Also I don't want 
to have to use align(64) on my T struct type since for my usecase 
I am decoding arrays of T.


The reason that I want to do this in the first place is that if 
the variable is aligned i get about a 2.5x speedup (i don't 
really know why... found it by accident)









Help with Concurrency

2015-11-03 Thread bertg via Digitalmars-d-learn

I am having trouble with a simple use of concurrency.

Running the following code I get 3 different tid's, multiple 
"sock in" messages printed, but no receives. I am supposed to get 
a "received!" for each "sock in", but I am getting hung up on 
"receiving...".


Am I misusing or misunderstanding the use of mailboxes?

===

class Connection {
Reactor reactor;
WebSocket webSocket;

this(Reactor r, WebSocket ws)
{
reactor = r;
webSocket = ws;

messageLoop();
}

void messageLoop()
{
std.concurrency.Tid tid = std.concurrency.thisTid();
writeln("tid 1 ~ " ~ 
to!string(std.concurrency.thisTid()));
writeln("tid 2 ~ " ~ 
to!string(std.concurrency.thisTid()));
writeln("tid 3 ~ " ~ 
to!string(std.concurrency.thisTid()));


// deal with websocket messages
spawn(&handleConnectionWebSocket, tid, cast(shared) 
webSocket);

// deal with pub/sub
//spawn();

while (true) {
writeln("receiving...");
std.concurrency.receive(
(string msg) {
writeln("conn: received ws message: " ~ msg);
}
);
writeln("received!");
}
}
}
void handleConnectionWebSocket(std.concurrency.Tid caller, shared 
WebSocket ws)

{
auto sock = cast(WebSocket) ws;
while (sock.connected) {
writeln("sock in");
auto msgIn = sock.receiveText();
std.concurrency.send(caller, msgIn);
}
}


Re: proper range usage

2015-11-03 Thread Alex via Digitalmars-d-learn

On Tuesday, 3 November 2015 at 22:36:21 UTC, Ali Çehreli wrote:

That's fine. D's slices do that all the time: arr[0..3] and 
arr[3..$] seem to share index 3 but it is not the case: The 
first slice does not use it but the second one does.


Ok... great! This is what I worried about...

Aside: If 'begin' and 'end' are indexes into an actual array 
(of Ms? I forgot), perhaps you can keep slices instead:


struct P
{
 int id;
 alias id this;
 M[] ms;// <--
}

It would be almost as efficient but larger: Altough you use two 
ints (total 8 bytes), a slice is 16 bytes on a 64-bit system: a 
pointer and a size_t.



Thats cool! Thank you very much!




Re: proper range usage

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

On 11/03/2015 01:12 AM, Alex wrote:

>> That problem is solved by the convention that 'end' is one beyond the
>> last valid element. So, when there is only the element 42, then
>> begin==42 and end==43. Only when the last element (42 in this case) is
>> consumed, begin==end.
>>
> This part is dangerous, and I'm not sure how dangerous it is.

If I understand correctly, you are referring to 'end' being one beyond. 
It is not dangerous because nobody dereferences that index. The slice is 
simply empty when beg==end.


This is how C++'s iterators work and how D's number ranges work:

foreach (i; 0 .. 3) {
arr[i];// i will not be 3
}

> Now, I
> have to dive into my structure a little bit deeper:
> Say, I have three classes:
>
>
> class B //current structure
> {
>  M[] _ms;
>  P[] _ps;
>  P[M] assoc;
> }
>
> struct M
> {
>  int id;
>  alias id this;
> }
>
> struct P
> {
>  int id;
>  alias id this;
>  int begin;
>  int end;
> }

> The idea is, that P structs are disjunct (and contigous, if this does
> matter) arrays of M's. And the question is, what happens, if I set the
> end property of a P to a number, which belongs to another P.

That's fine. D's slices do that all the time: arr[0..3] and arr[3..$] 
seem to share index 3 but it is not the case: The first slice does not 
use it but the second one does.


Aside: If 'begin' and 'end' are indexes into an actual array (of Ms? I 
forgot), perhaps you can keep slices instead:


struct P
{
 int id;
 alias id this;
 M[] ms;// <--
}

It would be almost as efficient but larger: Altough you use two ints 
(total 8 bytes), a slice is 16 bytes on a 64-bit system: a pointer and a 
size_t.


Ali



Re: good reasons not to use D?

2015-11-03 Thread DLearner via Digitalmars-d-learn

On Saturday, 31 October 2015 at 14:37:23 UTC, rumbu wrote:

On Friday, 30 October 2015 at 10:35:03 UTC, Laeeth Isharc wrote:

I'm writing a talk for codemesh on the use of D in finance.

Any other thoughts?


For finance stuff - missing a floating point decimal data type. 
Things like 1.1 + 2.2 = 3.3003


Agreed.

Suggest 'dec' type.

Example:
dec foo{15,3};

Means simple variable 'foo' can hold fifteen digits, last three 
decimal places.


DLearner



Re: good reasons not to use D?

2015-11-03 Thread steven kladitis via Digitalmars-d-learn

On Monday, 2 November 2015 at 17:09:41 UTC, Laeeth Isharc wrote:
On Saturday, 31 October 2015 at 16:06:47 UTC, Russel Winder 
wrote:
On Sat, 2015-10-31 at 15:41 +, tcak via 
Digitalmars-d-learn wrote:

[...]


In that std.bigint.BigInt provides the accuracy, yes it does 
suffice. But it is slow. As far as I am aware only IBM Big 
Iron (aka mainframes, aka z-Series) has hardware decimal 
floating point these days. (Even though 1970s and 1980s 
microprocessors had the feature.)


It would be nice to have fixed point numbers in Phobos, 
although it's not much work to implement, and there is a 
library solution already (which is maintained, but sometimes 
for a while breaks with newer versions of dmd).


I am Big Proponent of decimal arithmetic. It should be a breeze 
for a some of the smart folks using D. :):)


Re: Efficiency of immutable vs mutable

2015-11-03 Thread Jonathan M Davis via Digitalmars-d-learn
On Tuesday, November 03, 2015 18:44:06 Andrew via Digitalmars-d-learn wrote:
> This:
>
> On Tuesday, 3 November 2015 at 04:08:09 UTC, TheFlyingFiddle
> wrote:
> > __gshared char[4] lookup = ['a', 't', 'g', 'c];
>
> Has the same efficiency gain as immutable, so it looks like a
> thread-local vs global difference and the extra cost is going
> through the thread-local lookup.

Just FYI. Be _very_ careful with __gshared, since it essentially breaks the
type system. It's really only intended to be used with extern(C)
declarations so that you can access variables from C libraries. The compiler
still treats a __gshared variables as thread-local, because __gshared does
not affect the type. If you want a variable to not be in TLS, you really
should be using shared rather than __gshared. D's type system is designed so
that variables not marked with shared or immutable are thread-local, and
trying to get around that is just asking for trouble (e.g. the compiler can
and will make assumptions based on the fact that a non-shared variable is in
TLS per the type system).

- Jonathan M Davis



Re: Efficiency of immutable vs mutable

2015-11-03 Thread Andrew via Digitalmars-d-learn

This:

On Tuesday, 3 November 2015 at 04:08:09 UTC, TheFlyingFiddle 
wrote:

__gshared char[4] lookup = ['a', 't', 'g', 'c];


Has the same efficiency gain as immutable, so it looks like a 
thread-local vs global difference and the extra cost is going 
through the thread-local lookup.


Thanks


Re: foreach loop

2015-11-03 Thread Edwin van Leeuwen via Digitalmars-d-learn

On Tuesday, 3 November 2015 at 16:55:44 UTC, wobbles wrote:
On Tuesday, 3 November 2015 at 15:42:16 UTC, Edwin van Leeuwen 
wrote:

On Tuesday, 3 November 2015 at 15:29:31 UTC, Namal wrote:

writefln("Count is: %s", arr
  .filter!(a => a==true)
  .sum);

// Note: std.algorithm.sum is the same as
// std.algorithm.reduce!((a,b)=a+b);




Shouldn't you be using walkLength instead of sum, since you 
are counting the left over values?


import std.range : walkLength;
writefln("Count is: %s", arr
  .filter!(a => a==false)
  .walkLength);


That would work also yes.
Be interesting to know which is more efficient actually - I 
suspect they're very similar.


false converts to zero, so
[false,false,false].sum == 0

Of course true converts to one ->
[true,true,true].sum == 3


Re: foreach loop

2015-11-03 Thread wobbles via Digitalmars-d-learn
On Tuesday, 3 November 2015 at 15:42:16 UTC, Edwin van Leeuwen 
wrote:

On Tuesday, 3 November 2015 at 15:29:31 UTC, Namal wrote:

writefln("Count is: %s", arr
  .filter!(a => a==true)
  .sum);

// Note: std.algorithm.sum is the same as
// std.algorithm.reduce!((a,b)=a+b);




Shouldn't you be using walkLength instead of sum, since you are 
counting the left over values?


import std.range : walkLength;
writefln("Count is: %s", arr
  .filter!(a => a==false)
  .walkLength);


That would work also yes.
Be interesting to know which is more efficient actually - I 
suspect they're very similar.


Re: foreach loop

2015-11-03 Thread Edwin van Leeuwen via Digitalmars-d-learn

On Tuesday, 3 November 2015 at 15:29:31 UTC, Namal wrote:

writefln("Count is: %s", arr
  .filter!(a => a==true)
  .sum);

// Note: std.algorithm.sum is the same as
// std.algorithm.reduce!((a,b)=a+b);




Shouldn't you be using walkLength instead of sum, since you are 
counting the left over values?


import std.range : walkLength;
writefln("Count is: %s", arr
  .filter!(a => a==false)
  .walkLength);



Re: foreach loop

2015-11-03 Thread Namal via Digitalmars-d-learn

On Tuesday, 3 November 2015 at 15:10:43 UTC, wobbles wrote:

On Tuesday, 3 November 2015 at 15:06:00 UTC, Namal wrote:
On Tuesday, 3 November 2015 at 14:52:19 UTC, Adam D. Ruppe 
wrote:

On Tuesday, 3 November 2015 at 14:47:14 UTC, Namal wrote:
I remember it is possible to get the index for each element 
in the foreach loop, but I forgot how to do it. Can you help 
me out please. Thx.


for many of them it is as simple as:

foreach(index, element; array) { }


Thank you. I am still struggling with the functional ways of 
D. Now how could I write this foreach loop the functional way?


bool[] arr = [ture, false, ture, ...];

int count;
foreach(i;arr){

  if(!i)
count++;
}
writeln(count);


writefln("Count is: %s", arr
  .filter!(a => a==true)
  .sum);

// Note: std.algorithm.sum is the same as
// std.algorithm.reduce!((a,b)=a+b);


well I tried this that way, but my count stays 0, same as if I do 
it in an int function with a return though I clearly have some 
false elements in the arr.


Re: foreach loop

2015-11-03 Thread Namal via Digitalmars-d-learn

On Tuesday, 3 November 2015 at 15:10:43 UTC, wobbles wrote:

On Tuesday, 3 November 2015 at 15:06:00 UTC, Namal wrote:
On Tuesday, 3 November 2015 at 14:52:19 UTC, Adam D. Ruppe 
wrote:

On Tuesday, 3 November 2015 at 14:47:14 UTC, Namal wrote:
I remember it is possible to get the index for each element 
in the foreach loop, but I forgot how to do it. Can you help 
me out please. Thx.


for many of them it is as simple as:

foreach(index, element; array) { }


Thank you. I am still struggling with the functional ways of 
D. Now how could I write this foreach loop the functional way?


bool[] arr = [ture, false, ture, ...];

int count;
foreach(i;arr){

  if(!i)
count++;
}
writeln(count);


writefln("Count is: %s", arr
  .filter!(a => a==true)
  .sum);

// Note: std.algorithm.sum is the same as
// std.algorithm.reduce!((a,b)=a+b);


How do I save sum as integer or something I try;

arr.writeln;
return arr.filter!(a=>a==false).sum;

but I get
[true, false, true, false, true, true, true, false, true, false]
0




Re: foreach loop

2015-11-03 Thread wobbles via Digitalmars-d-learn

On Tuesday, 3 November 2015 at 15:06:00 UTC, Namal wrote:
On Tuesday, 3 November 2015 at 14:52:19 UTC, Adam D. Ruppe 
wrote:

On Tuesday, 3 November 2015 at 14:47:14 UTC, Namal wrote:
I remember it is possible to get the index for each element 
in the foreach loop, but I forgot how to do it. Can you help 
me out please. Thx.


for many of them it is as simple as:

foreach(index, element; array) { }


Thank you. I am still struggling with the functional ways of D. 
Now how could I write this foreach loop the functional way?


bool[] arr = [ture, false, ture, ...];

int count;
foreach(i;arr){

  if(!i)
count++;
}
writeln(count);


writefln("Count is: %s", arr
  .filter!(a => a==true)
  .sum);

// Note: std.algorithm.sum is the same as
// std.algorithm.reduce!((a,b)=a+b);


Re: foreach loop

2015-11-03 Thread wobbles via Digitalmars-d-learn

On Tuesday, 3 November 2015 at 15:10:43 UTC, wobbles wrote:

On Tuesday, 3 November 2015 at 15:06:00 UTC, Namal wrote:
On Tuesday, 3 November 2015 at 14:52:19 UTC, Adam D. Ruppe 
wrote:

On Tuesday, 3 November 2015 at 14:47:14 UTC, Namal wrote:

[...]


for many of them it is as simple as:

foreach(index, element; array) { }


Thank you. I am still struggling with the functional ways of 
D. Now how could I write this foreach loop the functional way?


bool[] arr = [ture, false, ture, ...];

int count;
foreach(i;arr){

  if(!i)
count++;
}
writeln(count);


writefln("Count is: %s", arr
  .filter!(a => a==true)
  .sum);

// Note: std.algorithm.sum is the same as
// std.algorithm.reduce!((a,b)=a+b);


Oh, I realise now you were counting the number of 'false' values.
I counted the true values - so the filter line is wrong here.


Re: foreach loop

2015-11-03 Thread Namal via Digitalmars-d-learn

On Tuesday, 3 November 2015 at 14:52:19 UTC, Adam D. Ruppe wrote:

On Tuesday, 3 November 2015 at 14:47:14 UTC, Namal wrote:
I remember it is possible to get the index for each element in 
the foreach loop, but I forgot how to do it. Can you help me 
out please. Thx.


for many of them it is as simple as:

foreach(index, element; array) { }


Thank you. I am still struggling with the functional ways of D. 
Now how could I write this foreach loop the functional way?


bool[] arr = [ture, false, ture, ...];

int count;
foreach(i;arr){

  if(!i)
count++;
}
writeln(count);




Re: foreach loop

2015-11-03 Thread wobbles via Digitalmars-d-learn

On Tuesday, 3 November 2015 at 14:47:14 UTC, Namal wrote:

Hello guys,

I remember it is possible to get the index for each element in 
the foreach loop, but I forgot how to do it. Can you help me 
out please. Thx.


auto arr = ["Hello", "World"];

foreach(int idx, string str; arr){
   writefln("%s = %s", idx, str);
}


Re: foreach loop

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

On Tuesday, 3 November 2015 at 14:47:14 UTC, Namal wrote:
I remember it is possible to get the index for each element in 
the foreach loop, but I forgot how to do it. Can you help me 
out please. Thx.


for many of them it is as simple as:

foreach(index, element; array) { }


Re: foreach loop

2015-11-03 Thread Namal via Digitalmars-d-learn

Hello guys,

I remember it is possible to get the index for each element in 
the foreach loop, but I forgot how to do it. Can you help me out 
please. Thx.


Re: proper range usage

2015-11-03 Thread Alex via Digitalmars-d-learn
... and yes, each P's M's are meant to be the same, as the 
associated M's in the B's class to the P. If you understand, what 
I mean ;)


Re: proper range usage

2015-11-03 Thread Alex via Digitalmars-d-learn

On Tuesday, 3 November 2015 at 08:23:20 UTC, Ali Çehreli wrote:

> "Programming in D" book (the revision of 2015-10-24)

Oooh! That smells very fresh. :)


:)


> In my case, the container class can't become empty. Even if
it contains
> one single element, in this case the example should return
true for
> begin == end, it is not empty.

That problem is solved by the convention that 'end' is one 
beyond the last valid element. So, when there is only the 
element 42, then begin==42 and end==43. Only when the last 
element (42 in this case) is consumed, begin==end.


This part is dangerous, and I'm not sure how dangerous it is. 
Now, I have to dive into my structure a little bit deeper:

Say, I have three classes:


class B //current structure
{
M[] _ms;
P[] _ps;
P[M] assoc;
}

struct M
{
int id;
alias id this;
}

struct P
{
int id;
alias id this;
int begin;
int end;
}

The idea is, that P structs are disjunct (and contigous, if this 
does matter) arrays of M's. And the question is, what happens, if 
I set the end property of a P to a number, which belongs to 
another P.
At the current time class B contains arrays of different M's 
(constant, big one, say order of 10^6 elements, elements itself 
are not very large, say about 5 members and a bunch of properties 
calculated at runtime) as well as an array of P's (at the 
beginning not so big one, but growing fast) and the array of 
associations between the M's and P's. In my program I implement 
this array as int[int], and reassign the associated values at the 
same time as the array of P's is growing.


Here I have to deliver the "buzz words", so I'm trying to 
implement a set partition algorithm with disjoint sets. With the 
standard question how to achieve the fastest cutting of the whole 
array into its single components. The application in my case are 
just some constraints which say, how the cutting is allowed.



Such ranges are called generators.

ok, cool! Thx.


Re: Capturing __FILE__ and __LINE in a variadic templated function

2015-11-03 Thread Jonathan M Davis via Digitalmars-d-learn
On Tuesday, November 03, 2015 07:35:40 Nordlöw via Digitalmars-d-learn wrote:
> On Tuesday, 3 November 2015 at 06:14:14 UTC, Jonathan M Davis
> wrote:
> > You should pretty much never use __FILE__ or __LINE__ as
> > template arguments unless you actually need to. The reason is
> > that it will end up creating a new instantiation of the
> > template pretty much every time that it's used, which is going
> > to be mean a _lot_ of extra code bloat if you use the template
> > much. And in some, rare circumstances that may be exactly what
> > you want. But it almost never is.
> >
> > - Jonathan M Davis
>
> So why is this pattern is used all over std.experimental.logger?

I don't know. I haven't looked at std.experimental.logger much. I do vaguely
recall there being a discussion about that and there being something that
prevented it from using __FILE__ and __LINE__ as runtime arguments, but I
don't remember the reason. If I had to guess though, it would be be because
of variadic arguments, since AFAIK, you can't have any function parameters
after the variadic ones (even if they have default arguments), which makes
it so that you can't put __FILE__ and __LINE__ as default arguments at the
end like you'd normally do. Maybe that would be a good reason for a language
enhancement that made it possible. It doesn't make sense when you want to be
able provide arguments other than the default arguments to those trailing
parameters, but it does make sense when you just want to use the default
arguments - which really only makes sense with stuff like __FILE__ and
__LINE__, but it would allow us to get rid of all of the template bloat that
std.experimental.logger is going to generate if it's taking the __FILE__ and
__LINE__ as template arguments.

- Jonathan M Davis




Re: Bidirectional Filter

2015-11-03 Thread Jakob Ovrum via Digitalmars-d-learn

On Tuesday, 3 November 2015 at 08:41:11 UTC, Nordlöw wrote:
Is there a reason why std.algorithm.iteration.filter() doesn't 
propagate bidirectional access?


http://dlang.org/phobos/std_algorithm_iteration.html#filterBidirectional


Bidirectional Filter

2015-11-03 Thread Nordlöw via Digitalmars-d-learn
Is there a reason why std.algorithm.iteration.filter() doesn't 
propagate bidirectional access?


Re: proper range usage

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

On 11/02/2015 11:59 PM, Alex wrote:

> "Programming in D" book (the revision of 2015-10-24)

Oooh! That smells very fresh. :)

> In my case, the container class can't become empty. Even if it contains
> one single element, in this case the example should return true for
> begin == end, it is not empty.

That problem is solved by the convention that 'end' is one beyond the 
last valid element. So, when there is only the element 42, then 
begin==42 and end==43. Only when the last element (42 in this case) is 
consumed, begin==end.


> A further problem/wish is: I don't want to store my elements explicitly
> as members. So taking the simplest example on page 486 has a further
> meaning for me: It does not contain an array as member and it is not
> meant to.

Such ranges are called generators. As long as .front returns the current 
element, and popFront() advances to the next one, you don't need to 
store any array. (You seem to say the same thing, so I don't understand 
the question.)


Ali



proper range usage

2015-11-03 Thread Alex via Digitalmars-d-learn

Hi everybody,

first of all: this question is going to be unclear, because I'm 
lack of the "buzz word" I would like to ask about, sorry for this 
in advance.


I try to describe the problem, where I stuck and hope somebody 
could think just a step further. Just a hint where to read about 
the way of solution would be enough, I think.


Starting with chapter 73 in the recent "Programming in D" book 
(the revision of 2015-10-24), about "foreach with structs and 
classes" I would like to implement either the opAssign method or 
the three range member functions in a simple struct.
Let's say, the example of NumberRange struct in the example on 
page 486 is enough. In terms of .front(), .popFront() and .empty: 
I understand what the first two things do, but I have a problem 
with the empty property:


In my case, the container class can't become empty. Even if it 
contains one single element, in this case the example should 
return true for begin == end, it is not empty. At the same time, 
my container is surely always finite, so I can't define the empty 
property to false as shown on page 568, last line.


A further problem/wish is: I don't want to store my elements 
explicitly as members. So taking the simplest example on page 486 
has a further meaning for me: It does not contain an array as 
member and it is not meant to.


So far from me. I hope, I could describe my problem good enough 
and I hope somebody could help me out. Still, the solution is 
important, but I also would like to know what a weird thing I'm 
describing in terms of... well... some structure... not to say 
pattern :)


Thanks in advance
Alex