Re: D JSON (WAT?!)

2014-07-25 Thread Meta via Digitalmars-d-learn

On Saturday, 26 July 2014 at 00:26:08 UTC, Ary Borenszweig wrote:
Or use Algebraic, but it currently doesn't support recursive 
type definitions.


Algebraic does support recursive type definitions.

import std.variant;

alias Rec = Algebraic!(int, This*);

void main()
{
//I'm not sure why this works
auto i = Rec(Rec(Rec(1)));

i = Rec(new Rec(new Rec(1)));
}


Re: myrange.at(i) for myrange.dropExactly(i).front

2014-07-25 Thread Jonathan M Davis via Digitalmars-d-learn

On Saturday, 26 July 2014 at 00:28:32 UTC, Ary Borenszweig wrote:

On 7/25/14, 6:39 PM, Jonathan M Davis wrote:

On Friday, 25 July 2014 at 21:33:23 UTC, Timothee Cour via
Digitalmars-d-learn wrote:

Is there a function for doing this?
myrange.at(i)
(with meaning of myrange.dropExactly(i).front)
it's a common enough operation (analog to myrange[i]; the 
naming is from

C++'s std::vector::at)


That would require a random access range, in which case you 
can just
index directly. For a non-random access range, which you're 
doing would

be the most direct way of doing it.

- Jonathan M Davis


No, the OP said the meaning was `myrange.dropExactly(i).front`, 
which is not a random access.


Sometimes you *do* want the n-th element of a range even if the 
range is not a random access.


That is an inherently expensive operation, so it would be a very 
bad idea IMHO to support it. The OP referenced vector, which has 
random access, and that's a completely different ballgame.


In general, when operating on ranges, you should be trying to 
iterate over them only once and to backtrack as little as 
possible if you have backtrack. It's true that's not always 
possible, but if at() were O(n), then it would make inefficient 
code less obvious.


I'd argue against at() working on non-random access ranges for 
the same reason that std.container doesn't support containers 
with a length property of O(n) - because it's a function that 
looks like it's O(1), and programmers will consistently think 
that it's O(1) and misuse it. C++ has had that problem with 
std::list' size function which is O(n). at() looks like it would 
be O(1) (and it always is in C++), so it would be inappropriate 
to have it in cases where it would need to be O(n), and since we 
already have [], why add at()? It exists on vector in addition to 
[] to give it range checking random-access. We already have that 
in D with [].


myrange.dropExactly(i).front makes it much more obvious what 
you're doing and that it's inefficient. It might be necessary in 
some cases, but we don't want to give the impression that it's 
cheap, which at() would do.


- Jonathan M Davis


Re: myrange.at(i) for myrange.dropExactly(i).front

2014-07-25 Thread Ary Borenszweig via Digitalmars-d-learn

On 7/25/14, 6:39 PM, Jonathan M Davis wrote:

On Friday, 25 July 2014 at 21:33:23 UTC, Timothee Cour via
Digitalmars-d-learn wrote:

Is there a function for doing this?
myrange.at(i)
(with meaning of myrange.dropExactly(i).front)
it's a common enough operation (analog to myrange[i]; the naming is from
C++'s std::vector::at)


That would require a random access range, in which case you can just
index directly. For a non-random access range, which you're doing would
be the most direct way of doing it.

- Jonathan M Davis


No, the OP said the meaning was `myrange.dropExactly(i).front`, which is 
not a random access.


Sometimes you *do* want the n-th element of a range even if the range is 
not a random access.


Re: D JSON (WAT?!)

2014-07-25 Thread Ary Borenszweig via Digitalmars-d-learn

On 7/25/14, 1:06 PM, Justin Whear wrote:

On Thu, 24 Jul 2014 22:00:43 +, Pavel wrote:


On Thursday, 24 July 2014 at 16:09:25 UTC, Justin Whear wrote:

On Thu, 24 Jul 2014 16:04:01 +, Pavel wrote:


Thanks to all you folks who explained "in" operator for me. My bad.
Let's focus on the real problem, which is JSON wrapper class. Is it
needed? Wouldn't it be better to get AA from parseJSON?


The following are valid JSON:

