Re: problem with exceptions

2015-10-02 Thread steven kladitis via Digitalmars-d-learn

On Friday, 2 October 2015 at 12:18:36 UTC, Dmitri wrote:
On Friday, 2 October 2015 at 11:44:21 UTC, steven kladitis 
wrote:

C:\d\examples>pb2
=>main's first line
  =>makeOmelet's first line
=>prepareAll's first line
  =>prepareEggs's first line
object.Exception@pb2.d(64): Cannot take -8 eggs from the fridge

0x00402252
0x0040512F
0x00405043
0x00403E48
0x7600338A in BaseThreadInitThunk
0x77A497F2 in RtlInitializeExceptionChain
0x77A497C5 in RtlInitializeExceptionChain


- I always see the info at the bottom. Is this normal. I 
was thinkig I should only the the exception message itself.

---  this is windows 7 32 bit.


that would the stack of the thread leading up to the exception. 
I think you get that if you dump the exception object itself 
(not just the message).


-- code is below
import std.stdio;
import std.string;

void indent(in int level)
{
foreach (i; 0 .. level * 2)
{
write(' ');
}
}
void entering(in char[] functionName, in int level)
{
indent(level);
writeln("=>", functionName, "'s first line");
}
void exiting(in char[] functionName, in int level)
{
indent(level);
writeln("<=", functionName, "'s last line");
}
void main()
{
entering("main", 0);
makeOmelet(-8);
eatOmelet();
exiting("main", 0);
}
void makeOmelet(int eggCount)
{
entering("makeOmelet", 1);
prepareAll(eggCount);
cookEggs();
cleanAll();
exiting("makeOmelet", 1);
}
void eatOmelet()
{
entering("eatOmelet", 1);
exiting("eatOmelet", 1);
}
void prepareAll(int eggCount)
{
entering("prepareAll", 2);
prepareEggs(eggCount);
prepareButter();
preparePan();
exiting("prepareAll", 2);
}
void cookEggs()
{
entering("cookEggs", 2);
exiting("cookEggs", 2);
}
void cleanAll()
{
entering("cleanAll", 2);
exiting("cleanAll", 2);
}
void prepareEggs(int count)
{
entering("prepareEggs", 3);
if (count < 1)
{
throw new Exception(
  format("Cannot take %s eggs from the fridge", 
count));

}
exiting("prepareEggs", 3);
}
void prepareButter()
{
entering("prepareButter", 3);
exiting("prepareButter", 3);
}
void preparePan()
{
entering("preparePan", 3);
exiting("preparePan", 3);
}



Re: problem with exceptions

2015-10-02 Thread Dmitri via Digitalmars-d-learn

On Friday, 2 October 2015 at 12:45:38 UTC, steven kladitis wrote:

On Friday, 2 October 2015 at 12:18:36 UTC, Dmitri wrote:
On Friday, 2 October 2015 at 11:44:21 UTC, steven kladitis 
wrote:

C:\d\examples>pb2
=>main's first line
  =>makeOmelet's first line
=>prepareAll's first line
  =>prepareEggs's first line
object.Exception@pb2.d(64): Cannot take -8 eggs from the 
fridge


0x00402252
0x0040512F
0x00405043
0x00403E48
0x7600338A in BaseThreadInitThunk
0x77A497F2 in RtlInitializeExceptionChain
0x77A497C5 in RtlInitializeExceptionChain


- I always see the info at the bottom. Is this normal. I 
was thinkig I should only the the exception message itself.

---  this is windows 7 32 bit.


that would the stack of the thread leading up to the 
exception. I think you get that if you dump the exception 
object itself (not just the message).


-- code is below
import std.stdio;
import std.string;

void indent(in int level)
{
foreach (i; 0 .. level * 2)
{
write(' ');
}
}
void entering(in char[] functionName, in int level)
{
indent(level);
writeln("=>", functionName, "'s first line");
}
void exiting(in char[] functionName, in int level)
{
indent(level);
writeln("<=", functionName, "'s last line");
}
void main()
{
entering("main", 0);
makeOmelet(-8);
eatOmelet();
exiting("main", 0);
}
void makeOmelet(int eggCount)
{
entering("makeOmelet", 1);
prepareAll(eggCount);
cookEggs();
cleanAll();
exiting("makeOmelet", 1);
}
void eatOmelet()
{
entering("eatOmelet", 1);
exiting("eatOmelet", 1);
}
void prepareAll(int eggCount)
{
entering("prepareAll", 2);
prepareEggs(eggCount);
prepareButter();
preparePan();
exiting("prepareAll", 2);
}
void cookEggs()
{
entering("cookEggs", 2);
exiting("cookEggs", 2);
}
void cleanAll()
{
entering("cleanAll", 2);
exiting("cleanAll", 2);
}
void prepareEggs(int count)
{
entering("prepareEggs", 3);
if (count < 1)
{
throw new Exception(
  format("Cannot take %s eggs from the fridge", 
count));

}
exiting("prepareEggs", 3);
}
void prepareButter()
{
entering("prepareButter", 3);
exiting("prepareButter", 3);
}
void preparePan()
{
entering("preparePan", 3);
exiting("preparePan", 3);
}


