How to mixin each element of tuple
My naive approach doesn't works struct Item1 (T) {} struct Item2 (T) {} struct Group (Items ...) { // how to do this? ... no static foreach :( static foreach (I; Items) mixin I!(int); } void main () { alias Group!(Item1, Item2) G; }
Re: newbie question: Can D do this?
Thank you for your quick replies. I'm impressed by the helpfulness and dedication of the D community! Here's another one. Is there a way to pass arguments to functions by keyword as in the calls to f and g below? void f(int a = 0, int b = 1) {} void g(int a) {} void main() { f(b = 1, a = 0); // compile error g(a = 0); // also compile error } On 12/19/2011 03:00 PM, digitalmars-d-learn-requ...@puremagic.com wrote: Send Digitalmars-d-learn mailing list submissions to digitalmars-d-learn@puremagic.com To subscribe or unsubscribe via the World Wide Web, visit http://lists.puremagic.com/cgi-bin/mailman/listinfo/digitalmars-d-learn or, via email, send a message with subject or body 'help' to digitalmars-d-learn-requ...@puremagic.com You can reach the person managing the list at digitalmars-d-learn-ow...@puremagic.com When replying, please edit your Subject line so it is more specific than Re: Contents of Digitalmars-d-learn digest... Today's Topics: 1. Re: newbie question: Can D do this? (Ali ?ehreli) 2. Re: newbie question: Can D do this? (Kai Meyer) 3. Re: newbie question: Can D do this? (Simen Kj?r?s) 4. Re: newbie question: Can D do this? (Ali ?ehreli) 5. Re: newbie question: Can D do this? (Jonathan M Davis) -- Message: 1 Date: Mon, 19 Dec 2011 10:41:29 -0800 From: Ali ?ehreliacehr...@yahoo.com To: digitalmars-d-learn@puremagic.com Subject: Re: newbie question: Can D do this? Message-ID:jco0gq$1ilt$1...@digitalmars.com Content-Type: text/plain; charset=UTF-8; format=flowed On 12/19/2011 08:17 AM, clk wrote: I'm a little bit intimidated by the fact that the topics in the d-learn list look rather advanced to a newbie like me. We need more newbie topics here! :) 1) Does D support something like the javascript 1.8 destructuring assigment (multiple assigment in python): [a, b] = [b, a]; No multiple assignment like that. But useful approarches exist for most needs, like the swap that simendsjo has shown. 2) D doesn't seem to support the list comprehension syntax available in python and javascript. Is this correct? [f(x) for x in list if condition] List comprehension is not part of the language. import std.algorithm; void f(int x) {} bool condition(int x) { return true; } void main() { auto list = [ 0, 1, 2 ]; map!f(filter!condition(list)); } You can define f and condition within the body of main(). It is possible to use function literals as well: import std.algorithm; void main() { auto list = [ 0, 1, 2 ]; map!((x){ /* ... this is f(x) ...*/ })(filter!((x) { return true; /* ... condition ... */ })(list)); } 3) D's slice operator apparently doesn't allow the use of a stride other than unity as is allowed with fortran and matlab. Is there a way to implement this feature so that [1, 2, 3, 4, 5][0..$:2] would refer to [1, 3, 5], etc..., where 2 is the non unit stride. Or is the find function from std.algorithm the only option to achieve the same behavior. std.range.stride does that: import std.range; // ... stride([1, 2, 3, 4, 5], 2) I find the 3 features above extremely convenient in every day coding. Thanks, -clk Ali -- Message: 2 Date: Mon, 19 Dec 2011 11:50:33 -0700 From: Kai Meyerk...@unixlords.com To: digitalmars-d-learn@puremagic.com Subject: Re: newbie question: Can D do this? Message-ID:jco11q$1lle$1...@digitalmars.com Content-Type: text/plain; charset=ISO-8859-1; format=flowed On 12/19/2011 09:17 AM, clk wrote: Hello, I'm new to this mailing list. I'm trying to learn D to eventually use it in production code. I'm a little bit intimidated by the fact that the topics in the d-learn list look rather advanced to a newbie like me. I have 3 fairly simple questions: 1) Does D support something like the javascript 1.8 destructuring assigment (multiple assigment in python): [a, b] = [b, a]; I would love multiple assignment like this, but it's tricky. But your usage isn't really multiple assignment as much as it is a swap. What I'd love is something like this: [a, b, c] = [get_a(), get_b(), get_c()]; Or [a, b, c] = [to!(int)(argv[1]), some_other_value, argv[4]); 2) D doesn't seem to support the list comprehension syntax available in python and javascript. Is this correct? [f(x) for x in list if condition] No, D's syntax is very C-ish. I don't expect syntax like this to ever show up (though what you are doing is possible with things like std.algorithm) 3) D's slice operator apparently doesn't allow the use of a stride other than unity as is allowed with fortran and matlab. Is there a way to implement this feature so that [1, 2, 3, 4, 5][0..$:2] would refer to [1, 3, 5], etc..., where 2 is the non unit stride. Or is the find
Re: Void initialization
On 19/12/2011 18:11, Steven Schveighoffer wrote: On Mon, 19 Dec 2011 12:24:18 -0500, Bear joanylepri...@yahoo.fr wrote: gc.malloc actually returns void[] http://www.d-programming-language.org/phobos/core_memory.html#malloc Looks like void* to me... Or is there another function I'm not aware of? I think it should be GC.malloc, not gc.malloc, so maybe I'm missing something... snip You are. That the OP was talking about std.gc.malloc in D1, not the core.memory stuff in D2. Stewart.
Re: Void initialization
On 19/12/2011 12:12, bearophile wrote: Bear: snip float[] f = cast(float[])std.gc.malloc(x*4); Try something like this (untested): alias float TF; TF[] f = (cast(TF*)std.gc.malloc(x * TF.sizeof))[0 .. x]; snip I fail to see any real difference from the OP's code: - Why the alias? - std.gc.malloc returns the array with correct length according to my quick test, so the use of [0..x] is redundant - using TF.sizeof instead of 4 might fix things if on the user's platform float isn't 4 bytes long. Otherwise, while using .sizeof instead of a hard-coded number is better practice, it isn't going to get rid of an AV. But knowing what platform the OP is using and having a complete testcase for the AVs/ABEs might help to understand what is going on. Stewart.
Re: How to mixin each element of tuple
On 12/20/2011 02:32 PM, Michal Minich wrote: My naive approach doesn't works struct Item1 (T) {} struct Item2 (T) {} struct Group (Items ...) { // how to do this? ... no static foreach :( static foreach (I; Items) mixin I!(int); } void main () { alias Group!(Item1, Item2) G; } This will do what you want: Solution 1: struct Item1(T){} struct Item2(T){} mixin template getItems(Items ...){ static if(Items.length) { private alias Items[0] _item; mixin _item!int; mixin getItems!(Items[1..$]); } } struct Group(Items...) { mixin getItems!Items; } void main(){ alias Group!(Item1, Item2) G; } Solution 2: struct Item1(T){} struct Item2(T){} struct Group(Items...) { private static string gen(){ string r; foreach(i;0..Items.length){ import std.conv; r~=`private alias Items[`~text(i)~`] _item`~text(i)~`;`; r~=`mixin _item`~text(i)~`!int;`; } return r; } mixin(gen()); } void main(){ alias Group!(Item1, Item2) G; } But I'd rather have static foreach too.
Re: newbie question: Can D do this?
On 12/20/2011 03:18 PM, clk wrote: Thank you for your quick replies. I'm impressed by the helpfulness and dedication of the D community! Here's another one. Is there a way to pass arguments to functions by keyword as in the calls to f and g below? void f(int a = 0, int b = 1) {} void g(int a) {} void main() { f(b = 1, a = 0); // compile error g(a = 0); // also compile error } No, there are no named arguments in D. Having them would sometimes be useful, but the drawback is that the parameter names become part of the public interface. On 12/19/2011 03:00 PM, digitalmars-d-learn-requ...@puremagic.com wrote: Send Digitalmars-d-learn mailing list submissions to digitalmars-d-learn@puremagic.com To subscribe or unsubscribe via the World Wide Web, visit http://lists.puremagic.com/cgi-bin/mailman/listinfo/digitalmars-d-learn or, via email, send a message with subject or body 'help' to digitalmars-d-learn-requ...@puremagic.com You can reach the person managing the list at digitalmars-d-learn-ow...@puremagic.com When replying, please edit your Subject line so it is more specific than Re: Contents of Digitalmars-d-learn digest... Today's Topics: 1. Re: newbie question: Can D do this? (Ali ?ehreli) 2. Re: newbie question: Can D do this? (Kai Meyer) 3. Re: newbie question: Can D do this? (Simen Kj?r?s) 4. Re: newbie question: Can D do this? (Ali ?ehreli) 5. Re: newbie question: Can D do this? (Jonathan M Davis) -- Message: 1 Date: Mon, 19 Dec 2011 10:41:29 -0800 From: Ali ?ehreliacehr...@yahoo.com To:digitalmars-d-learn@puremagic.com Subject: Re: newbie question: Can D do this? Message-ID:jco0gq$1ilt$1...@digitalmars.com Content-Type: text/plain; charset=UTF-8; format=flowed On 12/19/2011 08:17 AM, clk wrote: I'm a little bit intimidated by the fact that the topics in the d-learn list look rather advanced to a newbie like me. We need more newbie topics here! :) 1) Does D support something like the javascript 1.8 destructuring assigment (multiple assigment in python): [a, b] = [b, a]; No multiple assignment like that. But useful approarches exist for most needs, like the swap that simendsjo has shown. 2) D doesn't seem to support the list comprehension syntax available in python and javascript. Is this correct? [f(x) for x in list if condition] List comprehension is not part of the language. import std.algorithm; void f(int x) {} bool condition(int x) { return true; } void main() { auto list = [ 0, 1, 2 ]; map!f(filter!condition(list)); } You can define f and condition within the body of main(). It is possible to use function literals as well: import std.algorithm; void main() { auto list = [ 0, 1, 2 ]; map!((x){ /* ... this is f(x) ...*/ })(filter!((x) { return true; /* ... condition ... */ })(list)); } 3) D's slice operator apparently doesn't allow the use of a stride other than unity as is allowed with fortran and matlab. Is there a way to implement this feature so that [1, 2, 3, 4, 5][0..$:2] would refer to [1, 3, 5], etc..., where 2 is the non unit stride. Or is the find function from std.algorithm the only option to achieve the same behavior. std.range.stride does that: import std.range; // ... stride([1, 2, 3, 4, 5], 2) I find the 3 features above extremely convenient in every day coding. Thanks, -clk Ali -- Message: 2 Date: Mon, 19 Dec 2011 11:50:33 -0700 From: Kai Meyerk...@unixlords.com To:digitalmars-d-learn@puremagic.com Subject: Re: newbie question: Can D do this? Message-ID:jco11q$1lle$1...@digitalmars.com Content-Type: text/plain; charset=ISO-8859-1; format=flowed On 12/19/2011 09:17 AM, clk wrote: Hello, I'm new to this mailing list. I'm trying to learn D to eventually use it in production code. I'm a little bit intimidated by the fact that the topics in the d-learn list look rather advanced to a newbie like me. I have 3 fairly simple questions: 1) Does D support something like the javascript 1.8 destructuring assigment (multiple assigment in python): [a, b] = [b, a]; I would love multiple assignment like this, but it's tricky. But your usage isn't really multiple assignment as much as it is a swap. What I'd love is something like this: [a, b, c] = [get_a(), get_b(), get_c()]; Or [a, b, c] = [to!(int)(argv[1]), some_other_value, argv[4]); 2) D doesn't seem to support the list comprehension syntax available in python and javascript. Is this correct? [f(x) for x in list if condition] No, D's syntax is very C-ish. I don't expect syntax like this to ever show up (though what you are doing is possible with things like std.algorithm) 3) D's slice operator apparently doesn't allow the use of a stride other than unity as is
Re: Void initialization
On Tue, 20 Dec 2011 09:22:46 -0500, Stewart Gordon smjg_1...@yahoo.com wrote: On 19/12/2011 18:11, Steven Schveighoffer wrote: On Mon, 19 Dec 2011 12:24:18 -0500, Bear joanylepri...@yahoo.fr wrote: gc.malloc actually returns void[] http://www.d-programming-language.org/phobos/core_memory.html#malloc Looks like void* to me... Or is there another function I'm not aware of? I think it should be GC.malloc, not gc.malloc, so maybe I'm missing something... snip You are. That the OP was talking about std.gc.malloc in D1, not the core.memory stuff in D2. Ah, my bad. Having never used phobos1, I have no idea how it is implemented, so I can't help here. Well, I did use it for a week before thinking there has to be something better and found Tango :) I think bearophile has fell for the same thing. -Steve
Re: newbie question: Can D do this?
On Tue, 20 Dec 2011 09:18:16 -0500, clk c...@clksoft.com wrote: Thank you for your quick replies. I'm impressed by the helpfulness and dedication of the D community! Here's another one. Is there a way to pass arguments to functions by keyword as in the calls to f and g below? void f(int a = 0, int b = 1) {} void g(int a) {} void main() { f(b = 1, a = 0); // compile error g(a = 0); // also compile error } No. There are workarounds, but they are quite ugly. -Steve
Re: Void initialization
On 12/19/2011 01:04 PM, Bear wrote: Using D1, I have a program that creates tons of float[] ; for performance reasons, I would like them to be uninitialized. I've tried replacing float[] f = new float[x]; by float[] f = cast(float[])std.gc.malloc(x*4); Unfortunately I keep running into Access violation and sometimes Array bounds error. I've tried adding setTypeInfo(typeid(float), f.ptr); and hasNoPointer(f.ptr); which didn't work. However f[] = float.nan; solved the problem, but kinda defeats the purpose of using malloc... What am I doing wrong? Are you using GDC? https://bitbucket.org/goshawk/gdc/issue/287/casting-between-array-types-is-broken If so, this is the workaround: float[] f = (cast(float[])std.gc.malloc(x*4))[0..x];
Re: Allocating memory in D shared library when accessed from C++
Dne 20.12.2011 2:22, Andrej Mitrovic napsal(a): test.cpp: http://www.ideone.com/uh7vN DLibrary.d: http://www.ideone.com/fOLN8 $ g++ test.cpp $ dmd -ofDLibrary.dll DLibrary.d $ a.exe $ 9 Hi, Andrej, you are right, this works. Problem is going to be either in VisualD or cv2pdb. For those who are interested: The version produced by VisualD simply does not work and crashes. Hovewer, if you just execute dmd command that VisualD uses and then manually call cv2pdb, everything works fine and you can hapilly debug in Visual Studio. Library compiled by VisualD: DLibrary.dll | size: 936 082B DLibrary.pdb | size: 248 832B Library compiled by using VisualD commandline parameters: - $ dmd dllmain.d -g -debug -X -XfDLibrary.json -deps=DLibrary.dep -of..\Debug\DLibrary.dll -map DLibrary.map Results after calling cv2pdb on the DLibrary.dll DLibrary.dll | size: 935 570B DLibrary.pdb | size: 248 832B Manual execution of that two commands produces library that works and is debugable and also, by some strange coincidence, 112 bytes smaller. Funny... if you have any ideas, do not hesitate to share ;-) Regards, Martin
Re: Void initialization
Stewart Gordon: On 19/12/2011 12:12, bearophile wrote: Try something like this (untested): alias float TF; TF[] f = (cast(TF*)std.gc.malloc(x * TF.sizeof))[0 .. x]; snip I fail to see any real difference from the OP's code: - Why the alias? Because in that code I have used three times a type (TF), auto allows to remove only one of them. The alias is not the best solution (a better solution is to put that code into a templated function), but repeating the same generic type more than one time is usually a source of bugs. - std.gc.malloc returns the array with correct length according to my quick test, so the use of [0..x] is redundant Really? Well, as I have said I have not tested that code. Generally GC functions return a void*, so to create an array I think you need to slice it... What is the code of your quick test? - using TF.sizeof instead of 4 might fix things if on the user's platform float isn't 4 bytes long. In D I think float is always 4 bytes long. Otherwise, while using .sizeof instead of a hard-coded number is better practice, it isn't going to get rid of an AV. I don't know what an AV is. Bye, bearophile
Re: How to mixin each element of tuple
Michal Minich: struct Group (Items ...) { // how to do this? ... no static foreach :( static foreach (I; Items) In D if you use foreach on a typeTuple you get a static foreach. Bye, bearophile
Re: Void initialization
On 12/20/2011 07:12 PM, bearophile wrote: Stewart Gordon: On 19/12/2011 12:12, bearophile wrote: Try something like this (untested): alias float TF; TF[] f = (cast(TF*)std.gc.malloc(x * TF.sizeof))[0 .. x]; snip I fail to see any real difference from the OP's code: - Why the alias? Because in that code I have used three times a type (TF), auto allows to remove only one of them. The alias is not the best solution (a better solution is to put that code into a templated function), but repeating the same generic type more than one time is usually a source of bugs. - std.gc.malloc returns the array with correct length according to my quick test, so the use of [0..x] is redundant Really? Well, as I have said I have not tested that code. Generally GC functions return a void*, so to create an array I think you need to slice it... What is the code of your quick test? - using TF.sizeof instead of 4 might fix things if on the user's platform float isn't 4 bytes long. In D I think float is always 4 bytes long. Otherwise, while using .sizeof instead of a hard-coded number is better practice, it isn't going to get rid of an AV. I don't know what an AV is. Bye, bearophile Access Violation.
Re: How to mixin each element of tuple
On 12/20/2011 07:13 PM, bearophile wrote: Michal Minich: struct Group (Items ...) { // how to do this? ... no static foreach :( static foreach (I; Items) In D if you use foreach on a typeTuple you get a static foreach. Bye, bearophile Yes, but foreach cannot be used in declaration scope. I think having static foreach would greatly benefit the language. I regularly run into cases where I have to use one of the two inferior solutions when static foreach would be just the right tool for the job.
Re: Allocating memory in D shared library when accessed from C++
I'd say make a small test-case and file it to visuald's bugtracker.
Re: How to mixin each element of tuple
Timon Gehr: I think having static foreach would greatly benefit the language. Vote here! :-) http://d.puremagic.com/issues/show_bug.cgi?id=4085 Bye, bearophile
Re: newbie question: Can D do this?
On 20/12/2011 14:18, clk wrote: Here's another one. Is there a way to pass arguments to functions by keyword as in the calls to f and g below? I remember a discussion about year ago or so. It seems doable to have some kind of function transformer (adaptor?) for this. from: int foo(int a = 0, int b = 1, double c = 0.0, bool d = false) { return 1;} alias namedParams!foo nfoo; // transform it into a called-by-name function. nfoo([d: true]); // a = 0, b = 1, c = 0.0, d = true nfoo([d : true], [b : 100]); // a=0, b=100, c=0.0, d=true nfoo(1, 2, [d : true]); // a=1, b=2, c=0.0, d=true That is, it expects some values, then string/values couples as associative arrays. Would that be palatable? Because I think it's doable. To obtain the arguments names: int foo(int a, int b, double c = 0.0, bool d = true) { return 1;} template Name(alias foo) if (isCallable!foo) { enum string Name = S!(foo.stringof); } template S(string s) // this template is just a trick because foo.stringof directly displeases DMD { enum string S = s; } writeln(Name!foo); // int(int a, int b, double c = 0, bool d = true) So this gives us: - the arguments names - which ones have default values - what is that default value The difficulty here is correctly parsing the ( ,,,) part, without getting desoriented by argument types that themselves use (,), like templated types. I think that would make for an small interesting community challenge. Philippe
Re: newbie question: Can D do this?
On Mon, Dec 19, 2011 at 17:17, clk c...@clksoft.com wrote: 2) D doesn't seem to support the list comprehension syntax available in python and javascript. Is this correct? [f(x) for x in list if condition] Correct. As other have said, it's doable by combining std functions. As fas as I know, we do not have a cartesian product range, to iterate on all combinations of two or more ranges. [f(x,y) for x in list1 for y in list2 if condition] I gave it a try a few years ago and could get something like this: auto lc = comp!(tuple(a,b,c), a*a+b*b == c*c ab)(input1, input2, input3); - mapper, condition, input ranges, as many as you wish But at the time I couldn't find a way to do bindings, that is: [f(x,y) for x in [0..10] for y in [0..x]] - the range iterated by y depends on x. If anyone has an idea, I'm game. Philippe
Re: Void initialization
On 20/12/2011 18:12, bearophile wrote: snip Because in that code I have used three times a type (TF), auto allows to remove only one of them. The alias is not the best solution (a better solution is to put that code into a templated function), but repeating the same generic type more than one time is usually a source of bugs. I don't quite understand - why not just use float as it is? OK, so abbreviating it to TF saves 9 characters on that line, but the alias declaration and its trailing line break take up 16 characters, so you're not saving space at all. Moreover, the style guide discourages meaningless type aliases. (OK, so there are things I disagree with there, like using spaces not tabs for indentation, but that's another matter.) - std.gc.malloc returns the array with correct length according to my quick test, so the use of [0..x] is redundant Really? Well, as I have said I have not tested that code. Generally GC functions return a void*, so to create an array I think you need to slice it... If that were the case, the OP's code wouldn't have compiled. I made out that the OP was getting these errors at runtime, not compiletime. What is the code of your quick test? snip import std.stdio, std.gc; void main() { size_t x = 42; float[] f = cast(float[]) std.gc.malloc(x*4); writefln(f.length); alias float TF; f = (cast(TF*)std.gc.malloc(x * TF.sizeof))[0 .. x]; writefln(f.length); } Stewart.
Re: Void initialization
Stewart Gordon: I don't quite understand - why not just use float as it is? OK, so abbreviating it to TF saves 9 characters on that line, but the alias declaration and its trailing line break take up 16 characters, so you're not saving space at all. It's not a way to save chars, it's a way to avoid bugs, making the code my DRY. Moreover, the style guide discourages meaningless type aliases. That's also why I have said a better solution is to wrap that code into a function template, so there is no need for an alias. 6 import std.stdio, std.gc; void main() { size_t x = 42; float[] f = cast(float[]) std.gc.malloc(x*4); writefln(f.length); alias float TF; f = (cast(TF*)std.gc.malloc(x * TF.sizeof))[0 .. x]; writefln(f.length); } I didn't know this. It's handy. Bye, bearophile
Re: newbie question: Can D do this?
On 20/12/2011 20:36, Philippe Sigaud wrote: snip That is, it expects some values, then string/values couples as associative arrays. snip I've a recollection of seeing something like this in the PHP library, but I forget where. I believe it's used in some functions that have a lot of options to set, such that you'll typically just want to set a few of them in a given call. Stewart.
Re: Void initialization
On 20/12/2011 22:19, bearophile wrote: snip That's also why I have said a better solution is to wrap that code into a function template, so there is no need for an alias. snip So what you actually meant was to make TF a template parameter? That would make more sense. I can understand an alias being an intermediate step towards refactoring complex code into a template, but if you present it as part of a solution in simple cases like this then it's bound to get people wondering why on earth you're doing it, and possibly detract from what you're doing to actually (attempt to) solve the problem with the original code. Stewart.