Re: How to enforce compile time evaluation (and test if it was done at compile time)

2017-02-27 Thread Christian Köstlin via Digitalmars-d-learn
On 28/02/2017 01:20, sarn wrote:
> On Monday, 27 February 2017 at 19:26:06 UTC, Christian Köstlin wrote:
>> How can I make sure, that the calculations are done at compile time?
> 
> If you ever have doubts, you can always use something like this to check:
> 
> assert (__ctfe);
Thanks a lot, actually works as you describe it!
As I understand the only difference between assert and enforce is, that
assert is not compiled into releases?

Thanks!
Christian



Re: Recommend: IDE and GUI library

2017-02-27 Thread thedeemon via Digitalmars-d-learn

On Friday, 24 February 2017 at 22:44:55 UTC, XavierAP wrote:
Hi I've looked at wiki.dlang.org/IDEs, and I see that Visual D 
is linked from dlang.org/download.html. Still I was looking for 
personal opinions and experiences beyond hard specs, I wonder 
if one of the IDEs is already dominant at least for each OS for 
any good reason.


I don't think there is anything dominant, different people tend 
to make different choices.
For me Visual-D served well for years, and for GUI on Windows 
I've used DFL successfully (quite nice lib, very WinForms-like, 
with a visual editor) and now mostly use DLangUI (on both Windows 
and Linux).


Re: How to enforce compile time evaluation (and test if it was done at compile time)

2017-02-27 Thread sarn via Digitalmars-d-learn

On Tuesday, 28 February 2017 at 00:20:05 UTC, sarn wrote:
On Monday, 27 February 2017 at 19:26:06 UTC, Christian Köstlin 
wrote:
How can I make sure, that the calculations are done at compile 
time?


If you ever have doubts, you can always use something like this 
to check:


assert (__ctfe);


Sorry, "enforce" would more appropriate if you're really checking.


Re: How to enforce compile time evaluation (and test if it was done at compile time)

2017-02-27 Thread sarn via Digitalmars-d-learn
On Monday, 27 February 2017 at 19:26:06 UTC, Christian Köstlin 
wrote:
How can I make sure, that the calculations are done at compile 
time?


If you ever have doubts, you can always use something like this 
to check:


assert (__ctfe);


Re: getSymbolsByUDA toSymbols error instantiating

2017-02-27 Thread Ali Çehreli via Digitalmars-d-learn

On 02/27/2017 06:26 AM, Oleg B wrote:

Hello. I have this code:

```d
import std.traits;

enum myuda;

class A { @myuda int x; }

class B : A
{
@myuda int some;
void foo() { foreach (s; getSymbolsByUDA!(typeof(this), myuda)) {} }
}

void main() { (new B).foo(); }
```

And have this error:

```
% rdmd uda_symbols.d
/usr/include/dmd/phobos/std/traits.d-mixin-7250(7250): Error: template
instance AliasSeq!(some, x) AliasSeq!(some, x) is nested in both B and A
/usr/include/dmd/phobos/std/traits.d(7259): Error: template instance
std.traits.getSymbolsByUDA!(B, myuda).toSymbols!("some", "x") error
instantiating
uda_symbols.d(10):instantiated from here: getSymbolsByUDA!(B,
myuda)
```

If I understand correctly it's happens because code in std.traits can't
choose between A.x and B.x, but I don't understand why. It's a bug or
it's has a more complex base?


This looks like a bug to me. Please file it at https://issues.dlang.org/

Ali



Re: Parallel foreach over AliasSec?

2017-02-27 Thread Bastiaan Veelo via Digitalmars-d-learn

On Monday, 27 February 2017 at 16:04:00 UTC, Bastiaan Veelo wrote:

I get a bus error some time out in execution.


It could be that I am running out of stack space. I am on OS X, 
and non-main threads are given a very limited stack size, they 
say [1, 2]. This foreach of mine calls into itself, and for my 
test case it nests upto 52 levels deep, which may be too much. 
Core.thread allows threads to be created with specified stack 
size, but that seems to be abstracted away in std.parallelism.


