Re: D on ARM laptops?
On Thursday, July 4, 2019 2:49:18 AM MDT zoujiaqing via Digitalmars-d-learn wrote: > On Thursday, 4 July 2019 at 07:46:53 UTC, Paolo Invernizzi wrote: > > On Thursday, 4 July 2019 at 01:01:03 UTC, Nicholas Wilson wrote: > >> On Wednesday, 3 July 2019 at 20:49:20 UTC, JN wrote: > >>> Does anyone know if and how well D works on ARM laptops (such > >>> as Chromebooks and similar)? > >>> > >>> For example this one https://www.pine64.org/pinebook/ . Can > >>> it compile D? Obviously DMD is out because it doesn't have > >>> ARM builds. Not sure about GDC. How about LDC? > >> > >> Haven't tried on laptops, but I can't imagine it would be too > >> different to RasPi which LDC worked fine on. > > > > We are using LDC on Jetson boards and it's working fine ... > > LDC on FreeBSD working fine? It should work on FreeBSD 11. It almost certainly doesn't work on 12 because of bindings issues that still need to be resolved. Certainly, dmd does not work on FreeBSD 12, and ldc can't unless someone changed druntime specifically for ldc. - Jonathan M Davis
Re: Finding Max Value of Column in Multi-Dimesional Array
On Friday, 5 July 2019 at 00:54:15 UTC, Samir wrote: Is there a cleaner way of finding the maximum value of say the third column in a multi-dimensional array than this? int[][] p = [[1,2,3,4], [9,0,5,4], [0,6,2,1]]; writeln([p[0][2], p[1][2], p[2][2]].max); I've tried the following writeln([0, 1, 2].map!(p[a][2]).max); but get an "Error: undefined identifier a" error. I know there doesn't seem to be much of a difference between two examples but my real-world array is more complex which is why I'm looking for a more scalable option. Thanks Samir p.map!(a => a[2]).maxElement.writeln; // 5 p.map!"a[2]".maxElement.writeln; // 5 Or, modifying your example: writeln([0,1,2].map!(a => p[a][2]).maxElement; Thanks, Jordan
Re: Finding Max Value of Column in Multi-Dimesional Array
On Friday, 5 July 2019 at 00:54:15 UTC, Samir wrote: Is there a cleaner way of finding the maximum value of say the third column in a multi-dimensional array than this? int[][] p = [[1,2,3,4], [9,0,5,4], [0,6,2,1]]; writeln([p[0][2], p[1][2], p[2][2]].max); I've tried the following writeln([0, 1, 2].map!(p[a][2]).max); but get an "Error: undefined identifier a" error. I know there doesn't seem to be much of a difference between two examples but my real-world array is more complex which is why I'm looking for a more scalable option. Thanks Samir Hi Samir, You may want to take a look into mir-algorithm [1] library. It contains ndsilce package [2] to work with multidimensional data. The following example can be run online [3]: -- /+dub.sdl: dependency "mir-algorithm" version="~>3.4.4" +/ import mir.algorithm.iteration: reduce; import mir.ndslice: fuse, map, byDim; import mir.utility: max; import std.stdio: writeln; void main() { // create 2D matrix type of Slice!(int*, 2); auto matrix = [[1,2,3,4], [9,0,5,4], [0,6,2,1]].fuse; matrix .byDim!1 // by columns .map!(c => int.min.reduce!max(c)) .writeln; // [9, 6, 5, 4] } -- 1. https://github.com/libmir/mir-algorithm 2. http://mir-algorithm.libmir.org/mir_ndslice.html 3. https://run.dlang.io/is/OW6zvF
Finding Max Value of Column in Multi-Dimesional Array
Is there a cleaner way of finding the maximum value of say the third column in a multi-dimensional array than this? int[][] p = [[1,2,3,4], [9,0,5,4], [0,6,2,1]]; writeln([p[0][2], p[1][2], p[2][2]].max); I've tried the following writeln([0, 1, 2].map!(p[a][2]).max); but get an "Error: undefined identifier a" error. I know there doesn't seem to be much of a difference between two examples but my real-world array is more complex which is why I'm looking for a more scalable option. Thanks Samir
Re: Anyway to compare function aliases? Or any ideas?
On Thursday, 4 July 2019 at 15:22:08 UTC, Marco de Wild wrote: On Thursday, 4 July 2019 at 15:10:05 UTC, aliak wrote: [...] I don't know if it will solve your whole problem, but have you tried __traits(isSame, W0.fun, fun)? Reduced example: struct Foo(alias fun){ alias bar = fun; } void stuff(alias fun, T)(T t) { static if(__traits(isSame, fun, T.bar)) { pragma(msg, "Yes"); } else { pragma(msg, "No"); } } void a(){} void b(){} void main() { stuff!a(Foo!a()); // Yes stuff!a(Foo!b()); // No } Of course! That helps a lot yes. Thank you :) The case in the gist I pasted above works like a charm now. isSame seems like a decent enough powerhouse to get most of the practical cases it seems as well.
Re: For loop with separator
On Thursday, 4 July 2019 at 17:00:33 UTC, Q. Schroll wrote: Probably you've come over this problem once in a while, too. You have a repeating solution, so you use a for(each) loop. Sometimes, there is an action to be performed between the end of one iteration and the beginning of the next, if there is one. The prime example is printing the comma when printing a list: There is one between any two elements, but neither is one at front or behind the last one. Typical solutions I employed were: 1 Handling the first element separately 2 Condition in the loop, that is false exactly for the first iteration. 1 can be done with ranges easily: if (!range.empty) { action(range.front); range.popFront; foreach (element; range) { betweenAction(); action(element); } } This approach is clearly quite verbose for the problem, but there's nothing done unnecessarily. 2 can be done easily, too: foreach (i, element; range) { if (i > 0) betweenAction(); action(element); } While 2 is less code, it's prone to be checked every iteration. Note that 2 is rather D specific in its length. It can be done in other languages, but is more verbose. Is there a cleaner solution that I missed? As far as I can interpret it, joiner https://dlang.org/library/std/algorithm/iteration/joiner.html uses roughly the first approach.
For loop with separator
Probably you've come over this problem once in a while, too. You have a repeating solution, so you use a for(each) loop. Sometimes, there is an action to be performed between the end of one iteration and the beginning of the next, if there is one. The prime example is printing the comma when printing a list: There is one between any two elements, but neither is one at front or behind the last one. Typical solutions I employed were: 1 Handling the first element separately 2 Condition in the loop, that is false exactly for the first iteration. 1 can be done with ranges easily: if (!range.empty) { action(range.front); range.popFront; foreach (element; range) { betweenAction(); action(element); } } This approach is clearly quite verbose for the problem, but there's nothing done unnecessarily. 2 can be done easily, too: foreach (i, element; range) { if (i > 0) betweenAction(); action(element); } While 2 is less code, it's prone to be checked every iteration. Note that 2 is rather D specific in its length. It can be done in other languages, but is more verbose. Is there a cleaner solution that I missed?
Re: Anyway to compare function aliases? Or any ideas?
On Thursday, 4 July 2019 at 15:10:05 UTC, aliak wrote: Any ideas on how to be able to do something like this? struct S(alias _fun) { alias Fun = _fun; } void algorithm(alias f, T)(T s) { static if ( == ) { // trivial return } else { // must perform work, then return } } Can you use function addresses in some way? I've seen that some of them are resolved to __lambda0 and other times to a function type, i.e. Error: incompatible types for (& f) is (& __lambda1): void function(A!(function () => 3) t) and int function() pure nothrow @nogc @safe That comes from doing doing this: alias g = () => 3; algorithm!g(S!g()); I've thought of something along the lines of a function alias wrapper. But I'm not sure how to make that work either. Something like: struct Fun(alias _fun, string _id) { alias Fun = _fun; enum ID = _id; } Then maybe something like: alias g = () => 3; Fun!(g, "myid") gfun; algorithm!gfun(S!gfun()); Then inside algorithm the check becomes: static if ( == ) But then I'd like to generate the ID at instantiation point. So is using __LINE__, __FILE__ and applying some hash function a good idea here? Any other ideas? Cheers, - Ali PS: If you're curious of a semi working sample, to see what I'm actually trying to do, I've put up this gist that does not compile right now: https://gist.github.com/aliak00/fcdd4fa7512035405bb7015cf6d8016f I don't know if it will solve your whole problem, but have you tried __traits(isSame, W0.fun, fun)? Reduced example: struct Foo(alias fun){ alias bar = fun; } void stuff(alias fun, T)(T t) { static if(__traits(isSame, fun, T.bar)) { pragma(msg, "Yes"); } else { pragma(msg, "No"); } } void a(){} void b(){} void main() { stuff!a(Foo!a()); // Yes stuff!a(Foo!b()); // No }
Anyway to compare function aliases? Or any ideas?
Any ideas on how to be able to do something like this? struct S(alias _fun) { alias Fun = _fun; } void algorithm(alias f, T)(T s) { static if ( == ) { // trivial return } else { // must perform work, then return } } Can you use function addresses in some way? I've seen that some of them are resolved to __lambda0 and other times to a function type, i.e. Error: incompatible types for (& f) is (& __lambda1): void function(A!(function () => 3) t) and int function() pure nothrow @nogc @safe That comes from doing doing this: alias g = () => 3; algorithm!g(S!g()); I've thought of something along the lines of a function alias wrapper. But I'm not sure how to make that work either. Something like: struct Fun(alias _fun, string _id) { alias Fun = _fun; enum ID = _id; } Then maybe something like: alias g = () => 3; Fun!(g, "myid") gfun; algorithm!gfun(S!gfun()); Then inside algorithm the check becomes: static if ( == ) But then I'd like to generate the ID at instantiation point. So is using __LINE__, __FILE__ and applying some hash function a good idea here? Any other ideas? Cheers, - Ali PS: If you're curious of a semi working sample, to see what I'm actually trying to do, I've put up this gist that does not compile right now: https://gist.github.com/aliak00/fcdd4fa7512035405bb7015cf6d8016f
Re: Why are immutable array literals heap allocated?
On Thursday, 4 July 2019 at 10:56:50 UTC, Nick Treleaven wrote: immutable(int[]) f() @nogc { return [1,2]; } onlineapp.d(2): Error: array literal in `@nogc` function `onlineapp.f` may cause a GC allocation specify the size of the static array: immutable(int[ 2 /*HERE*/ ]) f() @nogc { return [1,2]; }
Re: Why are immutable array literals heap allocated?
On Thursday, 4 July 2019 at 10:56:50 UTC, Nick Treleaven wrote: immutable(int[]) f() @nogc { return [1,2]; } onlineapp.d(2): Error: array literal in `@nogc` function `onlineapp.f` may cause a GC allocation This makes dynamic array literals unusable with @nogc, and adds to GC pressure for no reason. What code would break if dmd used only static data for [1,2]? immutable(int[]) f() @nogc { static immutable arr = [1, 2]; return arr; } You have to spell it out that the data is static.
Why are immutable array literals heap allocated?
immutable(int[]) f() @nogc { return [1,2]; } onlineapp.d(2): Error: array literal in `@nogc` function `onlineapp.f` may cause a GC allocation This makes dynamic array literals unusable with @nogc, and adds to GC pressure for no reason. What code would break if dmd used only static data for [1,2]?
Re: Associative Array & different template types
On 2019-07-03 19:07:10 +, ag0aep6g said: On 03.07.19 20:20, Robert M. Münch wrote: So, I need to carry around the object from which a delegate was created from because it's not possible to query the delegate for the object later somewhere else in the code. It is possible to get the context object out of a delegate: class C { void method() {} } void main() { auto c = new C; void delegate() dg = assert(cast(C) dg.ptr is c); /* passes */ } You just have to know (or carry around) the type so that you can cast correctly. Hmm... IIRC I once played around and it didn't work... seems I messed this up. Thanks for correcting my wrong perception here. -- Robert M. Münch http://www.saphirion.com smarter | better | faster
Re: Blog Post #0048: Model, View, Controller
On Wednesday, 3 July 2019 at 17:12:04 UTC, lpcvoid wrote: Thanks for the time you invest in this. You're welcome. Tell your friends. :)
Re: D on ARM laptops?
On Thursday, 4 July 2019 at 07:46:53 UTC, Paolo Invernizzi wrote: On Thursday, 4 July 2019 at 01:01:03 UTC, Nicholas Wilson wrote: On Wednesday, 3 July 2019 at 20:49:20 UTC, JN wrote: Does anyone know if and how well D works on ARM laptops (such as Chromebooks and similar)? For example this one https://www.pine64.org/pinebook/ . Can it compile D? Obviously DMD is out because it doesn't have ARM builds. Not sure about GDC. How about LDC? Haven't tried on laptops, but I can't imagine it would be too different to RasPi which LDC worked fine on. We are using LDC on Jetson boards and it's working fine ... LDC on FreeBSD working fine?
Re: D on ARM laptops?
On Thursday, 4 July 2019 at 01:01:03 UTC, Nicholas Wilson wrote: On Wednesday, 3 July 2019 at 20:49:20 UTC, JN wrote: Does anyone know if and how well D works on ARM laptops (such as Chromebooks and similar)? For example this one https://www.pine64.org/pinebook/ . Can it compile D? Obviously DMD is out because it doesn't have ARM builds. Not sure about GDC. How about LDC? Haven't tried on laptops, but I can't imagine it would be too different to RasPi which LDC worked fine on. We are using LDC on Jetson boards and it's working fine ...