I guess it's normal:
void main()
{
throw new Exception("duh");
}

-->
object.Exception@/home/d955/f505.d(3): duh
 
./f505(_D2rt6dmain211_d_run_mainUiPPaPUAAaZiZ6runAllMFZ9__lambda1MFZv+0x1f) [0x41e467] ./f505(void rt.dmain2._d_run_main(int, char**, extern (C) int function(char[][])*).tryExec(scope void delegate())+0x2a) [0x41e3c2] ./f505(void rt.dmain2._d_run_main(int, char**, extern (C) int function(char[][])*).runAll()+0x2b) [0x41e423] ./f505(void rt.dmain2._d_run_main(int, char**, extern (C) int function(char[][])*).tryExec(scope void delegate())+0x2a) [0x41e3c2] ./f505(_d_run_main+0x1d2) [0x41e342] ./f505(main+0x12) [0x41d9ce] /usr/lib/libc.so.6(__libc_start_main+0xf5) [0x40967a15]



Looks like the runtime always dumps the uncaught exception that 
way. If you need it any other way, catch by Exception and print 
only the message.


Re: Linker error with dmd

2015-10-02 Thread John Colvin via Digitalmars-d-learn

On Friday, 2 October 2015 at 09:43:54 UTC, Chris wrote:
Why do I get this error msg with dmd 2.067.1 and 2.068.0 in 
release mode:


$ dub --build=release

(.data._D65TypeInfo_xC3std5range10interfaces18__T10InputRangeTiZ10InputRange6__initZ+0x10):
 undefined reference to 
`_D64TypeInfo_C3std5range10interfaces18__T10InputRangeTiZ10InputRange6__initZ'
collect2: error: ld returned 1 exit status
--- errorlevel 1
dmd failed with exit code 1.

It works fine with the latest version of ldc2.


What is it that you are building?


Re: Checking that a template parameter is an enum

2015-10-02 Thread Per Nordlöw via Digitalmars-d-learn

On Thursday, 1 October 2015 at 22:37:57 UTC, Ali Çehreli wrote:

template allSame(V...)
if (isExpressions!(V))
{
bool impl_(V...)() {
static if (V.length > 1) {
foreach (i, _; V[0 .. $ - 1]) {
if (V[i] != V[i + 1]) {
return false;
}
}

return true;

} else {
return true;
}
}

enum allSame = impl_!V();
}


Will proposed `static foreach` reduce number of instantiations 
here?


Re: problem with exceptions

2015-10-02 Thread Mike Parker via Digitalmars-d-learn

On Friday, 2 October 2015 at 11:44:21 UTC, steven kladitis wrote:

C:\d\examples>pb2
=>main's first line
  =>makeOmelet's first line
=>prepareAll's first line
  =>prepareEggs's first line
object.Exception@pb2.d(64): Cannot take -8 eggs from the fridge

0x00402252
0x0040512F
0x00405043
0x00403E48
0x7600338A in BaseThreadInitThunk
0x77A497F2 in RtlInitializeExceptionChain
0x77A497C5 in RtlInitializeExceptionChain


- I always see the info at the bottom. Is this normal. I 
was thinkig I should only the the exception message itself.

---  this is windows 7 32 bit.


The info at the bottom is the backtrace. Compiling with -g should 
make it readable (with function names and such).

```
void bar(int i) {
if(i > 10)
throw new Exception("i is too large!!");
}

