Re: question on map
On Wednesday, 12 May 2021 at 09:52:52 UTC, Alain De Vos wrote: As oppposed to what i expect code below prints nothing nothing on the screen. What is wrong and how to fix it ? ``` import std.stdio; import std.range:iota; import std.algorithm:map; bool mywriteln(int x){ writeln(x); return true; } void main(){ 5.iota.map!mywriteln; } ``` On Wednesday, 12 May 2021 at 09:52:52 UTC, Alain De Vos wrote: think lazy :)) 5.iota.map!mywriteln.array;
Re: rgba.ptr[0] vs rgba[0]
On Monday, 23 November 2020 at 17:39:09 UTC, Adam D. Ruppe wrote: On Monday, 23 November 2020 at 17:34:27 UTC, visitor wrote: Hi all, I would like to know why in the code below, rgba.ptr[0] is used instead of rgba[0] and allowing the method to be @safe The .ptr[0] skips bounds checking. Since this example is static length with a constant index it shouldn't matter anyway; the compiler can see it is obviously in bounds and skip it too. But if there's any runtime value there's a bounds check with `foo[0]` and that can be surprisingly expensive in certain situations. So `foo.ptr[0]` skipping that can give a nice performance boost. Just without bounds checking the code is obviously trusting the programmer... hence @trusted is required instead of safe. indeed because of the the static length and constant index, it was puzzling me ... Thanks Adam for clarification
rgba.ptr[0] vs rgba[0]
Hi all, I would like to know why in the code below, rgba.ptr[0] is used instead of rgba[0] and allowing the method to be @safe float[4] rgba = 0; ref inout(float) r() inout pure @trusted { pragma(inline, true); return rgba.ptr[0]; } why not : ref inout(float) r() inout pure @safe { pragma(inline, true); return rgba[0]; } avoid an allocation maybe ? Thanks for your time.
Re: How to use labeled break in static foreach?
https://run.dlang.io/is/xiqi4P not pretty :)) but ...
Re: How to use labeled break in static foreach?
On Wednesday, 9 September 2020 at 17:02:26 UTC, visitor wrote: On Friday, 14 February 2020 at 06:41:02 UTC, cc wrote: import std.meta; enum A = AliasSeq!(1, 2, 3, 4); static foreach (idx, field; A) { static if (__traits(compiles, THREELOOP)) {} else { static if (field == 3) { pragma(msg, "Got a 3!"); enum THREELOOP = 1; // works with enum, alias etc... } static if (idx == A.length - 1) { static assert(0, "Got no 3..."); } } } https://run.dlang.io/is/BDmIml oops the online editor saved a mix of an old iteration and a new one ... (probably my fault) import std.stdio; float test(float x) { float r = -1; static foreach(i; 0 .. 100) { static if (__traits(compiles, Check)) {} else { static if (i == 10) enum Check = 1; r = i * x; } } return r; } void main() { float ret = test(4.2); writefln("ret = %s", ret); }
Re: How to use labeled break in static foreach?
On Friday, 14 February 2020 at 06:41:02 UTC, cc wrote: import std.meta; enum A = AliasSeq!(1, 2, 3, 4); static foreach (idx, field; A) { static if (__traits(compiles, THREELOOP)) {} else { static if (field == 3) { pragma(msg, "Got a 3!"); enum THREELOOP = 1; // works with enum, alias etc... } static if (idx == A.length - 1) { static assert(0, "Got no 3..."); } } } https://run.dlang.io/is/BDmIml
Re: Is there any web browser control in D Lang to display html file ?
On Friday, 16 March 2018 at 20:19:59 UTC, aberba wrote: On Friday, 16 March 2018 at 17:11:17 UTC, visitor wrote: On Friday, 16 March 2018 at 10:31:51 UTC, Jayam wrote: I creating one simple desktop application using dlang. I need to display some html file in my desktop application. How can make it works ? There's also gtkd sourceview : https://github.com/gtkd-developers/GtkD/tree/master/generated/sourceview I think you mean Gtk web view. It comes from WebKit. oh yes, my bad, misreading, i thought OP wanted syntax highlighting and stuff...
Re: Is there any web browser control in D Lang to display html file ?
On Friday, 16 March 2018 at 10:31:51 UTC, Jayam wrote: I creating one simple desktop application using dlang. I need to display some html file in my desktop application. How can make it works ? There's also gtkd sourceview : https://github.com/gtkd-developers/GtkD/tree/master/generated/sourceview
Re: importing std.array: empty in a struct messes things up
On Sunday, 4 March 2018 at 19:53:59 UTC, ag0aep6g wrote: On 03/04/2018 08:45 PM, ag0aep6g wrote: I don't know what's going on here. The error message doesn't make sense to me. Might be a bug in the compiler. This one works: struct Stack(T) { T[] stack; alias stack this; bool empty() {return empty(stack);} /* not using UFCS */ import std.array: empty; /* has to come after the method */ } Looks very much like a compiler bug now. As far as I know, the order of declarations shouldn't have an effect like that in structs. And UFCS should also be ok there, as far as I can tell. ? https://run.dlang.io/is/Ws0qEx https://run.dlang.io/is/Bancgx
Re: How to concatenate a tuple of strings at compile time?
On Saturday, 6 January 2018 at 19:35:33 UTC, paul wrote: Hi! How to concatenate a tuple of strings at compile time? Something like that maybe ? import std.stdio; import std.meta; enum connected = () { auto something = AliasSeq!("one", "two", "three"); string res = ""; static foreach(item; something) {{ res ~= item; }} return res; }(); void main() { writefln("connected = %s", connected); }
Re: Putting function pointers / delegates into associative array.
On Thursday, 4 January 2018 at 01:40:21 UTC, Mark wrote: What I'm trying to do here is to be able to call a function based off of a key. class tester { private void delegate() [char] funcs; this() { funcs = ['a': , 'b': ]; } public void callFuncs(char a) { funcs[a](); } private void A() { writeln("A"); } private void B() { writeln("B"); } }
Re: Converting array in to aliased tuple type.
On Monday, 25 December 2017 at 21:35:18 UTC, visitor wrote: On Monday, 25 December 2017 at 21:11:08 UTC, aliak wrote: On Monday, 25 December 2017 at 17:59:54 UTC, visitor wrote: On Monday, 25 December 2017 at 15:03:08 UTC, aliak wrote: On Monday, 25 December 2017 at 14:08:08 UTC, Mengu wrote: probably better this way int[2] tmp; auto points = data.split(':') .map!( (a) { a.split(',').map!(to!int).enumerate() .each!( (i, el) => tmp[i] = el ); return Point(tmp); } );
Re: Converting array in to aliased tuple type.
On Monday, 25 December 2017 at 21:11:08 UTC, aliak wrote: On Monday, 25 December 2017 at 17:59:54 UTC, visitor wrote: On Monday, 25 December 2017 at 15:03:08 UTC, aliak wrote: On Monday, 25 December 2017 at 14:08:08 UTC, Mengu wrote: I was kind of hoping for some magical D variadic alias template on Tuple or something that will just deconstruct the arguments in to tuple components. i don't think it's better but : https://run.dlang.io/is/2rgOzh Heh, no it's probably not, but interesting! So it does work with a static array. Is it that .array on a range is not able to infer a size and hence produce a static array for the given situation? Is this a D limitation, a logical one or maybe just not implemented yet? Cheers! i don't know the reason, and would be glad to be unlighted :) There is a Tuple constructor which takes a static array as a param : https://dlang.org/phobos/std_typecons.html#.Tuple.this.2 i needed to iterate with an index when building the static array later, so i transformed the range (a.split(',').map!(to!int)) into a dynamic array, to do so. one could use std.range enumerate() instead: auto points = data.split(':') .map!( a => a.split(',').map!(to!int) ) .map!( (a) { int[2] staticArray; foreach (i, el; a.enumerate()) staticArray[i] = el; return staticArray; } ) .map!Point; Cheers
Re: Converting array in to aliased tuple type.
On Monday, 25 December 2017 at 17:59:54 UTC, visitor wrote: On Monday, 25 December 2017 at 15:03:08 UTC, aliak wrote: On Monday, 25 December 2017 at 14:08:08 UTC, Mengu wrote: I was kind of hoping for some magical D variadic alias template on Tuple or something that will just deconstruct the arguments in to tuple components. i don't think it's better but : https://run.dlang.io/is/2rgOzh oops sorry, compile time ... import std.stdio, std.string, std.algorithm, std.conv, std.range; import std.typecons; alias Point = Tuple!(int, "x", int, "y"); enum data = "1,2:8,9"; auto points = data.split(':') .map!( a => a.split(',').map!(to!int).array ) .map!( (da) { int[2] staticArray; foreach (i, el; da) staticArray[i] = el; return staticArray; } ) .map!Point ; void main() { writefln("points : %s", points); }
Re: Converting array in to aliased tuple type.
On Monday, 25 December 2017 at 15:03:08 UTC, aliak wrote: On Monday, 25 December 2017 at 14:08:08 UTC, Mengu wrote: I was kind of hoping for some magical D variadic alias template on Tuple or something that will just deconstruct the arguments in to tuple components. i don't think it's better but : https://run.dlang.io/is/2rgOzh
Re: Allocating a class within another class during object init w/o passing in an allocator
On Thursday, 15 December 2016 at 17:44:23 UTC, David Zhang wrote: would something like this be a solution ? import std.stdio; import std.experimental.allocator; class SomeClass { int someint = 42; static SomeClass opCall(int a) { auto inst = theAllocator.make!SomeClass; inst.someint += a; return inst; } void destruct() { theAllocator.dispose(this); } } class Foo { this() { sc = SomeClass(7); sc.someint -= 42; } ~this() { sc.destruct(); } size_t something; SomeClass sc; } void main(string[] args) { auto foo = theAllocator.make!Foo; assert(foo.sc.someint == 7); theAllocator.dispose(foo); }
Re: functional way doing array stuff/ lambda functions
On Monday, 14 December 2015 at 11:45:50 UTC, Namal wrote: foreach(k;1..11){ auto t = prim_factors(k,P); v~= [k,product(t)]; } it crashes because your first t in the loop is an empty array because 1 is not a prime ( in "prim_sieve" : T[1] = true) later when you loop starting with k = 1, prim_factors(1,P) gives you back an empty array. so either loop through 2..11 or guard against the empty array if (!t.empty) v~= [k, product(t)]; i don't think you need any ref in your functions parameters, as a slice is already a reference http://dlang.org/spec/arrays.html#slicing
Re: functional way doing array stuff/ lambda functions
On Sunday, 13 December 2015 at 03:08:33 UTC, Namal wrote: This works for me : import std.stdio, std.algorithm, std.range; int[] prim_factors(int n, const int[] P) { int[] v; P.filter!( x => x*x <= n).each!( (i) { while (n % i == 0) { v ~= i; n /= i; if (i == 1) break; // infinite loop otherwise ! } } ); if (n > 1) v ~= n; return v.sort.uniq.array; } void main(string[] args) { int[] P = [1,2,3,4,5]; writeln( prim_factors(10, P).reduce!((r, i) => r*i)() ); }
Re: benchmark on binary trees
ok, i have a working version (memory is nice, twice the speed as non parallel) ; http://dpaste.dzfl.pl/504a652c6c47 real0m14.427s user1m19.347s sys 0m0.124s i've got similar performances, without Allocators, using directly malloc and free i had to recursively deallocate ... though, still far from competing with the fastest, any advice ? i guess FreeList allocator is not the better tool here ?
Re: benchmark on binary trees
version with apr (like in c version) http://dpaste.dzfl.pl/68c0157225e7 compiled with ldc it's indeed a bit faster on average : real0m1.999s user0m9.810s sys 0m0.148 btw Rust version is even faster than my little bit outdated gcc (4.9) latest try with allocators : http://dpaste.dzfl.pl/86b9b3c4ad71 swallows huge memory, slow ! what am i doing wrong ? (concurrency problem ?)
Re: benchmark on binary trees
using Apache Portable Runtime(APR) like in the C version : http://dpaste.dzfl.pl/6ca8b5ffd6dc works like a charm, 2.061s on my machine ! if file name is binarytrees.d dmd -w -inline -O -release -I/usr/include/apr-1.0 -L/usr/lib/x86_64-linux-gnu/libapr-1.so -of"binarytrees" "binarytrees.d" measured with "time ./binarytrees 20" on command line: real0m2.061s user0m8.156s sys 0m0.181s C version gives : real0m1.892s user0m9.087s sys 0m0.188s Not bad !!! :-D Still i would like to know what i'm doing wrong with the allocator's version, besides the fact that apr tools are doing more sophisticated work than my naive approach :-D ?
Re: benchmark on binary trees
C++ version : real0m3.587s user0m9.211s sys 0m7.341s
Re: benchmark on binary trees
On Monday, 7 December 2015 at 10:55:25 UTC, Alex wrote: On Sunday, 6 December 2015 at 12:23:52 UTC, visitor wrote: Hello, interesting exercise for me to learn about allocators :-) Nice to know, a novice can inspire someone :) i managed to parallelize the code reaching similar performance, in terms of speed, as the non parallel version : http://dpaste.dzfl.pl/6c3e6edcff59 Cool! This is what I looked for! BUT it consumes insane memory, don't try with argument more than 17 !!! I assume, the allocator itself is something, that is not really needed in this case. Maybe, there is a more straight forward access to the problem. Even a simpler then in all the versions on the benchgame site, but I don't see it right now. And with the allocator attempt I had a chance to experiment with the experimental module and to write a very quick copy of a program, which I want to have... i've got more speed improvement with "taskPool.parallel(depthind, 2)" in the foreach parallel loop : second argument are workUnits (2 for me, on a quad core gave best results) Also using directly "FreeList!(Mallocator, Tree_node.sizeof)" without wrapping it in an allocatorObject gives speed improvement (with changes to makeTree method) i took inspiration from the C code, they use a memory pool management, like anonymous already pointed in c++ version, which i think could (must?) be achieved with allocators, to gain speed i think it's a key point, no GC !! FreeList allocator appears (to me) as a good candidate for this. but as i'm new to this, i'm sure to not doing it the right way ! i tried the exact same FreeList allocator but backed with the GCAllocator (not the Mallocator used in my code), then memory consumption is very good but of course it"s slow ! i tried a lot of other allocators, variations on the presented code, but memory management is awful :(
Re: benchmark on binary trees
On Sunday, 6 December 2015 at 08:35:33 UTC, Alex wrote: Thanks for commenting to everyone! If anybody has further ideas - all of them would be appreciated :) The original site is not interested in any further languages to be tested, so my experiment ends here for now... Hello, interesting exercise for me to learn about allocators :-) i managed to parallelize the code reaching similar performance, in terms of speed, as the non parallel version : http://dpaste.dzfl.pl/6c3e6edcff59 BUT it consumes insane memory, don't try with argument more than 17 !!! so either i'm doing something stupid (in parallel code, i guess) or the FreeList allocator is totally not the right tool ... (both ?) some light-shedding would be really appreciated, Thanks
Re: Pixelbuffer to draw on a surface
gtkd demos for examples, might be of interest, like clock : https://github.com/gtkd-developers/GtkD/blob/master/demos/cairo/cairo_clock/clock.d
Re: having problem with `std.algorithm.each`
On Monday, 30 November 2015 at 09:56:08 UTC, ref2401 wrote: DMD 2.069.1 OS Win8.1 Enterprise in a multiline statement, i believe you must use : arr.each!((ref e) { writeln(e); ++e; }) "=>" is for oneliner though i don"t understand why it fails silently ??
Re: Pixelbuffer to draw on a surface
but there is no specific solution inside GTKD? for example, in pseudo code auto surf = new Surface(...); // look for cairo.Surface doc auto cr = cairo.Context.Context.create(surf); // pb is an existing gdkpixbuf.Pixbuf gdk.Cairo.setSourcePixbuf(cr, pb, width, height); cr.paint(); adding onDraw event listener on the widget owner of the Pixbuf (gtk.Image for example)
Re: having problem with `std.algorithm.each`
On Monday, 30 November 2015 at 12:03:08 UTC, anonymous wrote: On 30.11.2015 11:50, visitor wrote: though i don"t understand why it fails silently ?? ref2491's original code is valid, but doesn't have the intended meaning. `e => {foo(e);}` is the same as `(e) {return () {foo(e);};}`, i.e. a (unary) function that returns a (nullary) delegate. Calling it does not run foo. In contrast, calling this runs foo: `e => foo(e)`. `e => {foo(e);}` is the same as `(e) {return () {foo(e);};}` Ok, Thanks ! :-)