auto json1 = parseJSON(`1`);
auto json2 = parseJSON(`"foo"`);
auto json3 = parseJSON(`[1, 2, 3]`);

None of these fit naturally into an JSONValue[string] return type.


Now we figured it out about JSON, but in that case:
Why not just use std.variant.Variant construct instead of JSONValue?


While I suspect the reason is simply historical, it might be a type-
safety/information problem as well.  Variant can store values of
essentially any type whereas JSON is strictly limited to a handful of
simple types.  Going from a JSON string to Variant might not be
troublesome, but going from Variant to JSON string almost certainly would.

That said, if you think it's worth it, I'd be up for reviewing a revamped
std.json.


Or use Algebraic, but it currently doesn't support recursive type 
definitions.


I think this would be the best way.



Re: Equivalent of DllMain on OSX?

2014-07-25 Thread Mark Isaacson via Digitalmars-d-learn


Loading multiple D shared libraries isn't supported on OS X 
yet, see these warnings in druntime:


https://github.com/D-Programming-Language/druntime/blob/master/src/rt/sections_osx.d#L198

If you only have a single D shared library, I think it's 
possible, you just may have to tweak dmd/druntime or your build 
process a bit.


I think Martin Nowak or Jacob Carlborg can really answer your 
questions, as Martin wrote a lot of that code and Jacob seems 
to follow it closely, whereas I don't use OS X but looked at 
that code when porting to Android.


It would only be a single shared library.



Finally got the code publicly available:
https://github.com/markisaa/presto-odbc/blob/osxCompile/driver/driver.d#L61

It should compile on OSX with a simple `make clean all`
Testing would require installing the iODBC driver manager and
pressing the 'Add Driver' button, possibly making a new User DSN,
and possibly pressing 'Test'.


Re: Equivalent of DllMain on OSX?

2014-07-25 Thread Joakim via Digitalmars-d-learn

On Friday, 25 July 2014 at 21:23:00 UTC, Mark Isaacson wrote:
I am presently trying to port a driver I wrote for Windows to 
OSX. The one thing standing in my way is figuring out how to 
get the equivalent of DllMain on OSX.


I need a place to call Runtime.initialize() and whatnot.

Reading the wiki, it seemed like `shared static this()`  was 
the appropriate construct to use, and indeed, when I run the 
test program from:

http://dlang.org/dll-linux.html#dso9

I get the desired results: namely, I see that the `shared 
static this()` in that test program is called the desired point.


When I try the same thing in my driver, it is never executed.

Are dylibs on OSX not supported still (I saw some very old 
threads from 2012 stating as much)? Does it matter that I have 
multiple modules in the driver? Any help would be appreciated.


I shall endeavor to get the modified driver code into a state 
where I can link it as well.


Loading multiple D shared libraries isn't supported on OS X yet, 
see these warnings in druntime:


https://github.com/D-Programming-Language/druntime/blob/master/src/rt/sections_osx.d#L198

If you only have a single D shared library, I think it's 
possible, you just may have to tweak dmd/druntime or your build 
process a bit.


I think Martin Nowak or Jacob Carlborg can really answer your 
questions, as Martin wrote a lot of that code and Jacob seems to 
follow it closely, whereas I don't use OS X but looked at that 
code when porting to Android.


Re: myrange.at(i) for myrange.dropExactly(i).front

2014-07-25 Thread Jonathan M Davis via Digitalmars-d-learn
On Friday, 25 July 2014 at 21:33:23 UTC, Timothee Cour via 
Digitalmars-d-learn wrote:

Is there a function for doing this?
myrange.at(i)
(with meaning of myrange.dropExactly(i).front)
it's a common enough operation (analog to myrange[i]; the 
naming is from

C++'s std::vector::at)


That would require a random access range, in which case you can 
just index directly. For a non-random access range, which you're 
doing would be the most direct way of doing it.


- Jonathan M Davis


myrange.at(i) for myrange.dropExactly(i).front

2014-07-25 Thread Timothee Cour via Digitalmars-d-learn
Is there a function for doing this?
myrange.at(i)
(with meaning of myrange.dropExactly(i).front)
it's a common enough operation (analog to myrange[i]; the naming is from
C++'s std::vector::at)


Equivalent of DllMain on OSX?

