Re: Skipping or Stepping Through an Array?

2020-10-21 Thread DMon via Digitalmars-d-learn
On Wednesday, 21 October 2020 at 16:38:34 UTC, Ferhat Kurtulmuş 
wrote:

ş

UTF-8: Å
Numeric: 
Ansi: ÅŸ


√




Re: Skipping or Stepping Through an Array?

2020-10-21 Thread DMon via Digitalmars-d-learn
On Wednesday, 21 October 2020 at 13:04:40 UTC, Ferhat Kurtulmuş 
wrote:

import std.range;
import std.stdio;

void main(){
auto source = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
auto chunks = chunks(source, 2);

writeln(chunks[0]); // [1, 2]

foreach(c; chunks)
writeln(c[1]);
}


And, thank you Kurtulmuṣ (that's the closest "s" this keyboard 
has).


I've played with std.range but didn't think a control structure 
or import should be necessary.





Re: Skipping or Stepping Through an Array?

2020-10-21 Thread DMon via Digitalmars-d-learn

On Wednesday, 21 October 2020 at 13:43:51 UTC, matheus wrote:

foreach (i,j;a){
if(i%2==0){ write(j, ", ");}
}


Thank you, matheus. for each on the list.


Re: Skipping or Stepping Through an Array?

2020-10-21 Thread DMon via Digitalmars-d-learn

On Wednesday, 21 October 2020 at 12:06:00 UTC, drug wrote:

There are two other way:


Thanks, drug.

stride was what I was looking for.



Skipping or Stepping Through an Array?

2020-10-21 Thread DMon via Digitalmars-d-learn

What is the simplest way to output every nth element of an array?

I can do it using a for loop:
void main()
{
int[5] a = [1,2,3,4,5];

for (int i = 0 ; i <= 4 ; i += 2)
{
writeln(a[i]);
}
}

Basically, I am wondering if I missed something.


Re: List of exceptions?

2020-10-12 Thread DMon via Digitalmars-d-learn
On Monday, 12 October 2020 at 09:11:35 UTC, Dominikus Dittes 
Scherkl wrote:




- ...not care about exceptions someone else defined...except 
for printing out their message in main()...
- ...not reuse exceptions defined by someone else. Define your 
own.

- ...only if you have a plan...
- ...no plan, better throw error...where and why the program 
crashed...


Therefore a list of possible exceptions doesn't make any sense.


Thanks, Dominikus, those are great ideas.




Re: List of exceptions?

2020-10-10 Thread DMon via Digitalmars-d-learn

On Saturday, 10 October 2020 at 19:55:44 UTC, Ali Çehreli wrote:

On 10/10/20 12:51 PM, DMon wrote:


Thank you for your and Imperatorns time.

Even if it did go in circles and get stuck in the mud.


Re: List of exceptions?

2020-10-10 Thread DMon via Digitalmars-d-learn

On Saturday, 10 October 2020 at 18:16:45 UTC, Ali Çehreli wrote:

On 10/10/20 9:16 AM, DMon wrote:

> catch (Exception e) // implicit (any exception)
> catch (ConvException f) // explicit (conversion only)
>
> Or is that not correct?

I think in class hierarchies, "more general" and "more 
specific" are better terms. :)


The answer is, catch by the most general under the Exception 
hierarchy that you care about. It depends on the program. In 
most of my programs, catching Exception in main is sufficient 
because I just print the error message.


However, sometimes the error message does not make sense at 
that level:


So, you can augment that error with another one:

One cool thing about storing the 'actual' exception is, you can 
later debug it by catching the specific FooException and 
printing 'actual' as is, which contains the stack trace:


But really, it all depends on your program. The simplest thing 
may to not catch at all. The default behavior is to dump the 
stack trace and it works for some programs.


Ali


I will copy that down.

The idea for specific exceptions came from the online docs and 
Programing in D, 39.2 The try-catch statemet


try
{ // the code block that is being executed, where an // exception 
may be thrown

}
catch (an_exception_type)
{ // expressions to execute if an exception of this // type is 
caught

}
catch (another_exception_type)
{ // expressions to execute if an exception of this // other type 
is caught // ... more catch blocks as appropriate ...

}
finally
{ // expressions to execute regardless of whether an // exception 
is thrown

}


This is where I'm at:

import std.stdio;
import std.conv;

// StdioException
// ConvException
// StringException
// ErrnoException
// FormatException
// UnicodeException
// UTFException
// FileMissingException
// DataCorruptionException
// FE_INEXACT
// FE_UNDERFLOW
// FE_OVERFLOW
//