[1] http://stackoverflow.com/a/33805928/2871767
[2] 
https://groups.google.com/a/chromium.org/forum/#!topic/chromium-reviews/DMt5bDdK7s8


Re: How to enforce compile time evaluation (and test if it was done at compile time)

2017-02-27 Thread Dukc via Digitalmars-d-learn
On Monday, 27 February 2017 at 19:26:06 UTC, Christian Köstlin 
wrote:

Is it enough to put up static immutable modifiers?
How can I make sure, that the calculations are done at compile 
time?


...
static immutable time = Unit("time", [Unit.Scale("ms", 1),
...


An initialization of a static variable (or constant, as in this 
case) is indeed always done at compile time. Another option would 
be using an enum storage class. Difference between enum and 
static or shared immutable is that enum IS a compile time 
constant, not just a runtime constant initialized at 
compile-time. That means you can use enums to calculate other 
compile-time stuff.


Template parameters are also calculated at compile time. If they 
take a value, they are enum values, only difference to normal 
enums being that their values are defined at template call site. 
Thus, if you pass an expression to a template argument, you can 
be sure it's calculated at compile time.


How to enforce compile time evaluation (and test if it was done at compile time)

2017-02-27 Thread Christian Köstlin via Digitalmars-d-learn
I have a small example, that can be used to express 3601000ms as 1h 1s
(a much more advanced version has already been done by
https://github.com/nordlow/units-d).

I would like to enforce that the precomputation (multiplying and
inverting the list of scale's) is done at compile time.

Is it enough to put up static immutable modifiers?
How can I make sure, that the calculations are done at compile time?

Thanks in advance,
Christian


public struct Unit {
  import std.algorithm.iteration;
  import std.range;

  public struct Scale {
string name;
long factor;
  }

  public struct Part {
string name;
long v;
string toString() {
  import std.conv;
  return v.to!(string) ~ name;
}
  }

  private string name;
  private Scale[] scales;

  public this(string name, Scale[] scales) {
this.name = name;
this.scales = cumulativeFold!((result,x) => Scale(x.name,
result.factor * x.factor))(scales).array.retro.array;
  }

  public Part[] transform(long v) immutable {
import std.array;

auto res = appender!(Part[]);
auto tmp = v;
foreach (Scale scale; scales) {
  auto h = tmp / scale.factor;
  tmp = v % scale.factor;
  res.put(Part(scale.name, h));
}
return res.data;
  }
}

Unit.Part[] onlyRelevant(Unit.Part[] parts) {
  import std.array;
  auto res = appender!(Unit.Part[]);
  bool needed = false;
  foreach (part; parts) {
if (needed || (part.v > 0)) {
  needed = true;
}
if (needed) {
  res.put(part);
}
  }
  return res.data;
}

Unit.Part[] mostSignificant(Unit.Part[] parts, long nr) {
  import std.algorithm.comparison;
  auto max = min(parts.length, nr);
  return parts[0..max];
}

unittest {
  static immutable time = Unit("time", [Unit.Scale("ms", 1),
Unit.Scale("s", 1000), Unit.Scale("m", 60), Unit.Scale("h", 60),
Unit.Scale("d", 24)]);

  auto res = time.transform(1 + 2*1000 + 3*1000*60 + 4*1000*60*60 + 5 *
1000*60*60*24);
  res.length.shouldEqual(5);
  res[0].name.shouldEqual("d");
  res[0].v.shouldEqual(5);
  res[1].name.shouldEqual("h");
  res[1].v.shouldEqual(4);
  res[2].name.shouldEqual("m");
  res[2].v.shouldEqual(3);
  res[3].name.shouldEqual("s");
  res[3].v.shouldEqual(2);
  res[4].name.shouldEqual("ms");
  res[4].v.shouldEqual(1);

  res = time.transform(2001).onlyRelevant;
  res.length.shouldEqual(2);
  res[0].name.shouldEqual("s");
  res[0].v.shouldEqual(2);
  res[1].name.shouldEqual("ms");
  res[1].v.shouldEqual(1);

  res = time.transform(2001).onlyRelevant.mostSignificant(1);
  res.length.shouldEqual(1);
  res[0].name.shouldEqual("s");
  res[0].v.shouldEqual(2);
}


Re: How to get the name for a Tid

2017-02-27 Thread Jack Stouffer via Digitalmars-d-learn
On Wednesday, 23 November 2016 at 21:04:38 UTC, Christian Köstlin 
wrote:
std.concurrency contains the register function to associate a 
name with
a Tid. This is stored internally in an associative array 
namesByTid.
I see no accessors for this. Is there a way to get to the 
associated

names of a Tid?

Thanks,
Christian


looks like there needs to be a Tid overload of 
std.concurrency.locate


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


Re: Package visibility strange behaviour

2017-02-27 Thread Jonathan M Davis via Digitalmars-d-learn
On Monday, February 27, 2017 14:07:21 Oleg B via Digitalmars-d-learn wrote:
> Hello. Is this behavior normal, or it's a bug? And if it's normal
> why it's normal? I want to use function with `package` visibility
> in same package where it's defined, but I don't.
>
> ```d
> module package_visible;
> package void foo() { }
> void main() { foo(); }
> ```
>
> ```
> % rdmd package_visible.d
> package_visible.d(3): Error: function package_visible.foo is not
> accessible from module package_visible
> Failed: ["dmd", "-v", "-o-", "package_visible.d", "-I."]
> ```
>
> dmd version v2.073.1

In your example, your module isn't in a package. It's at the top-level. So,
package doesn't work, becasue there's no package. In this case, you'd just
use private. Presumably, in any actual code, you would have a package rather
than putting the module at the top level, and then it would work.

- Jonathan M Davis



Re: Parallel foreach over AliasSec?

2017-02-27 Thread Bastiaan Veelo via Digitalmars-d-learn

On Monday, 27 February 2017 at 11:53:09 UTC, ag0aep6g wrote:
You can generate wrapper functions that have no overloads:



static int wrap(alias f)(int arg) { return f(arg); }
enum addrOf(alias f) = 
enum fptrs = staticMap!(addrOf, staticMap!(wrap, funcs));
/* ... r and foreach as before ... */



I'm in awe. 


[...] the template stuff just seems to add complexity.


Yes, but the template is one of my constraints (no pun). It needs 
to happen in there.


This compiles when I apply this to the Pegged source, but 
something else is wrong. I get a bus error some time out in 
execution. Maybe when tasks are garbage collected? Or because of 
missing synchronisation on the array that the tasks write into? 
This is a complicated situation, because the evaluation of these 
functions may themselves cause a parallel foreach on a different 
set of functions (or the same set, for recursive rules). I might 
not be able to solve this, sadly -- a parser that does parallel 
matching would have been so cool.


Anyway I am glad to have seen powers of meta programming that I 
didn't know were possible.


Bastiaan.


getSymbolsByUDA toSymbols error instantiating

2017-02-27 Thread Oleg B via Digitalmars-d-learn

Hello. I have this code:

```d
import std.traits;

enum myuda;

class A { @myuda int x; }

class B : A
{
@myuda int some;
void foo() { foreach (s; getSymbolsByUDA!(typeof(this), 
myuda)) {} }

}

void main() { (new B).foo(); }
```

And have this error:

```
% rdmd uda_symbols.d
/usr/include/dmd/phobos/std/traits.d-mixin-7250(7250): Error: 
template instance AliasSeq!(some, x) AliasSeq!(some, x) is nested 
in both B and A
/usr/include/dmd/phobos/std/traits.d(7259): Error: template 
instance std.traits.getSymbolsByUDA!(B, myuda).toSymbols!("some", 
"x") error instantiating
uda_symbols.d(10):instantiated from here: 
getSymbolsByUDA!(B, myuda)

```

If I understand correctly it's happens because code in std.traits 
can't choose between A.x and B.x, but I don't understand why. 
It's a bug or it's has a more complex base?


Package visibility strange behaviour

2017-02-27 Thread Oleg B via Digitalmars-d-learn
Hello. Is this behavior normal, or it's a bug? And if it's normal 
why it's normal? I want to use function with `package` visibility 
in same package where it's defined, but I don't.


```d
module package_visible;
package void foo() { }
void main() { foo(); }
```

```
% rdmd package_visible.d
package_visible.d(3): Error: function package_visible.foo is not 
accessible from module package_visible

Failed: ["dmd", "-v", "-o-", "package_visible.d", "-I."]
```

dmd version v2.073.1


Re: Parallel foreach over AliasSec?

2017-02-27 Thread ag0aep6g via Digitalmars-d-learn

On 02/27/2017 10:52 AM, Bastiaan Veelo wrote:

On Monday, 27 February 2017 at 02:02:57 UTC, ag0aep6g wrote:

[...]

enum fptr(alias f) = 

(This is still a bit magical to me: it this a shorthand for a template?)


Yes, it's short for this:

template fptr(alias f) { enum fptr =  }

"addrOf" is probably a better name for this. It's not restricted to 
functions.



Can the following be made to work?

int one(int) {return 1;}

[...]

int one(string) {return 0;} // How to ignore this?

int[8] values;

template eval_all(funcs...)
{
void eval_all(int val)
{

[...]

//enum fptr(alias f) =   // Error: cannot infer type
from
// overloaded function
symbol & one
enum fptr(alias int f(int)) =// ditto.


Aside: That funky, C-like syntax surprised me. I guess that's a function 
type as opposed to a function pointer type, which would be `alias int 
function(int) f`. That distinction always trips me up.



enum fptrs = staticMap!(fptr, funcs);
auto r = only(fptrs);

foreach (i, f; parallel(r))
values[i] = f(val);
}
}


You can generate wrapper functions that have no overloads:


static int wrap(alias f)(int arg) { return f(arg); }
enum addrOf(alias f) = 
enum fptrs = staticMap!(addrOf, staticMap!(wrap, funcs));
/* ... r and foreach as before ... */


This also unifies the signatures in other ways. For example, you can 
have a function that takes a `long` instead of an int.


Of course, if you passed the functions at run time, and not in a 
template parameter, the code would be much shorter:



void eval_all(int val, int function(int)[] funcs ...)
{
import std.parallelism;

foreach (i, f; parallel(funcs))
values[i] = f(val);
}
void main()
{
eval_all(42, , , , , , , ,
);
foreach(i, val; values)
assert(val == i + 1);
}


One little disadvantage of this is that the signatures have to match 
exactly. Overloads are fine, but you can't have a function with a `long` 
parameter. But that's really minor, and can be handled at the call site.


I think I'd prefer this over the template version. You have to make a 
run-time list of the functions anyway, for `parallel`, so the template 
stuff just seems to add complexity.


Re: Getting nice print of struct for debugging

2017-02-27 Thread Martin Tschierschke via Digitalmars-d-learn

On Saturday, 25 February 2017 at 01:30:09 UTC, Minty Fresh wrote:
On Saturday, 25 February 2017 at 01:27:09 UTC, Minty Fresh 
wrote:
On Wednesday, 22 February 2017 at 11:18:15 UTC, Martin 
Tschierschke wrote:

[...]


Since structs are Plain-old Data and don't do inheritance, the 
best option is a template mixin.


ie.

  template mixin PrettyPrint
  {
  string toString()
  {
  // . . .
  }
  }

From there, you can mix it into any struct you want.

  struct MyStruct
  {
  mixin PrettyPrint;
  }

If you're familiar with Rails, this is similar to a Concern.


Errata on that. Should actually be declared as:

  mixin template PrettyPrint()

This is why I shouldn't make posts from my phone.


Thank you, but this solution from Kevin Brogan, is an good 
alternative,
to add a special dump function globally, so no need to modify the 
struct definitions.


https://forum.dlang.org/post/yewavntuyutdvejwj...@forum.dlang.org

His solution:

import std.traits;

void main()
{
WSADATA wsa;
dump!wsa;
}

void dump(alias variable)()
{
writeln("\nDumping ",typeid(typeof(variable)),":\n");
writeln(variable.stringof, " = \n{");
foreach(member; FieldNameTuple!(typeof(variable)))
{
writeln("\t", member, ": ", mixin("variable."~member) );
}
writeln("}\n");
}





Re: Parallel foreach over AliasSec?

2017-02-27 Thread Bastiaan Veelo via Digitalmars-d-learn

On Monday, 27 February 2017 at 02:02:57 UTC, ag0aep6g wrote:
Make a range or an array of function pointers from the AliasSeq 
of function aliases:



import std.meta: staticMap;
import std.range: only;

enum fptr(alias f) = 
enum fptrs = staticMap!(fptr, funcs);
auto r = only(fptrs);

foreach (i, f; parallel(r))
values[i] = f(val);



Although this answers my question perfectly, it turns out that I 
have simplified my case too much. It looks like existing 
overloads are complicating the matter. (I am actually trying to 
parallelise 
https://github.com/PhilippeSigaud/Pegged/blob/master/pegged/peg.d#L1646.)


The problem seems to be

enum fptr(alias f) = 
(This is still a bit magical to me: it this a shorthand for a 
template?)


Can the following be made to work?

int one(int) {return 1;}
int two(int) {return 2;}
int three(int) {return 3;}
int four(int) {return 4;}
int five(int) {return 5;}
int six(int) {return 6;}
int seven(int) {return 7;}
int eight(int) {return 8;}

int one(string) {return 0;} // How to ignore this?

int[8] values;

template eval_all(funcs...)
{
void eval_all(int val)
{
import std.meta: staticMap, Filter;
import std.range: only;
import std.parallelism;
import std.traits;

alias int function(int) iwant;
//enum fptr(alias f) =   // Error: cannot 
infer type from
// overloaded 
function symbol & one

enum fptr(alias int f(int)) =// ditto.
enum fptrs = staticMap!(fptr, funcs);
auto r = only(fptrs);

foreach (i, f; parallel(r))
values[i] = f(val);
}
}

void main()
{
eval_all!(one, two, three, four, five, six, seven, eight)(42);
foreach(i, val; values)
assert(val == i + 1);
}


Re: How to get the name for a Tid

2017-02-27 Thread Oleg B via Digitalmars-d-learn
On Wednesday, 23 November 2016 at 21:04:38 UTC, Christian Köstlin 
wrote:
std.concurrency contains the register function to associate a 
name with
a Tid. This is stored internally in an associative array 
namesByTid.
I see no accessors for this. Is there a way to get to the 
associated

names of a Tid?

Thanks,
Christian


I have a same problem and I use workaround with manualy register 
threads and my own list of threads names. I think it's a little 
mistake and can be changed in dmd updates in the near future.


Re: code D'ish enough? - ignore previous post with same subject

2017-02-27 Thread Thorstein via Digitalmars-d-learn

On Sunday, 26 February 2017 at 21:50:38 UTC, Jordan Wilson wrote:
auto readNumMatCsv2 (string filePath, string ndv, string 
new_ndv){

double[][] p_numArray;
try {
auto lines = File(filePath,"r").byLine;
lines.popFront; // get read of header
p_numArray = lines.map!(a => a.replace (ndv,new_ndv)
  .splitter (",")
  .map!(a => a.to!double)
  .array)
  .array;
} catch (Exception e){
e.msg.writeln; // this replaces "Could not read file. 
Quit here."

}
return p_numArray;
}

It took me quite a while to get the whole usage of range stuff 
like "map" and "filter" etc., but I think it's worth the effort.


This looks like more friendly readable compact code, where should 
be the goal. Thanks for your insights!




Re: code D'ish enough? - ignore previous post with same subject

2017-02-27 Thread Thorstein via Digitalmars-d-learn


I really appriciate your comments and thoughts!

On Sunday, 26 February 2017 at 21:02:52 UTC, ag0aep6g wrote:

On Sunday, 26 February 2017 at 19:34:33 UTC, thorstein wrote:



* "no-data-value"?


No-data-values in data sets are common at least in geosciences: 
raster images, spatial simulation outputs etc. Not all cells have 
evaluable information and need to be filtered by a specific very 
high or very low numeric value. And different software may 
require different no-data-values.



* It's not obvious to me what "Mat" means in "readNumMatCsv".


means 'read numeric matrix from csv-like files'