void main() {
bar(12);
}
```

dmd -g foo

object.Exception@foo.d(14): i is too large!!

0x00402055 in void foo.bar(int) at 
C:\LearningD\Chapter05\foo.d(15)

0x00402065 in _Dmain at C:\LearningD\Chapter05\foo.d(19)
0x00402916 in 
D2rt6dmain211_d_run_mainUiPPaPUAAaZiZ6runAllMFZ9__lambda1MFZv
0x004028EB in void rt.dmain2._d_run_main(int, char**, extern (C) 
int function(char[][])*).runAll()

0x004027FF in _d_run_main
0x004024F4 in main
0x00419F51 in mainCRTStartup
0x75803744 in BaseThreadInitThunk
0x770EA064 in RtlSetCurrentTransaction
0x770EA02F in RtlSetCurrentTransaction



Re: Checking that a template parameter is an enum

2015-10-02 Thread John Colvin via Digitalmars-d-learn

On Friday, 2 October 2015 at 08:13:00 UTC, John Colvin wrote:

On Thursday, 1 October 2015 at 22:26:39 UTC, Nordlöw wrote:

On Thursday, 1 October 2015 at 02:06:48 UTC, Fusxfaranto wrote:

[...]


Thanks!

BTW: Is there some way to turn the recursive definition of 
`allSame`


template allSame(V...)
if (isExpressions!(V))
{
static if (V.length <= 1)
enum allSame = true;
else
enum allSame = V[0] == V[1] && allSame!(V[1..$]);
}

into an iterative definition?


Why? To avoid slowing down compilation with all those template 
instantiations?


How about a O(log2(N)) depth recursive version, something like 
this:


template allSame(V ...)
if (isExpressions!V)
{
static if (V.length <= 1)
enum allSame = true;
else static if(V.length & 1)
enum allSame = V[$-1] == V[0]
&& V[0 .. $/2] == V[$/2 .. $-1]
&& allSame!(V[0 .. $/2]);
else
enum allSame = V[0..$/2] == V[$/2 .. $]
&& allSame!(V[0 .. $/2]);
}


Although you should consider that isExpressions is instantiating 
V.length templates anyway (it uses a binary split to avoid 
excessive template recursion depth, but it ends up checking them 
all one-per-template in the end anyway.


Re: How to do unittests

2015-10-02 Thread Namal via Digitalmars-d-learn

On Wednesday, 30 September 2015 at 14:44:20 UTC, qsdf wrote:

On Wednesday, 30 September 2015 at 14:20:28 UTC, Namal wrote:
On Wednesday, 30 September 2015 at 13:03:52 UTC, Rikki 
Cattermole wrote:

On 01/10/15 1:59 AM, Namal wrote:

Hello,

can someone give me a complete example please how to do 
unittests? I
tried this with the example from german wikipedia, but the 
flag

-unittest didn't make any difference.


Example file with loads of unittests: 
https://github.com/rikkimax/alphaPhobos/blob/master/source/std/experimental/uri.d


If you were to compile it e.g. dmd uri.d it won't be much use 
(unittest wise). You will need to dmd -unittest uri.d to 
compile them in. Don't forget to do the same for your main 
function. When you run the final executable the tests will 
execute before your main function does.


can't I do unittest in the main?


D unit tests are like a stack of free functions. You put them 
separatly.



when there's a main: dmd -unittest a.d
--
module a;
void main(){}

unittest{}
--


when there is no main: (like std.uri): dmd -main -unittest a.d
--
module a;
unittest{}
--

the -main switch adds a dummy main function so that the output 
can be executed.


But most of the time you'll think that nothing happens because 
the tests succeed...


So do I understand it right that it stops after the first failed 
test? Is it possible to continue and get a list of all failed 
tests?





Re: Checking that a template parameter is an enum

2015-10-02 Thread John Colvin via Digitalmars-d-learn

On Thursday, 1 October 2015 at 22:26:39 UTC, Nordlöw wrote:

On Thursday, 1 October 2015 at 02:06:48 UTC, Fusxfaranto wrote:

/** Returns: true iff all values $(D V) are the same. */
template allSame(V...)  // TODO restrict to values 
only

{
static if (V.length <= 1)
enum bool allSame = true;
else
enum bool allSame = V[0] == V[1] && allSame!(V[1..$]);
}


std.traits to the rescue!

http://dlang.org/phobos/std_traits.html#isExpressions

Using isExpressions!V as a template constraint looks like the 
behavior you're looking for.


Thanks!

BTW: Is there some way to turn the recursive definition of 
`allSame`


template allSame(V...)
if (isExpressions!(V))
{
static if (V.length <= 1)
enum allSame = true;
else
enum allSame = V[0] == V[1] && allSame!(V[1..$]);
}

into an iterative definition?


Why? To avoid slowing down compilation with all those template 
instantiations?


How about a O(log2(N)) depth recursive version, something like 
this:


template allSame(V ...)
if (isExpressions!V)
{
static if (V.length <= 1)
enum allSame = true;
else static if(V.length & 1)
enum allSame = V[$-1] == V[0]
&& V[0 .. $/2] == V[$/2 .. $-1]
&& allSame!(V[0 .. $/2]);
else
enum allSame = V[0..$/2] == V[$/2 .. $]
&& allSame!(V[0 .. $/2]);
}


chunkBy limitation?

2015-10-02 Thread Dmitri via Digitalmars-d-learn
I wondered if this is a current limitation of chunkBy 
implementation:


// http://dpaste.dzfl.pl/52d6a0c5d0ab
void bar(int[] foo)
{
bool less(int a, int b) // contrived
{
return a < b;
};

foo.sort!less.groupBy;
}

resulting in:

/opt/compilers/dmd2/include/std/algorithm/iteration.d(1529): 
Error: function f351.bar.SortedRange!(int[], 
less).SortedRange.groupBy!().groupBy.ChunkByImpl!(__lambda1, 
int[]).ChunkByImpl.Group.popFront cannot access frame of function 
f351.bar.SortedRange!(int[], less).SortedRange.groupBy!().groupBy

...

I can see that the implementation of chunkBy is nested:
struct ChunkByImpl
{
static struct Group {}
}

and that the predicate (less in this case) is accessed from the 
nested struct Group which is, probably, what's causing the 
compilation error.


The question is if this is an implementation error (i.e. Group 
should probably be moved to the top level) or a compiler issue?


Thanks in advance.


Re: Interval Arithmetic

2015-10-02 Thread ponce via Digitalmars-d-learn

On Thursday, 1 October 2015 at 21:13:30 UTC, Marco Leise wrote:


Nice to have in Phobos. I assume you have to set the correct 
control word depending on whether you perform math on the FPU 
or via SSE (as is standard for x86_64)? And I assume further 
that DMD always uses FPU math and other compilers provide flags 
to switch between FPU and SSE?


I don't know which compiler use which. On x86_64, a compiler is 
in practice free to mix-and-match FPU and SSE, the instructions 
are still there and working.


Linker error with dmd

2015-10-02 Thread Chris via Digitalmars-d-learn
Why do I get this error msg with dmd 2.067.1 and 2.068.0 in 
release mode:


$ dub --build=release

(.data._D65TypeInfo_xC3std5range10interfaces18__T10InputRangeTiZ10InputRange6__initZ+0x10):
 undefined reference to 
`_D64TypeInfo_C3std5range10interfaces18__T10InputRangeTiZ10InputRange6__initZ'
collect2: error: ld returned 1 exit status
--- errorlevel 1
dmd failed with exit code 1.