2014-07-25 Thread Mark Isaacson via Digitalmars-d-learn
I am presently trying to port a driver I wrote for Windows to 
OSX. The one thing standing in my way is figuring out how to get 
the equivalent of DllMain on OSX.


I need a place to call Runtime.initialize() and whatnot.

Reading the wiki, it seemed like `shared static this()`  was the 
appropriate construct to use, and indeed, when I run the test 
program from:

http://dlang.org/dll-linux.html#dso9

I get the desired results: namely, I see that the `shared static 
this()` in that test program is called the desired point.


When I try the same thing in my driver, it is never executed.

Are dylibs on OSX not supported still (I saw some very old 
threads from 2012 stating as much)? Does it matter that I have 
multiple modules in the driver? Any help would be appreciated.


I shall endeavor to get the modified driver code into a state 
where I can link it as well.


Re: dual with statement

2014-07-25 Thread monarch_dodra via Digitalmars-d-learn

On Friday, 25 July 2014 at 01:54:53 UTC, Jay Norwood wrote:

On Thursday, 24 July 2014 at 20:16:53 UTC, monarch_dodra wrote:


Or did I miss something?


Yes, sorry, I should have pasted a full example previously. The 
code at the end is with the Raw_met members renamed (they were 
originally a and b but clashed).


