merging a group result
Hi all, I'm looking for a d-ish way to solve a basic "split-apply-combine" workflow. The idea is described (and solved) here: https://stackoverflow.com/questions/39922986/pandas-group-by-and-sum So, given a structure with some fields, say ´´´ struct S { string s; int i; } ´´´ I create an array of them, like ´´´ void main() { import std.experimental.all; S[] sarr; sarr.length = 6; sarr[0].s = "a"; sarr[1].s = "a"; sarr[2].s = "b"; sarr[3].s = "b"; sarr[4].s = "c"; sarr[5].s = "c"; sarr[0].i = 1; sarr[1].i = 2; sarr[2].i = 4; sarr[3].i = 8; sarr[4].i = 16; sarr[5].i = 32; auto res = sarr.group!((a, b) => a.s == b.s); //writeln(res); } ´´´ I'm also able to group them by a field, see last line. But now the problems begin: - The group operation tries to use the structure itself as a key, despite I provide a custom binary predicate. - I could ignore the fact above, but how, given the result of the grouping operation I can merge by some function (like sum) the group results? At this moment, I assume, that I'm approaching the problem from the wrong end, and simply don't see something trivial... Anyway, does anybody has a hint for me?
Re: How to get an inout constructor working with a template wrapper
On Sunday, 22 July 2018 at 23:11:09 UTC, Ali Çehreli wrote: Without much confidence on my side, first, I think you need to make the constructor parameter inout(T) as well. Otherwise, you may be making a const(W!T) initialized with a non-const T. After that, I like the "type constructor" syntax in main_alt() below (which works) but a better approach is to use a convenience function like wrap() below: struct W(T) { T val; this(inout(T) val) inout { this.val = val; } } class C {} void main_alt() { auto a = W!C(new C); auto b = immutable W!(immutable C)(new C); } auto wrap(T)(inout T t) { return inout(W!T)(t); } void main() { auto a = wrap(new C); auto b = wrap(new immutable(C)); } Ali "taklitlerinden sakınınız" :o) Thank you Ali! That helped :) I've gotten most of it sorted out now, and the factory wrap is definitely the way to go, it also turned out that inout(T) and inout T (so inout without parens) was surprisingly different (maybe it's a bug? - to test you can remove the parens around U on line 3 in this sample: https://run.dlang.io/is/gd5oxW Also over there, line 24: auto si = wrap!(immutable int)(3); seems to be giving problems. Any ideas there? Error is: onlineapp.d(8): Error: inout on return means inout must be on a parameter as well for pure nothrow @nogc @safe inout(W!(immutable(int)))(immutable(int) t) onlineapp.d(23): Error: template instance `onlineapp.wrap!(immutable(int))` error instantiating To make it compile successfully you can either: 1) Chance immutable to const, then it works for some reason. 2) Change the line to: "auto si = wrap(cast(immutable int)3);" - i.e. do not explicitly provide type information. Cheers, - Ali
Re: merging a group result
On 23/07/2018 11:49 PM, Alex wrote: At this moment, I assume, that I'm approaching the problem from the wrong end, and simply don't see something trivial... Anyway, does anybody has a hint for me? Map the range to the integer value, then sum it.
Re: How to get an inout constructor working with a template wrapper
On Monday, 23 July 2018 at 12:02:58 UTC, aliak wrote: https://run.dlang.io/is/gd5oxW Sorry wrong link! This one is correct -> https://run.dlang.io/is/azxmGN
Re: merging a group result
On Monday, 23 July 2018 at 11:49:58 UTC, Alex wrote: Hi all, I'm looking for a d-ish way to solve a basic "split-apply-combine" workflow. The idea is described (and solved) here: https://stackoverflow.com/questions/39922986/pandas-group-by-and-sum So, given a structure with some fields, say ´´´ struct S { string s; int i; } ´´´ I create an array of them, like ´´´ void main() { import std.experimental.all; S[] sarr; sarr.length = 6; sarr[0].s = "a"; sarr[1].s = "a"; sarr[2].s = "b"; sarr[3].s = "b"; sarr[4].s = "c"; sarr[5].s = "c"; sarr[0].i = 1; sarr[1].i = 2; sarr[2].i = 4; sarr[3].i = 8; sarr[4].i = 16; sarr[5].i = 32; auto res = sarr.group!((a, b) => a.s == b.s); //writeln(res); } ´´´ I'm also able to group them by a field, see last line. But now the problems begin: - The group operation tries to use the structure itself as a key, despite I provide a custom binary predicate. - I could ignore the fact above, but how, given the result of the grouping operation I can merge by some function (like sum) the group results? At this moment, I assume, that I'm approaching the problem from the wrong end, and simply don't see something trivial... Anyway, does anybody has a hint for me? You could use chunkBy: auto res = sarr.chunkBy!((a, b) => a.s == b.s).map!(a => tuple(a.front.s, a.map!(b => b.i).sum)); https://run.dlang.io/is/TJOEmf chunkBy: --- [S("a", 1), S("a", 2)] [S("b", 4), S("b", 8)] [S("c", 16), S("c", 32)] --- group: --- Tuple!(S, uint)(const(S)("a", 1), 2) Tuple!(S, uint)(const(S)("b", 4), 2) Tuple!(S, uint)(const(S)("c", 16), 2) ---
Re: merging a group result
On Monday, 23 July 2018 at 12:07:37 UTC, Seb wrote: You could use chunkBy: auto res = sarr.chunkBy!((a, b) => a.s == b.s).map!(a => tuple(a.front.s, a.map!(b => b.i).sum)); https://run.dlang.io/is/TJOEmf Ha... This helps! Thanks a lot! :)
Re: QWebView requesting QtE5WebEng32.so on windows
On Saturday, 21 July 2018 at 19:11:08 UTC, Dr.No wrote: So I went to try out QWebView on Windows from this wrapper: https://github.com/MGWL/QtE5 all the examples went fine until I tried QWebView: Prior to version Qt-5.5 WebKit was used, and for later versions of chromium-based WebEngine. You probably have a WebKit-based version and need to use: Loader(dll.Qt5widgets + dll.QtE5Web, true)). LoadQt (...) for Windows 32/64 WebEngine contains a typo ... Must be DLL
Re: How to get an inout constructor working with a template wrapper
On Monday, 23 July 2018 at 12:02:58 UTC, aliak wrote: Thank you Ali! That helped :) I've gotten most of it sorted out now, and the factory wrap is definitely the way to go, it also turned out that inout(T) and inout T (so inout without parens) was surprisingly different (maybe it's a bug? - to test you can remove the parens around U on line 3 in this sample: https://run.dlang.io/is/gd5oxW Also over there, line 24: auto si = wrap!(immutable int)(3); Both of these seem to work (as you pointed out) // immutable(W!int) auto si = wrap!(int)(cast(immutable)3); // or wrap(cast(immutable)3); // W!(immutable(int)) auto si2 = W!(immutable int)(3); seems to be giving problems. Any ideas there? Error is: onlineapp.d(8): Error: inout on return means inout must be on a parameter as well for pure nothrow @nogc @safe inout(W!(immutable(int)))(immutable(int) t) onlineapp.d(23): Error: template instance `onlineapp.wrap!(immutable(int))` error instantiating To make it compile successfully you can either: 1) Chance immutable to const, then it works for some reason. 2) Change the line to: "auto si = wrap(cast(immutable int)3);" - i.e. do not explicitly provide type information. I don't know why wrap!(immutable int)(3); is not working. The error message "Error: inout on return means inout must be on a parameter as well for pure nothrow @nogc @safe inout(W!(immutable(int)))(return immutable(int) t)" sounds very odd and not at all helpful, at least regarding that removing immutable from the template argument works. Cheers, - Ali The depths of D. Why does the following only work with "return ref"? struct W(T) { T val; this(U : T)(auto ref inout U val) inout { pragma(msg, typeof(val)); this.val = val; } } // Fails without "return ref" (escaping t warning...) auto wrap(T)(return ref inout T t) { return inout W!T(t); } class C {} void main() { immutable C ci = new immutable C; auto i = wrap(im); pragma(msg, typeof(i)); }
How to avoid inout type constructor with Optional type wrapper undoing string type
Hi, I'm playing around with an Optional wrapper type. It stores a type T and a bool that defines whether a value is defined or not: struct Optional(T) { T value; bool defined = false; this(U : T)(auto ref inout(U) value) inout { this.value = value; this.defined = true; } } To facilitate it's use I have two type constructors: inout(Optional!T) some(T)(auto ref inout(T) value) { return inout(Optional!T)(value); } Optional!T no(T)() { return Optional!T(); } The above produces a problem when working with strings. Basically the type information gets slightly altered so you can't do this: auto a = [no!string, some("hello")]; You get a type mismatch: * no!string = Optional!string * some("hello") = immutable(Optional!(char[])) I've created a short code gist, so basically I'm wondering how to get it to compile without changing what's in main() https://run.dlang.io/is/BreNdZ I guess I can specialize on string type T, but this is a more general problem that can be shown with: struct S {} alias Thing = immutable S; Thing thing = S(); auto x = some(thing); auto y = no!Thing; auto arr = [x, y]; // no can do buddy Cheers, - Ali
Re: How to avoid inout type constructor with Optional type wrapper undoing string type
On 2018-07-23 20:39, aliak wrote: Hi, I'm playing around with an Optional wrapper type. It stores a type T and a bool that defines whether a value is defined or not: struct Optional(T) { T value; bool defined = false; this(U : T)(auto ref inout(U) value) inout { this.value = value; this.defined = true; } } To facilitate it's use I have two type constructors: inout(Optional!T) some(T)(auto ref inout(T) value) { return inout(Optional!T)(value); } Optional!T no(T)() { return Optional!T(); } The above produces a problem when working with strings. Basically the type information gets slightly altered so you can't do this: auto a = [no!string, some("hello")]; You get a type mismatch: * no!string = Optional!string * some("hello") = immutable(Optional!(char[])) I've created a short code gist, so basically I'm wondering how to get it to compile without changing what's in main() https://run.dlang.io/is/BreNdZ I guess I can specialize on string type T, but this is a more general problem that can be shown with: struct S {} alias Thing = immutable S; Thing thing = S(); auto x = some(thing); auto y = no!Thing; auto arr = [x, y]; // no can do buddy This [1] compiles the first example but not the second. [1] https://run.dlang.io/is/SJ02kP -- /Jacob Carlborg
Re: How to avoid inout type constructor with Optional type wrapper undoing string type
On Monday, 23 July 2018 at 19:02:02 UTC, Jacob Carlborg wrote: This [1] compiles the first example but not the second. [1] https://run.dlang.io/is/SJ02kP Aye it does, but it also sets T to always const which is unfortunately impractical for my use case :(
Re: How to avoid inout type constructor with Optional type wrapper undoing string type
On Monday, 23 July 2018 at 19:22:13 UTC, aliak wrote: On Monday, 23 July 2018 at 19:02:02 UTC, Jacob Carlborg wrote: This [1] compiles the first example but not the second. [1] https://run.dlang.io/is/SJ02kP Aye it does, but it also sets T to always const which is unfortunately impractical for my use case :( Ok, now I'm totally confused. Defining an extra type constructor makes everything work. I.e add a const one to the inout one: auto defined(T)(const auto ref T value) { return W!T(value); } and everything works! Can anyone say why that is?
Re: How to avoid inout type constructor with Optional type wrapper undoing string type
On Monday, 23 July 2018 at 19:31:42 UTC, aliak wrote: Ok, now I'm totally confused. Defining an extra type constructor makes everything work. I.e add a const one to the inout one: auto defined(T)(const auto ref T value) { return W!T(value); } and everything works! Can anyone say why that is? Boh, seems other problems crop up now as doing this: auto a = defined!(int*)(null); produces error: onlineapp.d(13): Error: inout constructor onlineapp.W!(int*).W.__ctor!(int*).this creates const object, not mutable onlineapp.d(30): Error: template instance `onlineapp.defined!(int*)` error instantiating https://run.dlang.io/is/BWYxA8
trait to get the body code of a function?
Hello, __traits and std.traits already offer access to function information like input parameters, e.g. in std.traits: ParameterIdentifierTuple ParameterStorageClassTuple Even if that might sound strange, is there a compile time access to the body of the function, e.g. as a code string, or at least to the filename where it was declared? I have not found a direct "string" access, but I found these: https://dlang.org/phobos/std_traits.html#moduleName https://dlang.org/phobos/std_traits.html#packageName ...and I am not sure how to proceed from here to get the code as a string that declared the function. Context: I have some compile-time ideas in mind (code transformation). Best regards, Guillaume Lathoud
Re: trait to get the body code of a function?
On 24/07/2018 4:43 PM, Guillaume Lathoud wrote: Hello, __traits and std.traits already offer access to function information like input parameters, e.g. in std.traits: ParameterIdentifierTuple ParameterStorageClassTuple Even if that might sound strange, is there a compile time access to the body of the function, e.g. as a code string, or at least to the filename where it was declared? I have not found a direct "string" access, but I found these: https://dlang.org/phobos/std_traits.html#moduleName https://dlang.org/phobos/std_traits.html#packageName ...and I am not sure how to proceed from here to get the code as a string that declared the function. Context: I have some compile-time ideas in mind (code transformation). Best regards, Guillaume Lathoud Doesn't exist.