It works fine with the latest version of ldc2.


Re: How to do unittests

2015-10-02 Thread Atila Neves via Digitalmars-d-learn

On Friday, 2 October 2015 at 10:22:40 UTC, Namal wrote:

On Wednesday, 30 September 2015 at 14:44:20 UTC, qsdf wrote:

On Wednesday, 30 September 2015 at 14:20:28 UTC, Namal wrote:

[...]


D unit tests are like a stack of free functions. You put them 
separatly.



when there's a main: dmd -unittest a.d
--
module a;
void main(){}

unittest{}
--


when there is no main: (like std.uri): dmd -main -unittest a.d
--
module a;
unittest{}
--

the -main switch adds a dummy main function so that the output 
can be executed.


But most of the time you'll think that nothing happens because 
the tests succeed...


So do I understand it right that it stops after the first 
failed test? Is it possible to continue and get a list of all 
failed tests?


http://code.dlang.org/packages/unit-threaded
https://github.com/D-Programming-Language/phobos/pull/3207

Atila


problem with exceptions

2015-10-02 Thread steven kladitis via Digitalmars-d-learn

C:\d\examples>pb2
=>main's first line
  =>makeOmelet's first line
=>prepareAll's first line
  =>prepareEggs's first line
object.Exception@pb2.d(64): Cannot take -8 eggs from the fridge

0x00402252
0x0040512F
0x00405043
0x00403E48
0x7600338A in BaseThreadInitThunk
0x77A497F2 in RtlInitializeExceptionChain
0x77A497C5 in RtlInitializeExceptionChain


- I always see the info at the bottom. Is this normal. I was 
thinkig I should only the the exception message itself.

---  this is windows 7 32 bit.





Re: problem with exceptions

2015-10-02 Thread Dmitri via Digitalmars-d-learn

On Friday, 2 October 2015 at 11:44:21 UTC, steven kladitis wrote:

C:\d\examples>pb2
=>main's first line
  =>makeOmelet's first line
=>prepareAll's first line
  =>prepareEggs's first line