int main()
{


string z1 = "+ "; string z2 = "++ ";
bool a1 = false; bool a2 = true;
int b1 = 0; int b2 = 1;
uint c1 = 0; uint c2 = 1;
float d1 = 1f; float d2 = 2f;
char e1 = 'a'; char e2 = 'b';

int o1; int o2;

//auto test;
int[3] ar1; int[5] ar2;
string st1 = "arg";


writeln("Control\n\n");

/*
writeln("Testing");
try
{
writeln(z1 ~ e1 ~ st1);
writeln();
}
catch (Exception y1)
{
writeln("Something ", y1.msg,
  y1.info);
}
*/


writeln("try...catch");
try
{
o1 = to!int("hello");

}
catch (Exception i1)
{
writefln(z1 ~ "Message from exception i1: %s", i1.msg);
writefln(z1 ~ "Info from exception i1: %s", i1.info);
}
writeln();

writeln("try...finally");
try // Will run as normal code.
{
o1 = a2 + b2;
writeln(z1 ~ "", o1);
}
finally
{
writeln("Continues to run normally and no exceptions are 
displayed.");

}
writeln();

writeln("try...catch...finally");
try
{
  to!int(z1);
  to!int(z2);
}
catch (ConvException j1)
{
writefln(z1 ~ "1st Exception msg: %s", j1.msg);
writeln();
}
catch (Exception k1)
{
writeln(z1 ~ "2nd Exception msg: %s", k1.msg);
}
finally
{
writeln(z1 ~ "There are two exceptions.");
writeln(z2 ~ "The first exception is caught and that, 
immediately, exits the try clause.");

}
writeln("This still runs.");
writeln();


/*
writeln("Nesting");
try
{
*/
return 0;

}


Re: List of exceptions?

2020-10-10 Thread DMon via Digitalmars-d-learn

On Saturday, 10 October 2020 at 16:37:23 UTC, Imperatorn wrote:

On Saturday, 10 October 2020 at 12:12:35 UTC, DMon wrote:
To clarify, do you want a list of *possible* exceptions, like 
in Java?


Please.

I've been looking and thinking that I'm over complicating it for 
myself so it may not be necessary. Instead use multiples of 
catch(Exception var) and writefln(var.some_method, 
var.another_method) for multiple exceptions.


Re: List of exceptions?

2020-10-10 Thread DMon via Digitalmars-d-learn

On Saturday, 10 October 2020 at 16:00:26 UTC, Ali Çehreli wrote:

On 10/10/20 8:46 AM, DMon wrote:
On Saturday, 10 October 2020 at 14:56:31 UTC, Ali Çehreli 
wrote:

On 10/10/20 5:12 AM, DMon wrote:
Is there a list of a list of the exceptions or what can be 
used with catch?


Only Throwable and classes that are derived from it can be 
thrown and caught.


Ali
Is explicite cathing the best practice or is implicite how its 
done?


I don't know implicit catching. I would like to learn from 
others.


Ali


Maybe those are the wrong words.
catch (Exception e) // implicit (any exception)
catch (ConvException f) // explicit (conversion only)

Or is that not correct?

You wrote a book and I'm typing snippets.


Re: List of exceptions?

2020-10-10 Thread DMon via Digitalmars-d-learn

On Saturday, 10 October 2020 at 14:56:31 UTC, Ali Çehreli wrote:

On 10/10/20 5:12 AM, DMon wrote:
Is there a list of a list of the exceptions or what can be 
used with catch?


Only Throwable and classes that are derived from it can be 
thrown and caught.


Ali


Thanks for the reply.

I am looking to catch exceptions explicitly and get that it does 
not have to be. Is explicite cathing the best practice or is 
implicite how its done?


List of exceptions?

2020-10-10 Thread DMon via Digitalmars-d-learn
Is there a list of a list of the exceptions or what can be used 
with catch?


I'm thinking that I missed it and there is something easier than 
breaking old code, scouring the site, or hypnotic regression.


Re: If and isType with arrays

2020-10-07 Thread DMon via Digitalmars-d-learn

On Wednesday, 7 October 2020 at 16:39:07 UTC, Paul Backus wrote:

On Wednesday, 7 October 2020 at 16:25:33 UTC, DMon wrote:
Can isType be used as a condition in an if statement with 
arrays?


You can do this with `is()` and `typeof()`:

if (is(typeof(a) == int[5]))
{
write("true");
}

The example you have that "works" is just a coincidence:


I had previously gotten your example to work along with other 
basic properties to enter the if statement and isType in the 
write function.


Thanks for the clarification and the note on the second part.




If and isType with arrays

2020-10-07 Thread DMon via Digitalmars-d-learn

Can isType be used as a condition in an if statement with arrays?

import std.stdio;
import std.traits;

void main()
{
int[5] a = [1,2,3,4,5];

// Something like this:
if (a == isType!(int[5]))
{
write("true");
}

// This works:
if (a[0] == isType!(int))
{
write("true");
}
}


Re: Array Slicing

2020-09-29 Thread DMon via Digitalmars-d-learn

On Sunday, 27 September 2020 at 14:25:34 UTC, H. S. Teoh wrote:
On Sun, Sep 27, 2020 at 01:59:07PM +, DMon via 
Digitalmars-d-learn wrote:

Are these in the Specification or Phobos?


See: https://dlang.org/articles/d-array-article.html


T


Or in Articles?

Thanks, Teoh.


Array Slicing

2020-09-27 Thread DMon via Digitalmars-d-learn
Are these in the Specification or Phobos? I figured them out a 
few days ago. These are from my snippet journal on arrays and 
there may be more.


void main()
{
int[5] a = [1, 2, 3, 4, 5];
int[5][3] b = [[6, 7, 8, 9, 10], [11, 12, 13, 14, 15], [16, 
17, 18, 19, 20]];

int[5] c;

// This:
writeln(a[1..4][0]);
writeln(b[0][1..4]);
writeln(b[1][1..4][2]);
//writeln(b[0..1][1..3]); // Range violation.
//writeln(a[$-2..$-3]); // NG.
writeln(a[$-3..$-2])

// And, this:
c[0..5] = a[0..$-0]
writeln(c);
}

My apologizes, if any if these are noted or wrong.