So, if Raw_met members were still a and b ...
  with(ar.r as v) with (ar.rm as res){
 res.a = v.a + v.c;
 res.b = (v.c==0)? 0: (v.a + v.b)/ v.c;

I don't recall the exact use case for the database expressions, 
but I believe they were substituting a simple symbol for the 
fully qualified object. So they would have done something 
analogous to this ...


Isn't:
with(foo.bar as baz) { ... }

Functionally nothing more than an alias? EG:
{
alias baz = foo.bar;
...
}

Or mix of both. I don't see a need for "as".


Re: findSplit

2014-07-25 Thread Jakob Ovrum via Digitalmars-d-learn

On Friday, 25 July 2014 at 18:56:44 UTC, Gecko wrote:
findSplit accepts a predicate but it doesnt compile for me if i 
use an other function than ((a,b) => a == b).


Not at the moment. I've been working on a patch that makes the 
`findSplit*` family of algorithms as powerful as `find`, 
including support for your use case.


findSplit

2014-07-25 Thread Gecko via Digitalmars-d-learn

Hello,
is there a pretty way to split a range into 3 pieces, like 
findSplit, just with a predicate instead of comparing it with an 
element.


Like this :

void main() {
import std.algorithm, std.stdio;

auto list = [1,2,3,4,5,6,7];
writeln(findSplit!(x => x == 3 )(list));
//should print Tuple!() ([1,2], [3], [4,5,6,7])
}

findSplit accepts a predicate but it doesnt compile for me if i 
use an other function than ((a,b) => a == b).


Re: D JSON (WAT?!)

2014-07-25 Thread Justin Whear via Digitalmars-d-learn
On Thu, 24 Jul 2014 22:00:43 +, Pavel wrote:

> On Thursday, 24 July 2014 at 16:09:25 UTC, Justin Whear wrote:
>> On Thu, 24 Jul 2014 16:04:01 +, Pavel wrote:
>>> 
>>> Thanks to all you folks who explained "in" operator for me. My bad.
>>> Let's focus on the real problem, which is JSON wrapper class. Is it
>>> needed? Wouldn't it be better to get AA from parseJSON?
>>
>> The following are valid JSON:
>>
>> auto json1 = parseJSON(`1`);
>> auto json2 = parseJSON(`"foo"`);
>> auto json3 = parseJSON(`[1, 2, 3]`);
>>
>> None of these fit naturally into an JSONValue[string] return type.
> 
> Now we figured it out about JSON, but in that case:
> Why not just use std.variant.Variant construct instead of JSONValue?

While I suspect the reason is simply historical, it might be a type-
safety/information problem as well.  Variant can store values of 
essentially any type whereas JSON is strictly limited to a handful of 
simple types.  Going from a JSON string to Variant might not be 
troublesome, but going from Variant to JSON string almost certainly would.

That said, if you think it's worth it, I'd be up for reviewing a revamped 
std.json.


Re: Associative array to Struct at compile time

2014-07-25 Thread BlackEdder via Digitalmars-d-learn

On Friday, 25 July 2014 at 15:48:54 UTC, John Colvin wrote:

On Friday, 25 July 2014 at 15:25:43 UTC, BlackEdder wrote:
Is it possible to automatically convert an associative array 
to a

struct?

Basically I want to do the following (for any struct).

struct A {
   double x = 1;
}

double[string] aa = ["x":1];

auto a = toStruct!A( aa );

I've been trying to do this at compile time, but can't work out
how setMembers and or loop over the associative array at 
compile

time.

Is this possible at all?


one possible way:

enum aa = ["x": 1];
import std.traits;

struct AAtoStruct(alias aa)
if(isAssociativeArray!(typeof(aa)) && 
isSomeString!(KeyType!(typeof(aa

{
alias T = ValueType!(typeof(aa));

import std.range, std.algorithm, std.array;
mixin(zip(aa.keys, aa.values)
  .map!`"T " ~ a[0] ~ " = " ~ a[1].to!string ~ ";\n"`
  .joiner.array);
}

alias S = AAtoStruct!aa;


The associative array is actually not known at compile time. I 
got it working though:


unittest {
  T toStruct( T )( double[string] aa ) {
T t;
foreach (name; __traits(allMembers, T))
{
  static if(
  __traits(compiles, __traits(getMember, t, name))
  //  Skip Functions
  && !isSomeFunction!(__traits(getMember, t, name) )
  )
  {
mixin( "t." ~ name ~ "= aa[\"" ~ name ~ "\"];" );
  }
}
return t;
  }

  struct A {
double x = -1;
 }

 double[string] aa = ["x":1];

 auto a = toStruct!A( aa );

 assert( a.x == 1 );
}



Re: Associative array to Struct at compile time

2014-07-25 Thread John Colvin via Digitalmars-d-learn

On Friday, 25 July 2014 at 15:25:43 UTC, BlackEdder wrote:
Is it possible to automatically convert an associative array to 
a

struct?

Basically I want to do the following (for any struct).

struct A {
double x = 1;
}

double[string] aa = ["x":1];

auto a = toStruct!A( aa );

I've been trying to do this at compile time, but can't work out
how setMembers and or loop over the associative array at compile
time.

Is this possible at all?


one possible way:

enum aa = ["x": 1];
import std.traits;

struct AAtoStruct(alias aa)
if(isAssociativeArray!(typeof(aa)) && 
isSomeString!(KeyType!(typeof(aa

{
alias T = ValueType!(typeof(aa));

import std.range, std.algorithm, std.array;
mixin(zip(aa.keys, aa.values)
  .map!`"T " ~ a[0] ~ " = " ~ a[1].to!string ~ ";\n"`
  .joiner.array);
}

alias S = AAtoStruct!aa;


Re: Array Operations question

2014-07-25 Thread bearophile via Digitalmars-d-learn

Márcio Martins:


float[10] a;
a[] = uniform(0.0f, 1.0f);

This is going to set all values to the result of a single call 
to uniform();


Is there a way to express my intent that I want one result per 
array element?


Currently D array operations can't be used for that kind of 
usage. So you have to use a normal foreach loop:


void main() {
import std.stdio, std.random;

float[10] a;

foreach (ref x; a)
x = uniform01;

a.writeln;
}


Or you can also use ranges, in a functional style:


void main() {
import std.stdio, std.range, std.random, std.algorithm;

float[10] a;

a
.length
.iota
.map!(_ => uniform01)
.copy(a[]);

a.writeln;
}

Bye,
bearophile


Array Operations question

2014-07-25 Thread via Digitalmars-d-learn

Hello!

I started exploring D today and one of the things that caught my 
attention was the "array operations" feature.


While playing around and writing some code with it, I came across 
a problem I wasn't able to find a solution for. The following is 
a simple example of the assignment array operator to initialize 
an array.


float[10] a;
a[] = uniform(0.0f, 1.0f);

This is going to set all values to the result of a single call to 
uniform();


Is there a way to express my intent that I want one result per 
array element?


Re: Associative array to Struct at compile time

2014-07-25 Thread bearophile via Digitalmars-d-learn

BlackEdder:


Is this possible at all?


I think it's possible, as long as your associative array contents 
are known at compile-time. You can write a function that given 
the associative array returns a string that mixed-in defines the 
struct. And then you can define a toStruct template.


Bye,
bearophile


Associative array to Struct at compile time

2014-07-25 Thread BlackEdder via Digitalmars-d-learn

Is it possible to automatically convert an associative array to a
struct?

Basically I want to do the following (for any struct).

struct A {
double x = 1;
}

double[string] aa = ["x":1];

auto a = toStruct!A( aa );

I've been trying to do this at compile time, but can't work out
how setMembers and or loop over the associative array at compile
time.

Is this possible at all?


Re: dual with statement

2014-07-25 Thread Jay Norwood via Digitalmars-d-learn

On Friday, 25 July 2014 at 01:54:53 UTC, Jay Norwood wrote:
I don't recall the exact use case for the database expressions, 
but I believe they were substituting a simple symbol for the 
fully qualified object.


The sql with clause is quite a bit different than I remembered.  
For one thing, I have the order reversed, so it would have been 
with (a as something.x.y).


It looks more like they are more like creating a temporary tuple 
from some more complicated selection.  It isn't clear what the 
implementation is, but just the that it might be a more concise 
way of stating things so that the expressions inside the body can 
be simple


So, my prior example
  with (ar.rm.a as d) with (ar.rm.b as e) with (ar.r.a as a) with
(ar.r.b as b) with (ar.r.c as c){
 d = a + c;
 e = (c==0)?0:(a+b)/c;
}


would reduce to something maybe simpler to read.  The sql seems 
to be using this as a foreach type operation, but I was just 
interested in the name substitution so that the expressions 
inside the with block could be simpler, as well as get rid of the 
struct member name clashes.


with ( (d,e,a,b,c) as (ar.rm.a, ar.rm.b, ar.r.a, ar.r.b, ar.r.c)){
 d = a + c;
 e = (c==0)?0:(a+b)/c;
}


Re: Mocking serial device

2014-07-25 Thread Alfredo Palhares via Digitalmars-d-learn

Hello.

Just to finish this thread, umockdev[1] seems to be just what I 
want.

Thank you for your sugestion!

[1] https://github.com/martinpitt/umockdev


Trying to add automatic radix detection in std.conv.parse

2014-07-25 Thread klb via Digitalmars-d-learn
hello, I'd like to test if it's possible to add an automatic 
radix detection in std.conv, parse.


I've added...

if (s.length >= 2)
{
if (s[0..2] == "0x" || s[0..2] == "0X")
{
uint radix = 16;
Source nbr = s[2..$];
return .parse!Target(nbr, radix);
}
else if (s[0..2] == "0b" || s[0..2] == "0B")
{
uint radix = 2;
Source nbr = s[2..$];
return .parse!Target(nbr, radix);
}
}


...to the first parse version. The (maybe `naive`) idea is to 
redirect to the right parse version if the prefixes are found.


But it seems to break the purity of format() because I get , when 
compiling phobos, the following errors:



std\utf.d(71): Error: pure function 'std.utf.UTFException.this' 
cannot call impure function 'std.str

ing.format!(char, uint).format'
std\utf.d(71): Error: safe function 'std.utf.UTFException.this' 
cannot call system function 'std.str

ing.format!(char, uint).format'
std\uuid.d(1536): Error: pure function 
'std.uuid.UUIDParsingException.this' cannot call impure funct

ion 'std.string.format!(char, string, string, uint).format'


What's wrong ?


Showing a user specified error message when no overloads match

2014-07-25 Thread via Digitalmars-d-learn
I have a template function with a particular constraint (in this 
case [1]). When this constraint doesn't match, I want to give the 
user a suggestion what to do instead.


The only way I know of to do this currently is to relax the 
template constraints, and adding a `static assert`. However, if 
there is another function that would match, this would lead to an 
ambiguity.


Is there a way to trigger the static assert only if there are no 
other overloads (including members and UFCS functions) that 
match? Or maybe a solution that only works for this particular 
case?


[1] https://github.com/D-Programming-Language/phobos/pull/2350