object.Exception@pb2.d(64): Cannot take -8 eggs from the fridge

0x00402252
0x0040512F
0x00405043
0x00403E48
0x7600338A in BaseThreadInitThunk
0x77A497F2 in RtlInitializeExceptionChain
0x77A497C5 in RtlInitializeExceptionChain


- I always see the info at the bottom. Is this normal. I 
was thinkig I should only the the exception message itself.

---  this is windows 7 32 bit.


that would the stack of the thread leading up to the exception. I 
think you get that if you dump the exception object itself (not 
just the message).


Re: How to break gdb on D exception ?

2015-10-02 Thread Dmitri via Digitalmars-d-learn

On Friday, 2 October 2015 at 04:50:59 UTC, BBasile wrote:

On Friday, 2 October 2015 at 04:46:51 UTC, BBasile wrote:

On Friday, 2 October 2015 at 04:24:11 UTC, Adam D. Ruppe wrote:

On Friday, 2 October 2015 at 03:58:45 UTC, BBasile wrote:

none of the following GB commands work:


give

break d_throw

or maybe `break d_throwc` a try


unfortunately it doesn't work, i get

---
(gdb) Function "d_throw"/"d_throwc" not defined.


it was almost that actually,
'break _d_throwc


Or you could break on a specific exception class's constructor.


Re: How to do unittests

2015-10-02 Thread Jonathan M Davis via Digitalmars-d-learn
On Friday, October 02, 2015 10:22:38 Namal via Digitalmars-d-learn wrote:
> So do I understand it right that it stops after the first failed
> test? Is it possible to continue and get a list of all failed
> tests?

Once a unittest block within a module has a failure in it, then no more
unittest blocks within that module are run. However, unittest blocks in
other modules will still be run. So, if you have failures across multiple
modules, then you'll see multiple failures, but if it's just one module,
then you'll only see the first one, because any further tests within the
module won't even have been run.

- Jonathan M Davis



Re: Checking that a template parameter is an enum

2015-10-02 Thread Meta via Digitalmars-d-learn

On Friday, 2 October 2015 at 07:52:22 UTC, Nordlöw wrote:

On Friday, 2 October 2015 at 02:39:56 UTC, Meta wrote:
Highly doubtful as CTFE already allocates like there's no 
tomorrow.


Could that memory usage be tested somehow?


Your favourite process monitor I guess.


Re: problem with exceptions

2015-10-02 Thread Jonathan M Davis via Digitalmars-d-learn
On Friday, October 02, 2015 11:44:19 steven kladitis via Digitalmars-d-learn 
wrote:
> C:\d\examples>pb2
> =>main's first line
>=>makeOmelet's first line
>  =>prepareAll's first line
>=>prepareEggs's first line
> object.Exception@pb2.d(64): Cannot take -8 eggs from the fridge
> 
> 0x00402252
> 0x0040512F
> 0x00405043
> 0x00403E48
> 0x7600338A in BaseThreadInitThunk
> 0x77A497F2 in RtlInitializeExceptionChain
> 0x77A497C5 in RtlInitializeExceptionChain
>
>
> - I always see the info at the bottom. Is this normal. I was
> thinkig I should only the the exception message itself.
> ---  this is windows 7 32 bit.

If you don't want to see a stack trace, then catch the exception and just
print its msg property. And if you want the file and line number in addition
to the message, then you can access those file the exception's file and line
properties. But the toString function on exceptions is always going to print
out the stack trace in addition to the message, which usually pretty useful,
though in this case, you didn't get the function names as part of the stack
trace, which is less useful. You probably need to build with the debug info
turned on to get it.

- Jonathan M Davis



Re: chunkBy limitation?

2015-10-02 Thread Ali Çehreli via Digitalmars-d-learn

On 10/02/2015 02:21 AM, Dmitri wrote:
> I wondered if this is a current limitation of chunkBy implementation:
>
> // http://dpaste.dzfl.pl/52d6a0c5d0ab
> void bar(int[] foo)
> {
>  bool less(int a, int b) // contrived
>  {
>  return a < b;
>  };
>
>  foo.sort!less.groupBy;
> }
>
> resulting in:
>
> /opt/compilers/dmd2/include/std/algorithm/iteration.d(1529): Error:
> function f351.bar.SortedRange!(int[],
> less).SortedRange.groupBy!().groupBy.ChunkByImpl!(__lambda1,
> int[]).ChunkByImpl.Group.popFront cannot access frame of function
> f351.bar.SortedRange!(int[], less).SortedRange.groupBy!().groupBy
> ...
>
> I can see that the implementation of chunkBy is nested:
> struct ChunkByImpl
> {
>  static struct Group {}
> }
>
> and that the predicate (less in this case) is accessed from the nested
> struct Group which is, probably, what's causing the compilation error.
>
> The question is if this is an implementation error (i.e. Group should
> probably be moved to the top level) or a compiler issue?
>
> Thanks in advance.

