Re: How to filter an array so the result is an array again?
On Friday, September 16, 2011 04:04:39 Cheng Wei wrote: > The standard library std.algorithm is based on Range. So if > a = [1, 2, 3, 4]; > auto r = filter!("a < 2")(a); > > Here, r is a range. > How about I want an new array? Is there any easy way to convert the > result to array? > > If we have to do like: > int[] b; > for (v; r) { > b ~= v; > } > > Then maybe it is easier to not use filter at all as: > int [] b; > for (v; a) { >if (v < 2) b ~= v; > } Use std.array.array. auto a = [1, 2, 3, 4]; auto r = array(filter!"a < 2"(a)); - Jonathan M Davis
Re: How to filter an array so the result is an array again?
Sorry, the 'for' should be 'foreach'.
How to filter an array so the result is an array again?
The standard library std.algorithm is based on Range. So if a = [1, 2, 3, 4]; auto r = filter!("a < 2")(a); Here, r is a range. How about I want an new array? Is there any easy way to convert the result to array? If we have to do like: int[] b; for (v; r) { b ~= v; } Then maybe it is easier to not use filter at all as: int [] b; for (v; a) { if (v < 2) b ~= v; } Thanks a lot.
Re: Is it possible to check if a type is an instance of a template?
On Thursday, September 15, 2011 14:17 Andrej Mitrovic wrote: > On 9/15/11, Jonathan M Davis wrote: > > Every template instantiation is a new set of code with _zero_ assocation > > with > > any other template instantation. > > Yeah, I know. But I don't think this has to be set in stone. Having > some specific compile-time type information about a template instance > would be cool. True. I think that it could be made possible to get traits information on whether something is an instantation of a particular template without changing how templates work in general, but it just doesn't work that way right now. - Jonathan M Davis
Re: Is it possible to check if a type is an instance of a template?
On Thu, 15 Sep 2011 22:45:21 +0200, Andrej Mitrovic wrote: Cool, that works, thanks. Is this in Phobos by any chance? Otherwise I'll just use a more flexible version of that. No more flexible version available, sadly. I wish this worked (I think it's in Bugzilla somewhere): template IsA( alias F, T ) { static if ( is( T t == F!U, U ) ) { enum IsA = true; } else { enum IsA = false; } } -- Simen
Re: Is it possible to check if a type is an instance of a template?
On 9/15/11, Jonathan M Davis wrote: > Every template instantiation is a new set of code with _zero_ assocation > with > any other template instantation. Yeah, I know. But I don't think this has to be set in stone. Having some specific compile-time type information about a template instance would be cool.
Re: std.datetime impenetrable
On 09/15/2011 07:48 PM, Steve Teale wrote: Alternatively, let me explain my desire. When my program first runs, I want to hazard a guess as to what size of paper the user is likely to use - US Letter Size, or A4/inches or metric. GTK does not seem to want to tell me about the default printer settings - could be the beer of course. To get the default paper size with gtkD you could use: new PaperSize(cast(string)null); This creates an PaperSize object representing an US Letter if the locale is set to Canada, US or Mexico, and A4 otherwise. -- Mike Wey
Re: Is it possible to check if a type is an instance of a template?
On Thursday, September 15, 2011 13:45 Andrej Mitrovic wrote: > Cool, that works, thanks. Is this in Phobos by any chance? How could it be? It's specific to the template that you're testing. - Jonathan M Davis
Re: Is it possible to check if a type is an instance of a template?
On 9/15/11, Timon Gehr wrote: > template bar(T : Foo!S,S){ } Yeah, that should do it too. Thanks.
Re: Is it possible to check if a type is an instance of a template?
On Thursday, September 15, 2011 13:24 Andrej Mitrovic wrote: > I can do this: > > struct Foo(T) { } > template bar(T : Foo!int) { } > > I can check if T is a specific instantiation of Foo. But I want to > check whether T is *any* instantiation of Foo. Is this possible to do? > > Otherwise I'm currently having to hardcode via: > > template bar(T) if (isOneOf!(T, Foo!int, Foo!double)) { } > > But having to list all possible instantiations doesn't really scale too > well. Every template instantiation is a new set of code with _zero_ assocation with any other template instantation. Foo!int and Foo!float might as well be FooInt and FooFloat for how related they are. So no, there is no easy way to check whether a type is an instantation of a particular template. If you want a more scalable way to test it though, then you can add something to Foo that you can test for. For instance, it could have a static isFoo function. It wouldn't have to do anything - just exist - and you might even be able to make it private. - Jonathan M Davis
Re: Is it possible to check if a type is an instance of a template?
Cool, that works, thanks. Is this in Phobos by any chance? Otherwise I'll just use a more flexible version of that.
Re: Is it possible to check if a type is an instance of a template?
On 09/15/2011 10:24 PM, Andrej Mitrovic wrote: I can do this: struct Foo(T) { } template bar(T : Foo!int) { } I can check if T is a specific instantiation of Foo. But I want to check whether T is *any* instantiation of Foo. Is this possible to do? Otherwise I'm currently having to hardcode via: template bar(T) if (isOneOf!(T, Foo!int, Foo!double)) { } But having to list all possible instantiations doesn't really scale too well. template bar(T : Foo!S,S){ } S will be bound to the type that was used to instantiate Foo. It won't work if Foo takes a variable number of parameters though. Probably a bug.
Re: Is it possible to check if a type is an instance of a template?
On Thu, 15 Sep 2011 22:24:50 +0200, Andrej Mitrovic wrote: I can do this: struct Foo(T) { } template bar(T : Foo!int) { } I can check if T is a specific instantiation of Foo. But I want to check whether T is *any* instantiation of Foo. Is this possible to do? Otherwise I'm currently having to hardcode via: template bar(T) if (isOneOf!(T, Foo!int, Foo!double)) { } But having to list all possible instantiations doesn't really scale too well. static if can do this: template IsAFoo(T) { static if (is(T t == Foo!U, U)) { enum IsAFoo = true; } else { enum IsAFoo = false; } } -- Simen
Is it possible to check if a type is an instance of a template?
I can do this: struct Foo(T) { } template bar(T : Foo!int) { } I can check if T is a specific instantiation of Foo. But I want to check whether T is *any* instantiation of Foo. Is this possible to do? Otherwise I'm currently having to hardcode via: template bar(T) if (isOneOf!(T, Foo!int, Foo!double)) { } But having to list all possible instantiations doesn't really scale too well.
Re: std.datetime impenetrable
On Thursday, September 15, 2011 19:30:24 Steve Teale wrote: > Oh, so it is difficult after all! I thought it was just me. No. It's hard. The best that you can get by asking Posix functions is the std and DST abbreviations for the current time zone - and those are non-unique. You'd have to do some crazy stuff like trying to find the time zone file for the current time zone on disk (whose location is usually something like /etc/localtime or /etc/timezone but is not entirely standard) and then compare it with every other time zone file to figure out which one it is. It's ugly. I do hope to come up with a reasonable way to do it at some point (you _should_ be able to do it after all), but I haven't yet. It would require a fair bit of research to figure out what's standard enough to rely on and what isn't. So, yeah. It's one of the many reasons that dealing with time sucks. Though hopefully std.datetime manages to make most of it fairly easy and pleasant to deal with. > So I am probably going to have to ask. Then the interesting question will be > which way around will offend fewest people. > > I have installed it as ISO, then have to ask US users if they would prefer > Letter Size, or the other way round then all those Euro and Indian users > say 'typical American programmer!'. > > Thanks for the rundown anyway. No problem. std.datetime is actually pretty easy to use, but it's large enough that it can be a bit overwhelming. - Jonathan M Davis
Re: std.datetime impenetrable
Oh, so it is difficult after all! I thought it was just me. So I am probably going to have to ask. Then the interesting question will be which way around will offend fewest people. I have installed it as ISO, then have to ask US users if they would prefer Letter Size, or the other way round then all those Euro and Indian users say 'typical American programmer!'. Thanks for the rundown anyway. Steve
Re: std.datetime impenetrable
On Thursday, September 15, 2011 10:48 Steve Teale wrote: > Looking at the documentation makes my head hurt, especially if I have > consumed some beer, when I am not pure and immutable. For an overview, check out http://d-programming-language.org/intro-to- datetime.html I'd _love_ to fix the links on the top of the page, but ddoc needs some improvements with regards to how it generates anchor names for that to be possible (anchors are based solely on function or type names with no concept of scaping, so every function with the same name ends up with the same anchor regardless of whether it's a free function or what it's a member function of). > Can anyone help me to understand how to determine what timezone the user > has selected to be his/hers. > > Alternatively, let me explain my desire. When my program first runs, I want > to hazard a guess as to what size of paper the user is likely to use - US > Letter Size, or A4/inches or metric. GTK does not seem to want to tell me > about the default printer settings - could be the beer of course. > > The timezone might allow me to bracket the USA, Canada, and Mexico though > of course then the poor sods in South America and the Philippines and so > on would be SOL. > > Anyway, how would you do it? Determining the exact time zone that the computer is in is really hard. It's actually pretty easy to do in Windows, but on Posix... not so much. So, there's no function in std.datetime to do it. If I can ever figure out how to sanely do it on Posix, then I'll add it, but until then, it's not there. If all you want to do is bracket it, then you can just use the UTC offset for the time zone. Because the UTC offset varies depending on the time of year (or even from year to year), you can't just ask a TimeZone for its UTC offset, but I do have a pull request which makes it possible to ask what the UTC offset is at a particular std time or to ask a SysTime what its UTC offset is - but that hasn't been released yet. So, if you wanted to find out what the offset was right now in the current time zone, you would do auto sysTime = Clock.currTime(); auto utcOffset = dur!"hnsecs"(sysTime.timezone.utcToTZ(sysTime.stdTime) - sysTime.stdTime); So, for instance, America/Los_Angeles would give you an offset of -7 hours right now and -8 during the Winter. If you then wanted to narrow the hemisphere, you can do it in _some_ cases by checking whether DST is in effect in July: auto north = SysTime(Date(2011, 7, 1)).dstInEffect; but that will only work if the timezone has DST (and a large chunk of the Southern Hemisphere does not). If you were just trying to bracket USA, Canada, and Mexico though (all of which mostly have DST), it would mostly work to do something like auto jan = SysTime(Date(2011, 1, 11)); auto jul = SysTime(Date(2011, 7, 11)); auto utcOffset = dur!"hnsecs"(jan.timezone.utcToTZ(jan.stdTime) - jan.stdTime); auto inBracket = jul.dstInEffect && utcOffset >= dur!"hours"(-10) && utcOffset <= dur!"hours"(-3) + dur!"minutes"(-30); But that will fail for Hawaii or any other northern time zone with no DST, and it'll probably take in more time zones than you want it to above the equator, but it would _mostly_ work. But honestly, even if std.datetime _could_ tell you what the exact time zone was, that would still be a bit of a pain. You'd have to get the exact list of time zones that you cared about and compare that against the current time zone. It _would_ be mork accurate though. Maybe I'll figure out a reliable way to get the current time zone on Posix at some point, but unfortunately, it's not something that Posix is set up to do very easily (it's time zone handling is _much_ more accurate than Windows though, so neither OS really does it entirely right). Of course, what you really want to do if you can't just ask what the default printer settings are is to do it based on locale, but I don't know how straightforward that is. It might just be easier to use the hack that you're trying to do. And the code that I gave above should do that for you. - Jonathan M Davis
Re: port c macro to D
Also by static function you probably mean private in D. No, I meant for mixin I nead static functions, but I just realized, it also works without the static keyword. I think static has no meaning in global scope, only in function scope where it tells the compiler you don't want a closure.
std.datetime impenetrable
Looking at the documentation makes my head hurt, especially if I have consumed some beer, when I am not pure and immutable. Can anyone help me to understand how to determine what timezone the user has selected to be his/hers. Alternatively, let me explain my desire. When my program first runs, I want to hazard a guess as to what size of paper the user is likely to use - US Letter Size, or A4/inches or metric. GTK does not seem to want to tell me about the default printer settings - could be the beer of course. The timezone might allow me to bracket the USA, Canada, and Mexico though of course then the poor sods in South America and the Philippines and so on would be SOL. Anyway, how would you do it? B Steve
Re: Why can't templates with default arguments be instantiated without the bang syntax?
On Thu, 15 Sep 2011 17:54:19 +0200, Christophe wrote: "Simen Kjaeraas" , dans le message (digitalmars.D.learn:29539), a écrit : On Thu, 15 Sep 2011 16:46:24 +0200, Andrej Mitrovic wrote: struct Foo(T = int) {} void main() { Foo foo; // fail Foo!() bar; // ok } It would be very convenient to be able to default to one type like this. For example, in CairoD there's a Point structure which takes doubles as its storage type, and then there's PointInt that takes ints. The reason they're not both a template Point() that takes a type argument is because in most cases the user will use the Point structure with doubles, and only in rare cases Point with ints. So to simplify code one doesn't have to write Point!double in all of their code, but simply Point. If the bang syntax wasn't required in presence of default arguments then these workarounds wouldn't be needed. How would you then pass a single-argument template as a template alias parameter? Example: template Foo( ) { template Bar( ) { } } template Baz(alias A) { mixin A!(); } void main( ) { mixin Baz!Foo; } Does this mixin Foo or Bar to main's scope? I don't get the problem. Maybe I am not used to mixin enough. Can you mixin normal templates, and not only mixin templates ? Anyway, why would this mixin Bar ? As I understand the proposition, only "mixin Baz!(Foo.Bar);" and of course "mixin Baz!(Foo!().Bar)" should mixin Bar. Sorry, you're right. I meant: template Foo( ) { template Foo( ) { } } -- Simen
Re: Why can't templates with default arguments be instantiated without the bang syntax?
On 2011-09-15 16:46, Andrej Mitrovic wrote: struct Foo(T = int) {} void main() { Foo foo; // fail Foo!() bar; // ok } It would be very convenient to be able to default to one type like this. For example, in CairoD there's a Point structure which takes doubles as its storage type, and then there's PointInt that takes ints. The reason they're not both a template Point() that takes a type argument is because in most cases the user will use the Point structure with doubles, and only in rare cases Point with ints. So to simplify code one doesn't have to write Point!double in all of their code, but simply Point. If the bang syntax wasn't required in presence of default arguments then these workarounds wouldn't be needed. I've wondered the same thing, why this doesn't work: template Foo (T = int) {} mixin Foo; But this works: template Foo () {} mixin Foo; -- /Jacob Carlborg
Re: Why can't templates with default arguments be instantiated without the bang syntax?
On Thu, 15 Sep 2011 10:46:24 -0400, Andrej Mitrovic wrote: struct Foo(T = int) {} void main() { Foo foo; // fail Foo!() bar; // ok } It would be very convenient to be able to default to one type like this. For example, in CairoD there's a Point structure which takes doubles as its storage type, and then there's PointInt that takes ints. The reason they're not both a template Point() that takes a type argument is because in most cases the user will use the Point structure with doubles, and only in rare cases Point with ints. So to simplify code one doesn't have to write Point!double in all of their code, but simply Point. If the bang syntax wasn't required in presence of default arguments then these workarounds wouldn't be needed. Perhaps a different approach: struct PointT(T) {...} alias PointT!(double) Point; // and if so desired: alias PointT!int PointInt; Just a thought... -Steve
Re: Why can't templates with default arguments be instantiated without the bang syntax?
On Thursday, September 15, 2011 16:46:24 Andrej Mitrovic wrote: > struct Foo(T = int) {} > > void main() > { > Foo foo; // fail > Foo!() bar; // ok > } > > It would be very convenient to be able to default to one type like this. > > For example, in CairoD there's a Point structure which takes doubles > as its storage type, and then there's PointInt that takes ints. The > reason they're not both a template Point() that takes a type argument > is because in most cases the user will use the Point structure with > doubles, and only in rare cases Point with ints. So to simplify code > one doesn't have to write Point!double in all of their code, but > simply Point. > > If the bang syntax wasn't required in presence of default arguments > then these workarounds wouldn't be needed. There is no type inference for templated structs, so you need the bang. Only functions get type inference. They way to fix this is to create a separate factory function to construct the type. std.container does this with redBlackTree for RedBlackTree. - Jonathan M Davis
Re: Why can't templates with default arguments be instantiated without the bang syntax?
"Simen Kjaeraas" , dans le message (digitalmars.D.learn:29539), a écrit : > On Thu, 15 Sep 2011 16:46:24 +0200, Andrej Mitrovic > wrote: > >> struct Foo(T = int) {} >> >> void main() >> { >> Foo foo; // fail >> Foo!() bar; // ok >> } >> >> It would be very convenient to be able to default to one type like this. >> >> For example, in CairoD there's a Point structure which takes doubles >> as its storage type, and then there's PointInt that takes ints. The >> reason they're not both a template Point() that takes a type argument >> is because in most cases the user will use the Point structure with >> doubles, and only in rare cases Point with ints. So to simplify code >> one doesn't have to write Point!double in all of their code, but >> simply Point. >> >> If the bang syntax wasn't required in presence of default arguments >> then these workarounds wouldn't be needed. > > How would you then pass a single-argument template as a template alias > parameter? > > Example: > > template Foo( ) { > template Bar( ) { > } > } > > template Baz(alias A) { > mixin A!(); > } > > void main( ) { > mixin Baz!Foo; > } > > Does this mixin Foo or Bar to main's scope? I don't get the problem. Maybe I am not used to mixin enough. Can you mixin normal templates, and not only mixin templates ? Anyway, why would this mixin Bar ? As I understand the proposition, only "mixin Baz!(Foo.Bar);" and of course "mixin Baz!(Foo!().Bar)" should mixin Bar. -- Christophe
Re: Why can't templates with default arguments be instantiated without the bang syntax?
On Thu, 15 Sep 2011 16:46:24 +0200, Andrej Mitrovic wrote: struct Foo(T = int) {} void main() { Foo foo; // fail Foo!() bar; // ok } It would be very convenient to be able to default to one type like this. For example, in CairoD there's a Point structure which takes doubles as its storage type, and then there's PointInt that takes ints. The reason they're not both a template Point() that takes a type argument is because in most cases the user will use the Point structure with doubles, and only in rare cases Point with ints. So to simplify code one doesn't have to write Point!double in all of their code, but simply Point. If the bang syntax wasn't required in presence of default arguments then these workarounds wouldn't be needed. How would you then pass a single-argument template as a template alias parameter? Example: template Foo( ) { template Bar( ) { } } template Baz(alias A) { mixin A!(); } void main( ) { mixin Baz!Foo; } Does this mixin Foo or Bar to main's scope? -- Simen
Why can't templates with default arguments be instantiated without the bang syntax?
struct Foo(T = int) {} void main() { Foo foo; // fail Foo!() bar; // ok } It would be very convenient to be able to default to one type like this. For example, in CairoD there's a Point structure which takes doubles as its storage type, and then there's PointInt that takes ints. The reason they're not both a template Point() that takes a type argument is because in most cases the user will use the Point structure with doubles, and only in rare cases Point with ints. So to simplify code one doesn't have to write Point!double in all of their code, but simply Point. If the bang syntax wasn't required in presence of default arguments then these workarounds wouldn't be needed.
Re: port c macro to D
On 15.09.2011 13:48, Trass3r wrote: If you are willing to write V(+1,-1,+1) instead you can just turn them into functions. really a good point! :) Also by static function you probably mean private in D. No, I meant for mixin I nead static functions, but I just realized, it also works without the static keyword.
Re: port c macro to D
Am 15.09.2011, 13:37 Uhr, schrieb Matthias Pleh : When porting c-code to D, I come consitently to the problem, how to convert such a c-macro: #define V(a,b,c) glVertex3d( a size, b size, c size ); #define N(a,b,c) glNormal3d( a, b, c ); N( 1.0, 0.0, 0.0); V(+,-,+); V(+,-,-); V(+,+,-); V(+,+,+); ... Ok, I could just write it out, but that's not the case. Sometimes I just want to keep it small and clear as in this example. I've started with some static functions and use it in mixins: static string V(string a, string b, string c) { return "glVertex3d("~a~" size, "~b~" size, "~c~" size)"; } static string N(string a, string b, string c) { return "glNormal3d("~a~","~b~","~c~");"; } If you are willing to write V(+1,-1,+1) instead you can just turn them into functions. Also by static function you probably mean private in D.
port c macro to D
When porting c-code to D, I come consitently to the problem, how to convert such a c-macro: #define V(a,b,c) glVertex3d( a size, b size, c size ); #define N(a,b,c) glNormal3d( a, b, c ); N( 1.0, 0.0, 0.0); V(+,-,+); V(+,-,-); V(+,+,-); V(+,+,+); ... Ok, I could just write it out, but that's not the case. Sometimes I just want to keep it small and clear as in this example. I've started with some static functions and use it in mixins: static string V(string a, string b, string c) { return "glVertex3d("~a~" size, "~b~" size, "~c~" size)"; } static string N(string a, string b, string c) { return "glNormal3d("~a~","~b~","~c~");"; } But I wonder, if there is another clean solution for this? Any regards. Matthias
Re: Should the 2.054 feature about warning on implicit fallthrough
bearophile , dans le message (digitalmars.D.learn:29532), a écrit : > Well, I don't understand the error it gives :-) Are you able to explain it to > me? > > > import std.stdio; > void main() { > int i = 1; > switch(i) { > case 0: > writeln("case 0"); > goto default; // needed here > aLabel: > writeln("a label"); > default: > writeln("default"); > // But always falls through here > } > } > > test.d(10): Error: switch case fallthrough - use 'goto default;' if intended If there is a goto aLabel somewhere, the program could go to line 9. Then it should fall to the default statement, but the compiler asks for an explicit fallthrough, since default is not a simple label, but something like a case statement. -- Christophe