This is a known D issue. Currently, objects have a single context 
pointer. Here is a bug discussion about it:


  https://issues.dlang.org/show_bug.cgi?id=5710

A workaround for your example is making less() static, which removes its 
context pointer:


void bar(int[] foo)
{
import std.algorithm : sort;

static bool less(int a, int b) // contrived
{
return a < b;
}

foo.sort!less.groupBy;
}

void main() {}

I've just realized that I don't know how to handle the case where less() 
really needs some other state (e.g. it may need to use a local variable 
in main). What can we do?


Ali



Re: Checking that a template parameter is an enum

2015-10-02 Thread Meta via Digitalmars-d-learn

On Friday, 2 October 2015 at 18:52:22 UTC, Nordlöw wrote:

On Friday, 2 October 2015 at 16:21:22 UTC, Meta wrote:

Could that memory usage be tested somehow?


Your favourite process monitor I guess.


Any suggestions on an evil D snippet that stress tests 
different implementations of the above mentioned traits?


I don't know off the top of my head, but I remember somebody on 
these forums mentioning a certain CTFE program that caused DMD to 
take up over 100GB in RAM before the process was killed.


Re: Checking that a template parameter is an enum

2015-10-02 Thread Nordlöw via Digitalmars-d-learn

On Friday, 2 October 2015 at 16:21:22 UTC, Meta wrote:

Could that memory usage be tested somehow?


Your favourite process monitor I guess.


Any suggestions on an evil D snippet that stress tests different 
implementations of the above mentioned traits?


How to use std.range.interfaces in pure @safe code

2015-10-02 Thread Freddy via Digitalmars-d-learn
How do I use http://dlang.org/phobos/std_range_interfaces.html in 
pure @safe code?


Re: chunkBy limitation?

2015-10-02 Thread Dmitri via Digitalmars-d-learn

On Friday, 2 October 2015 at 18:16:59 UTC, Ali Çehreli wrote:

On 10/02/2015 02:21 AM, Dmitri wrote:
> [...]
implementation:
> [...]
Error:
> [...]
function
> [...]
less).SortedRange.groupBy!().groupBy
> [...]
the nested
> [...]
compilation error.
> [...]
Group should
> [...]

This is a known D issue. Currently, objects have a single 
context pointer. Here is a bug discussion about it:


  https://issues.dlang.org/show_bug.cgi?id=5710

A workaround for your example is making less() static, which 
removes its context pointer:


void bar(int[] foo)
{
import std.algorithm : sort;

static bool less(int a, int b) // contrived
{
return a < b;
}

foo.sort!less.groupBy;
}

void main() {}

I've just realized that I don't know how to handle the case 
where less() really needs some other state (e.g. it may need to 
use a local variable in main). What can we do?


Ali


Thanks, Ali - this is helpful. The problem was figuring out what 
exactly is wrong given a bunch of unhelpful error messages. A 
somewhat better message comes when using a unary predicate 
version of chunkBy.


How will std.allocator change how we program in D?

2015-10-02 Thread Taylor Hillegeist via Digitalmars-d-learn
I do not come from a c++ background. but have looked at what 
allocators do for c++. I know in D the standard for memory 
management is garbage collection and if we want to manage it 
ourselfs we have to do things like @nogc. I was just curious how 
the std allocator will change how we do things.


Re: Checking that a template parameter is an enum

2015-10-02 Thread Nordlöw via Digitalmars-d-learn

On Friday, 2 October 2015 at 02:39:56 UTC, Meta wrote:
Highly doubtful as CTFE already allocates like there's no 
tomorrow.


Could that memory usage be tested somehow?


Re: AWS API Dlang, hmac sha256 function.

2015-10-02 Thread Rikki Cattermole via Digitalmars-d-learn

On 03/10/15 6:01 PM, Rikki Cattermole wrote:

On 03/10/15 4:54 PM, holo wrote:

On Saturday, 3 October 2015 at 03:15:21 UTC, Rikki Cattermole wrote:


You could implement it yourself, (it looks pretty easy).
Or go the route of Botan:
https://github.com/etcimon/botan/blob/3bdc835d5b9bad7523deaa86fe98b1fbb2f09d6b/source/botan/mac/hmac.d





Thank you for answer. I used dub to fetch botan, after that i created
project:

dub init projectname botan

and tried to add botan module:

$ cat app.d
#!/usr/bin/rdmd

import std.stdio;
import botan.mac.hmac;

void main()
{
 writeln("Edit source/app.d to start your project.");
}

but when im trying to run my simple app i get:

$ ./app.d
./app.d(4): Error: module hmac is in file 'botan/mac/hmac.d' which
cannot be read
import path[0] = .
import path[1] = /usr/include/dlang/dmd
Failed: ["dmd", "-v", "-o-", "./app.d", "-I."]

What am i doing wrong? How to add library with dub package manager? Im
really beginner in D (basically in programming) sorry if im asking
obvious questions.


By the looks of that error message with paths, botan isn't actually
added as a dependency. Can you please paste the dub file?


O wait nevermind.
Use dub to compile/run your program. You are using the shebang line!

$ dub run


According to my other question are that functions equivalent to that
python one? If they are not doing exact that same i could not create
auth string which is needed to connect to AWS api.


I can't say for certain if it is not exact. The reason I did not answer
it is because I am not familiar with environment or any of what you are
doing.
I can unfortunately only point you in the right direction.




Re: AWS API Dlang, hmac sha256 function.

2015-10-02 Thread Rikki Cattermole via Digitalmars-d-learn

On 03/10/15 4:54 PM, holo wrote:

On Saturday, 3 October 2015 at 03:15:21 UTC, Rikki Cattermole wrote:


You could implement it yourself, (it looks pretty easy).
Or go the route of Botan:
https://github.com/etcimon/botan/blob/3bdc835d5b9bad7523deaa86fe98b1fbb2f09d6b/source/botan/mac/hmac.d




Thank you for answer. I used dub to fetch botan, after that i created
project:

dub init projectname botan

and tried to add botan module:

$ cat app.d
#!/usr/bin/rdmd

import std.stdio;
import botan.mac.hmac;

void main()
{
 writeln("Edit source/app.d to start your project.");
}

but when im trying to run my simple app i get:

$ ./app.d
./app.d(4): Error: module hmac is in file 'botan/mac/hmac.d' which
cannot be read
import path[0] = .
import path[1] = /usr/include/dlang/dmd
Failed: ["dmd", "-v", "-o-", "./app.d", "-I."]

What am i doing wrong? How to add library with dub package manager? Im
really beginner in D (basically in programming) sorry if im asking
obvious questions.


By the looks of that error message with paths, botan isn't actually 
added as a dependency. Can you please paste the dub file?



According to my other question are that functions equivalent to that
python one? If they are not doing exact that same i could not create
auth string which is needed to connect to AWS api.


I can't say for certain if it is not exact. The reason I did not answer 
it is because I am not familiar with environment or any of what you are 
doing.

I can unfortunately only point you in the right direction.


Re: How will std.allocator change how we program in D?

2015-10-02 Thread Laeeth Isharc via Digitalmars-d-learn
On Friday, 2 October 2015 at 23:54:18 UTC, Taylor Hillegeist 
wrote:
I do not come from a c++ background. but have looked at what 
allocators do for c++. I know in D the standard for memory 
management is garbage collection and if we want to manage it 
ourselfs we have to do things like @nogc. I was just curious 
how the std allocator will change how we do things.


others will give better answers.  I suppose containers (I think 
one of Andrei's next projects) will make a big difference.  you 
can look at the following, which is used by EMSI in production 
(and now based on Andrei's allocator), a company whose work was 
recently profiled by the New York Times.


https://github.com/economicmodeling/containers

btw you use nogc if you want to be sure you are not allocating by 
mistake (or track down rogue allocations you hadn't thought 
about).  and it gives users of your code confidence.  but if you 
don't allocate much, you don't absolutely need to use nogc.


OT: interesting talk by Jane Street technical guy on why they used Ocaml

2015-10-02 Thread Laeeth Isharc via Digitalmars-d-learn

https://www.youtube.com/watch?v=hKcOkWzj0_s

a little old but still relevant.  talks about importance of 
brevity and strong types for readability (also avoiding 
boilerplate).  two of the partners there committed to read every 
line of code (originally because they were terrified).  very hard 
to code review boilerplate carefully because it is just too dull! 
 (can't pay people enough!)


correctness is v important if you are doing high volumes.  but 
being able to iterate rapidly is important in other areas too.


value of predictable performance in generated code.

much easier to hire great programmers in ocaml.
way they switched wasn't a big strategic plan.  guy just turned 
up at a windows shop (spreadsheets with VB backends etc) in a 
temp job between university courses.  ended up being permanent 
thing.  started hiring people to help him with analysis.  became 
clearer they needed a better solution - nightmare from 
copying/paste with spreadsheets.  sent email to ocaml list and 15 
responses of which 12 great and 3 he hired - great ratio.  maybe 
do an experiment.  wrote first version of system in 3 months, 
worked well and slowly expanded from there.


part of attraction of ocaml was ability to hire.  later if they 
need more people they can teach them.  they don't hire bad 
programmers, and it's easy to teach good ones.


F# does not perform well.  needs to allocate!  F# developers 
dismissive 'you must have bug in your program'.  F# has null 
problem - becomes problem when calling .net libraries.  windows 
not great for high performance (timestamps weird).


AWS API Dlang, hmac sha256 function.

2015-10-02 Thread holo via Digitalmars-d-learn

Hello

I'm trying to contact AWS API with D according to documentation:

http://docs.aws.amazon.com/general/latest/gr/sigv4_signing.html

there is written example in python which i want to rewrite to D:

http://docs.aws.amazon.com/general/latest/gr/sigv4-signed-request-examples.html

I want to ask about such 2 things:

Is it equivalent in D:

hexDigest!SHA256("sometexttohash");

for such python method:

hashlib.sha256('sometexttohash').hexdigest()

?

Maybe is there some prettier solution in D eg:

toHexString(sha256Of("sometexttohash"));

or

SHA256 sha;
sha.start();
string data = "sometexttohash";
sha.put(data);
toHexString(sha.finish())

If those are equivalents to python method which should i choose?

Last but not least, how to write such function in D:

def sign(key, msg):
return hmac.new(key, msg.encode("utf-8"), 
hashlib.sha256).digest()


?

I can't find in standard libraryt hmac function, is it existing?

//holo


Re: AWS API Dlang, hmac sha256 function.

2015-10-02 Thread Rikki Cattermole via Digitalmars-d-learn

On 03/10/15 4:11 PM, holo wrote:

Hello

I'm trying to contact AWS API with D according to documentation:

http://docs.aws.amazon.com/general/latest/gr/sigv4_signing.html

there is written example in python which i want to rewrite to D:

http://docs.aws.amazon.com/general/latest/gr/sigv4-signed-request-examples.html


I want to ask about such 2 things:

Is it equivalent in D:

hexDigest!SHA256("sometexttohash");

for such python method:

hashlib.sha256('sometexttohash').hexdigest()

?

Maybe is there some prettier solution in D eg:

toHexString(sha256Of("sometexttohash"));

or

SHA256 sha;
sha.start();
string data = "sometexttohash";
sha.put(data);
toHexString(sha.finish())

If those are equivalents to python method which should i choose?

Last but not least, how to write such function in D:

def sign(key, msg):
 return hmac.new(key, msg.encode("utf-8"), hashlib.sha256).digest()

?

I can't find in standard libraryt hmac function, is it existing?

//holo


You could implement it yourself, (it looks pretty easy).
Or go the route of Botan: 
https://github.com/etcimon/botan/blob/3bdc835d5b9bad7523deaa86fe98b1fbb2f09d6b/source/botan/mac/hmac.d


Re: AWS API Dlang, hmac sha256 function.

2015-10-02 Thread holo via Digitalmars-d-learn
On Saturday, 3 October 2015 at 03:15:21 UTC, Rikki Cattermole 
wrote:


You could implement it yourself, (it looks pretty easy).
Or go the route of Botan: 
https://github.com/etcimon/botan/blob/3bdc835d5b9bad7523deaa86fe98b1fbb2f09d6b/source/botan/mac/hmac.d



Thank you for answer. I used dub to fetch botan, after that i 
created project:


dub init projectname botan

and tried to add botan module:

$ cat app.d
#!/usr/bin/rdmd

import std.stdio;
import botan.mac.hmac;

void main()
{
writeln("Edit source/app.d to start your project.");
}

but when im trying to run my simple app i get:

$ ./app.d
./app.d(4): Error: module hmac is in file 'botan/mac/hmac.d' 
which cannot be read

import path[0] = .
import path[1] = /usr/include/dlang/dmd
Failed: ["dmd", "-v", "-o-", "./app.d", "-I."]

What am i doing wrong? How to add library with dub package 
manager? Im really beginner in D (basically in programming) sorry 
if im asking obvious questions.


According to my other question are that functions equivalent to 
that python one? If they are not doing exact that same i could 
not create auth string which is needed to connect to AWS api.