Re: Understanding switch + foreach

2014-04-17 Thread Timon Gehr via Digitalmars-d-learn
On 04/17/2014 08:04 PM, Matej Nanut wrote: The expansion with gotos explains the behaviour nicely! Cool. The error about fall-through is still missing though? Good point, this error should probably be triggered. I guess the problem is roughly that indeed every case statement in the code is

Re: Math-Parser

2014-05-03 Thread Timon Gehr via Digitalmars-d-learn
On 05/03/2014 12:34 AM, Tim Holzschuh via Digitalmars-d-learn wrote: Most probably this isn't a wrong use of something D-specific Some of the problems are: @property Lexer lexer() pure { return _lexer; } If you change the result of a call to 'lexer' this will not change '_lexer'. Mark the

Re: Math-Parser

2014-05-03 Thread Timon Gehr via Digitalmars-d-learn
On 05/03/2014 08:20 PM, Tim Holzschuh via Digitalmars-d-learn wrote: Let me know if you also want hints on how to get the logic right. Would be very nice! While 2*2 works, 2+2 throws an Error because the number-method gets an END-Token instead of a Number-Token (although I'm not sure why).

Re: const ref parameters and r-value references

2014-05-04 Thread Timon Gehr via Digitalmars-d-learn
On 05/04/2014 12:58 PM, Marc Schütz schue...@gmx.net wrote: This means that you will still get a (bit-wise) copy if you pass in an r-value. But semantically, this is a move, not a copy, so it is potentially cheaper than a copy (if your type has an expensive postblit). It can be constructed

Re: Math-Parser

2014-05-04 Thread Timon Gehr via Digitalmars-d-learn
On 05/04/2014 04:56 PM, Tim Holzschuh via Digitalmars-d-learn wrote: Am 03.05.2014 21:47, schrieb Timon Gehr via Digitalmars-d-learn: On 05/03/2014 08:20 PM, Tim Holzschuh via Digitalmars-d-learn wrote: Let me know if you also want hints on how to get the logic right. Would be very nice

Re: static if (__ctfe)

2014-05-08 Thread Timon Gehr via Digitalmars-d-learn
On 05/07/2014 12:07 PM, Yuriy wrote: On Wednesday, 7 May 2014 at 09:51:01 UTC, John Colvin wrote: On Wednesday, 7 May 2014 at 09:47:20 UTC, Yuriy wrote: Hello, is there any way to static if(__ctfe)? I want to declare class members which are only available in ctfe. Thanx. Sadly not as far as

Re: Question about @nogc

2014-05-20 Thread Timon Gehr via Digitalmars-d-learn
On 05/20/2014 11:04 PM, anonymous wrote: On Tuesday, 20 May 2014 at 20:15:09 UTC, Dominikus Dittes Scherkl wrote: /// create a fixed size array with the given name and with *max* entries max + 1 entries /// of immutable values of the same type as the return value of the /// given function.

Re: Question about @nogc

2014-05-20 Thread Timon Gehr via Digitalmars-d-learn
On 05/20/2014 11:48 PM, Timon Gehr wrote: This achieves the same: template lookupTable(alias fn,uint max=255){ static assert(maxuint.max); enum ReturnType!fn[max+1] lookupTable=iota(0,max+1).map!fn.array; } (Though I'd never actually do template argument checking in a static

Re: enums

2014-05-31 Thread Timon Gehr via Digitalmars-d-learn
On 05/31/2014 11:21 PM, Paul D Anderson wrote: On Saturday, 31 May 2014 at 20:14:59 UTC, bearophile wrote: Miles Stoudenmire: In contrast to those two examples where immutable can be used at compile time, what are some other cases where it is necessary to use enum instead of immutable? By

Re: Casting Structs

2014-05-31 Thread Timon Gehr via Digitalmars-d-learn
On 06/01/2014 12:25 AM, Ali Çehreli wrote: dec10 little = cast(dec10(bingo)); You meant cast(dec10)(bingo). assert(little == dec10(123.45)); Is this expected behavior? Paul That is surprising. I've discovered that if the template has members that depend on a template parameter than the

Re: Casting Structs

2014-06-01 Thread Timon Gehr via Digitalmars-d-learn
On 06/01/2014 09:59 AM, Philippe Sigaud via Digitalmars-d-learn wrote: On Sun, Jun 1, 2014 at 12:34 AM, Timon Gehr via Digitalmars-d-learn digitalmars-d-learn@puremagic.com wrote: This behaviour is independent of templates. Struct values of the same size can be reinterpret-cast to each other

Re: port C++ to D - copy constness

2014-06-02 Thread Timon Gehr via Digitalmars-d-learn
On 06/02/2014 09:06 AM, dennis luehring wrote: i want to port this C++ code to good/clean D and have no real idea how to start contains 2 templates - a slice like and a binary reader for an slice main idea was to copy the immutablity of the slice data to the reader http://pastebin.com/XX2yhm8D

Re: delegate issue

2014-06-02 Thread Timon Gehr via Digitalmars-d-learn
On 06/02/2014 04:30 PM, captaindet wrote: This doesn't work, because a delegate needs a context it can capture, which is available only inside of a function. so the real explanation is that a module as such has no context. much of the module design has the look and feel as if it were some

Re: Conflict between function and template with the same name

2014-06-29 Thread Timon Gehr via Digitalmars-d-learn
On 06/29/2014 11:31 AM, Rene Zwanenburg wrote: On Sunday, 29 June 2014 at 08:52:36 UTC, Uranuz wrote: import std.stdio; string getByName(string name) { return smth; } template getByName(string name) { enum getByName = .getByName(name); } void main() { writeln(getByName!(name));

Re: Question about @nogc in D 2.066

2014-07-11 Thread Timon Gehr via Digitalmars-d-learn
On 07/11/2014 10:39 PM, Dicebot wrote: Key difference is that type of string literal is immutable(char)[] so it is perfectly legal to keep it in binary text segment. Type of array literal is just T[] (int[] here) and you can possibly mutate their elements. Because of this each assignment of

Re: std.algorithm.among

2014-07-13 Thread Timon Gehr via Digitalmars-d-learn
On 07/13/2014 01:18 PM, bearophile wrote: The idea of not making std.algorithm.among!() a predicate was not so good: ... Agreed. void main() { import std.stdio, std.algorithm; auto s = hello how\nare you; s.until!(c = c.among!('\n', '\r')).writeln; } (A normal workaround is

Re: DStyle: Braces on same line

2014-07-13 Thread Timon Gehr via Digitalmars-d-learn
On 07/13/2014 06:45 PM, Joseph Rushton Wakeling via Digitalmars-d-learn wrote: Two consequences of adapting myself to Phobos style were that I realized (i)how little most of these things really matter, and (ii) pretty much any stylistic choice carries both benefits and drawbacks. ... Wrong.

Re: DStyle: Braces on same line

2014-07-13 Thread Timon Gehr via Digitalmars-d-learn
On 07/13/2014 07:51 PM, Brian Rogoff wrote: On Sunday, 13 July 2014 at 17:24:40 UTC, Timon Gehr wrote: On 07/13/2014 06:45 PM, Joseph Rushton Wakeling via Digitalmars-d-learn wrote: Wrong. There are things which are simply bad ideas. E.g. in this case, Egyptian-style braces definitely make

Re: std.algorithm.among

2014-07-13 Thread Timon Gehr via Digitalmars-d-learn
On 07/13/2014 08:51 PM, Meta wrote: That's weird, I always assumed this worked. Was it always the case that numeric types can't be implicitly casted to bool? Yes, unless their range fits into [0,2).

Re: Calling dynamically bound functions from weakly pure function

2014-07-19 Thread Timon Gehr via Digitalmars-d-learn
On 07/19/2014 03:53 PM, Kagamin wrote: It's ok to deduce opDispatch as pure, but then its purity should be enforced and error reported. Why would it be ok to deduce opDispatch as pure only to report an error that it is not actually pure? This would be a bug as well (albeit none that causes

Re: status of D optimizers benefiting from contracts ?

2014-11-10 Thread Timon Gehr via Digitalmars-d-learn
On 11/09/2014 11:39 PM, H. S. Teoh via Digitalmars-d-learn wrote: The original meaning of assert() is what assume() means nowadays, whereas nowadays what people think of as assert() is actually what enforce() does in Phobos. T No.

Re: status of D optimizers benefiting from contracts ?

2014-11-10 Thread Timon Gehr via Digitalmars-d-learn
On 11/09/2014 05:24 PM, H. S. Teoh via Digitalmars-d-learn wrote: On Sun, Nov 09, 2014 at 04:12:06PM +, bearophile via Digitalmars-d-learn wrote: H. S. Teoh: Walter *did* mention recently that he was planning to eventually take advantage of information in assert()'s as optimizer hints.

Re: Lambda functions in D

2015-05-09 Thread Timon Gehr via Digitalmars-d-learn
On 05/09/2015 05:52 PM, Dennis Ritchie wrote: On Saturday, 9 May 2015 at 14:15:21 UTC, Ali Çehreli wrote: On 05/09/2015 04:59 AM, Dennis Ritchie wrote: On Saturday, 9 May 2015 at 11:49:48 UTC, Timon Gehr wrote: assert((function int(int x)=x?x*__traits(parent,{})(x-1):1)(10)==3628800);

Re: Lambda functions in D

2015-05-09 Thread Timon Gehr via Digitalmars-d-learn
On 05/09/2015 01:20 PM, Dennis Ritchie wrote: Hi, Can lambda functions or delegates in D to call themselves? Can I write something like this: - import std.stdio; void main() { auto fact = function (int x) = x * { if (x) fact(x - 1); }; assert(fact(10) == 3628800); }

Re: UFCS

2015-05-15 Thread Timon Gehr via Digitalmars-d-learn
On 05/15/2015 11:31 PM, Manfred Nowak wrote: class C{} int main(){ void opOpAssign( string op)( C a, C b){ } C a, b; a+= b; } https://issues.dlang.org/show_bug.cgi?id=8062

Re: Autocorrelation function with ranges

2015-06-27 Thread Timon Gehr via Digitalmars-d-learn
On 06/27/2015 12:29 PM, kerdemdemir wrote: Hi My question is more about Maths than D lang, I am hoping, maybe somebody worked with AutoCorrelation function before. auto autoCorrelation(R)(R range) if (isRandomAccessRange!R) { auto residual = residualPowerOf2(range.length); //

Re: Autocorrelation function with ranges

2015-06-27 Thread Timon Gehr via Digitalmars-d-learn
On 06/27/2015 02:17 PM, Timon Gehr wrote: You then also don't need the final map to extract the real part. (This is actually not true, your inverseFFT presumably still returns complex numbers.)

Re: @property on free function for UFCS?

2015-06-14 Thread Timon Gehr via Digitalmars-d-learn
On 06/14/2015 05:50 PM, ketmar wrote: On Sun, 14 Jun 2015 12:26:52 +, rcorre wrote: Suppose I have a function defined like so: void foo(int i) { } intended to be called like: 5.foo Should it be labeled with @property? Or is @property only for true member functions? only if you plan

Re: drastic slowdown for copies

2015-05-28 Thread Timon Gehr via Digitalmars-d-learn
On 05/28/2015 11:27 PM, Adam D. Ruppe wrote: 16 bytes is 64 bit It's actually 128 bits.

Re: Get index of string in array at compile time

2015-05-29 Thread Timon Gehr via Digitalmars-d-learn
On 05/29/2015 06:43 PM, tcak wrote: I have define an immutable string array: [code] immutable string[] placeHolderDefinitionList = [ !-- fullname --, !-- list item -- ]; [/code] I need to get index of a string at compile time. So I have written a function as below: [code] public

Re: problem with custom predicate

2015-05-27 Thread Timon Gehr via Digitalmars-d-learn
On 05/27/2015 05:30 PM, Steven Schveighoffer wrote: On 5/27/15 9:11 AM, Simon =?UTF-8?B?QsO8cmdlciI=?= simon.buer...@rwth-aachen.de wrote: On Wednesday, 27 May 2015 at 14:58:39 UTC, drug wrote: Do you want to dynamically change priority? Actually yes. In my actual code I am not using a

Re: Null argument and function resolution

2015-05-27 Thread Timon Gehr via Digitalmars-d-learn
On 05/27/2015 10:09 PM, Meta wrote: On Wednesday, 27 May 2015 at 19:38:16 UTC, ketmar wrote: On Wed, 27 May 2015 14:09:47 +, Adam D. Ruppe wrote: Two options: 1) add an overload that takes typeof(null) this(typeof(null)) { /* handles the null literal specially */ } you keep breaking

Re: Static constructors guaranteed to run?

2015-06-27 Thread Timon Gehr via Digitalmars-d-learn
On 06/27/2015 11:54 AM, Marc =?UTF-8?B?U2Now7x0eiI=?= schue...@gmx.net wrote: Also are static constructors in templated types guaranteed to run for every instantiation? Even if the instantiation is never actually used outside of compile time code, like in an alias or in a UDA? Definitely

Re: Distinguish recursive Templates

2015-05-22 Thread Timon Gehr via Digitalmars-d-learn
On 05/23/2015 12:12 AM, Manfred Nowak wrote: Matt Kline wrote: isn't making any use of the template argument T Correct. I do not know how to use `T' to determine the recursion depth of the template---and I want no further parameter. -manfred import std.stdio, std.range, std.algorithm;

Re: Should these aliases kind be illegal ?

2015-08-12 Thread Timon Gehr via Digitalmars-d-learn
On 08/13/2015 12:17 AM, anonymous wrote: The following alias declaration is totally legal but actually it's not usable --- class Foo { void something(size_t param){} } class Bar { private Foo foo; this(){foo = new Foo;} alias somethingelse = foo.something; } void

Re: automatically verifying code samples in phobos docs

2015-08-19 Thread Timon Gehr via Digitalmars-d-learn
On 08/20/2015 01:41 AM, Laeeth Isharc wrote: BTW I don't know why you can't convert a char[] to string - seems harmless enough conversion that way around. It would need to allocate a new string, otherwise, one would be able to modify the contents of the immutable string via the char[]

Re: automatically verifying code samples in phobos docs

2015-08-19 Thread Timon Gehr via Digitalmars-d-learn
On 08/20/2015 02:02 AM, Laeeth Isharc wrote: On Thursday, 20 August 2015 at 00:00:55 UTC, Timon Gehr wrote: On 08/20/2015 01:41 AM, Laeeth Isharc wrote: BTW I don't know why you can't convert a char[] to string - seems harmless enough conversion that way around. It would need to allocate a

Re: Find on sorted range slower?

2015-08-07 Thread Timon Gehr via Digitalmars-d-learn
On 08/07/2015 11:03 AM, Tofu Ninja wrote: On Friday, 7 August 2015 at 08:18:04 UTC, Nordlöw wrote: On Friday, 7 August 2015 at 05:21:32 UTC, Tofu Ninja wrote: HAHAH wow, this is hilarious, I just checked, nothing in std.algo takes advantage of sorted ranges, sort doesn't even take advantage of

Re: iterating through a range, operating on last few elements at a time

2015-08-14 Thread Timon Gehr via Digitalmars-d-learn
On 08/14/2015 05:12 AM, H. S. Teoh via Digitalmars-d-learn wrote: On Fri, Aug 14, 2015 at 02:42:26AM +, Laeeth Isharc via Digitalmars-d-learn wrote: I have a range that is an array of structs. I would like to iterate through the range, calling a function with the prior k items in the

Re: iterating through a range, operating on last few elements at a time

2015-08-14 Thread Timon Gehr via Digitalmars-d-learn
On 08/14/2015 03:26 PM, Timon Gehr wrote: On 08/14/2015 05:12 AM, H. S. Teoh via Digitalmars-d-learn wrote: ... I didn't figure out how to eliminate the short slices toward the end, ... :o) ... Less hacky and less efficient: auto slidingWindow(R)(R range, int k) { return

Re: Attributes not propagating to objects via typeinfo?

2015-08-14 Thread Timon Gehr via Digitalmars-d-learn
On 08/13/2015 06:05 PM, Steven Schveighoffer wrote: On 8/13/15 11:59 AM, Steven Schveighoffer wrote: That is definitely a bug. It's because typeid is looking up the derived type via the vtable, but the compiler should rewrap it with 'shared' afterwards. Actually, now that I think about it,

Re: std.array: array, ulong and Win32

2015-08-09 Thread Timon Gehr via Digitalmars-d-learn
On 08/09/2015 10:13 PM, ixid wrote: This seems like a reasonable use but errors, obviously I can do it in many other ways: ulong[] result = iota(1UL, 10UL).array; Error: static assert Argument types in (ulong) are not all convertible to size_t: (ulong)

Re: cannot implicitly convert char[] to string

2015-08-15 Thread Timon Gehr via Digitalmars-d-learn
On 08/15/2015 01:54 PM, Timon Gehr wrote: On 08/15/2015 01:25 PM, vladde wrote: I made a PR to phobos where I modified `std.format.format`. https://github.com/D-Programming-Language/phobos/pull/3528 However the auto builder fails, with the error message: runnable/test23.d(1219): Error: cannot

Re: cannot implicitly convert char[] to string

2015-08-15 Thread Timon Gehr via Digitalmars-d-learn
On 08/15/2015 01:25 PM, vladde wrote: I made a PR to phobos where I modified `std.format.format`. https://github.com/D-Programming-Language/phobos/pull/3528 However the auto builder fails, with the error message: runnable/test23.d(1219): Error: cannot implicitly convert expression (format(s =

Re: Passing struct and struct[] into a template

2015-07-21 Thread Timon Gehr via Digitalmars-d-learn
On 07/22/2015 06:29 AM, Taylor Gronka wrote: Hi, I have a template function, and I want it to do something if the input variable is a list of structs, and something else if the input is a struct. 1) What's the best way to test this? I suppose I can call __traits(identifier, results) and look

Re: Fixed Length Array Syntax in extern(C) Function Signatures

2015-07-09 Thread Timon Gehr via Digitalmars-d-learn
On 07/09/2015 05:19 PM, Nordlöw wrote: Given extern(C): struct AVFrame { uint8_t*[AV_NUM_DATA_POINTERS] data; int[AV_NUM_DATA_POINTERS] linesize; int width, height; ... } void av_image_copy(ubyte *[4] dst_data, int[4] dst_linesizes,

Re: Should this compile?

2015-08-26 Thread Timon Gehr via Digitalmars-d-learn
On 08/25/2015 08:29 PM, Vladimir Panteleev wrote: I think this is a bug, but is easily worked around with: auto test(string a) { return .test(a, b); } I suspect that the reason the error occurs, is that the auto return type automatically rewrites the function declaration into an

Re: Should this compile?

2015-08-26 Thread Timon Gehr via Digitalmars-d-learn
On 08/26/2015 09:55 PM, Timon Gehr wrote: On 08/25/2015 08:29 PM, Vladimir Panteleev wrote: I think this is a bug, but is easily worked around with: auto test(string a) { return .test(a, b); } I suspect that the reason the error occurs, is that the auto return type automatically

Re: stuck on opDiv / opBinary

2015-08-30 Thread Timon Gehr via Digitalmars-d-learn
On 08/30/2015 07:02 PM, Spacen Jasset wrote: I have just added an opDiv to this class, but it doesn't seem to pick it up. math/vector.d(30): Error: 'this /= mag' is not a scalar, it is a Vector3 I can't see why that is, becuase my opMul works in the same place. Can anyone point out what I have

Re: Superfluous code in switch statement

2015-09-04 Thread Timon Gehr via Digitalmars-d-learn
On 09/04/2015 09:39 PM, Paul wrote: I discovered the other day (during a cut and paste malfunction!) that it's possible to have code before the first case in a switch. Google tells me that it's legal C code and something I read said it could be used for initialization but was rather vague. void

Re: Superfluous code in switch statement

2015-09-04 Thread Timon Gehr via Digitalmars-d-learn
On 09/04/2015 11:12 PM, anonymous wrote: On Friday 04 September 2015 23:04, Timon Gehr wrote: DMD never warns about dead code. It warns here: import std.stdio; void main() { return; writeln("hi"); /* Warning: statement is not reachable */ } You are right, it does.

Re: bug? for(int i=0;i<1;) vs while(true)

2015-09-17 Thread Timon Gehr via Digitalmars-d-learn
On 09/17/2015 09:47 PM, ddos wrote: yeah i tried for(;;) and it generates the same warning :) sure, here is the full example, it's not too long anyways ( the example doesn't make much sense tho because socket.accept is blocking :P ) http://pastebin.com/9K0wRRD6 ps: pastebin needs D support :-D

Re: operator overload for enum

2016-06-10 Thread Timon Gehr via Digitalmars-d-learn
On 10.06.2016 13:02, Satoshi wrote: Hello, why operator overloading is not working as a static methods through the UFCS? ... It's an arbitrary limitation. https://issues.dlang.org/show_bug.cgi?id=8062 (The specification has been updated in the meantime, it now documents the limitation

Re: Operator overloading through UFCS doesn't work

2016-05-26 Thread Timon Gehr via Digitalmars-d-learn
On 25.05.2016 01:19, Elie Morisse wrote: On Saturday, 13 October 2012 at 22:58:56 UTC, Timon Gehr wrote: Afaik free-function operator overloads (but not in the context of UFCS) were considered and turned down because D did not want to get amidst discussions about adding Koenig lookup. UFCS does

Re: Why aren't overloaded nested functions allowed?

2016-05-31 Thread Timon Gehr via Digitalmars-d-learn
On 30.05.2016 18:22, Max Samukha wrote: From the spec (https://dlang.org/spec/function.html#nested): "Nested functions cannot be overloaded." Anybody knows what's the rationale? The rationale is that nobody has implemented it in DMD. https://issues.dlang.org/show_bug.cgi?id=12578

Re: Variadic function with parameters all of a specific type

2016-06-17 Thread Timon Gehr via Digitalmars-d-learn
On 17.06.2016 23:00, Nordlöw wrote: I want to create a function that takes a variadic number of arguments all of a specific type, say T, without having to create GC-allocated heap array. Is there a better way than: f(Args...)(Args args) if (allSameType!(Args, T); in terms of template

Re: Functions that return type

2016-01-16 Thread Timon Gehr via Digitalmars-d-learn
On 01/16/2016 11:50 PM, data pulverizer wrote: I guess the constraints are that of a static language. (This is not true.)

Re: Why this code can't take advantage from CTFE?

2016-02-03 Thread Timon Gehr via Digitalmars-d-learn
On 02/03/2016 11:39 PM, Andrea Fontana wrote: On Wednesday, 3 February 2016 at 17:49:39 UTC, Marc Schütz wrote: On Wednesday, 3 February 2016 at 16:07:59 UTC, Messenger wrote: What is a good way to try to force it? Using enum? Then optionally copying the value once to avoid the "manifest

Re: Functions that return type

2016-01-19 Thread Timon Gehr via Digitalmars-d-learn
On 01/17/2016 08:09 PM, data pulverizer wrote: On Sunday, 17 January 2016 at 02:08:06 UTC, Timon Gehr wrote: On 01/16/2016 11:50 PM, data pulverizer wrote: I guess the constraints are that of a static language. (This is not true.) Could you please explain? E.g., a few of the systems

Re: Ada-Style Modulo Integer Types

2016-04-22 Thread Timon Gehr via Digitalmars-d-learn
On 22.04.2016 21:52, Nordlöw wrote: On Friday, 22 April 2016 at 17:37:44 UTC, Nordlöw wrote: Have anybody implement Ada-style modulo types https://en.wikibooks.org/wiki/Ada_Programming/Types/mod Here's my first try https://github.com/nordlow/phobos-next/blob/master/src/modulo.d Is there a

Re: Combining "chunkBy" and "until" algorithms

2016-11-05 Thread Timon Gehr via Digitalmars-d-learn
On 04.11.2016 09:04, Jacob Carlborg wrote: I have a file with a bunch of lines I want to process. I want to process these lines line by line. Most of these lines have the same pattern. Some of the lines have a different pattern. I want to bundle those lines, which have a non-standard pattern,

Re: DRY version of `static if(__traits(compiles, expr)) fun(expr)`

2016-12-13 Thread Timon Gehr via Digitalmars-d-learn
On 14.12.2016 00:00, Timothee Cour via Digitalmars-d-learn wrote: what's the best (and DRY) way to achieve: ``` static if(__traits(compiles, expr)) fun(expr); ``` ie, without repeating the expression inside expr? eg: ``` static if(__traits(compiles, foo.bar[2])){ counter++; writeln("

Re: DRY version of `static if(__traits(compiles, expr)) fun(expr)`

2016-12-15 Thread Timon Gehr via Digitalmars-d-learn
On 15.12.2016 01:38, Basile B. wrote: On Wednesday, 14 December 2016 at 22:06:35 UTC, Ali Çehreli wrote: On 12/14/2016 09:25 AM, Basile B. wrote: > On Tuesday, 13 December 2016 at 23:37:59 UTC, Timon Gehr wrote: >> I usually do >> >> enum code = q{expr}; >> static

Re: Primality test function doesn't work on large numbers?

2017-01-12 Thread Timon Gehr via Digitalmars-d-learn
On 10.01.2017 04:02, Elronnd wrote: Thank you! Would you mind telling me what you changed aside from pow() and powm()? 1. This code: // make 2^a = integer-1 while ((integer-1)%(pow(bigint(2), a))!=0) a--; m = (integer-1) / pow(bigint(2), a); a starts out as integer-1, so this computes

Re: Delegates: Print 0..9

2016-12-02 Thread Timon Gehr via Digitalmars-d-learn
On 01.12.2016 21:12, Ali Çehreli wrote: This is a common issue with D and some other languages (as I had learned during a Dart language presentation, of which Dart does not suffer from). All those delegates do close on the same loop variable. You need to produce copies of the variable. This

Re: Primality test function doesn't work on large numbers?

2017-01-08 Thread Timon Gehr via Digitalmars-d-learn
On 08.01.2017 08:52, Elronnd wrote: I'm working on writing an RSA implementation, but I've run into a roadblock generating primes. With a more than 9 bits, my program either hangs for a long time (utilizing %100 CPU!) or returns a composite number. With 9 or fewer bits, I get primes, but I

Re: It makes me sick!

2017-07-29 Thread Timon Gehr via Digitalmars-d-learn
On 29.07.2017 23:52, FoxyBrown wrote: On Saturday, 29 July 2017 at 21:48:09 UTC, Timon Gehr wrote: On 28.07.2017 23:30, FoxyBrown wrote: because you didn't want to spend 10 minutes to fix a program. You need to realize that the same thing applies to you. There is no "us" vs "you". I.e. if

Re: It makes me sick!

2017-07-29 Thread Timon Gehr via Digitalmars-d-learn
On 28.07.2017 23:30, FoxyBrown wrote: because you didn't want to spend 10 minutes to fix a program. You need to realize that the same thing applies to you. There is no "us" vs "you". I.e. if you know it to only be 10 minutes of work, why don't you just fix it yourself? Mike currently has as

Re: Cannot use std.array.Appender in recursive types

2017-08-09 Thread Timon Gehr via Digitalmars-d-learn
On 09.08.2017 21:00, Steven Schveighoffer wrote: On 8/9/17 2:25 PM, Nordlöw wrote: Why doesn't appending to `subs` work with std.array.Appender in struct T { string src; import std.array : Appender; Appender!(T[]) subs; } T t; t.subs ~=

Re: delegates/lambas do not pick up calling convention

2017-08-10 Thread Timon Gehr via Digitalmars-d-learn
On 10.08.2017 15:22, Adam D. Ruppe wrote: On Wednesday, 9 August 2017 at 23:52:00 UTC, Johnson Jones wrote: extern(C) delegate(void*) {} You should very rarely use extern(C) delegate... delegate is a D type, so the C function is almost certainly not actually receiving it. Only time you'd

Re: delegates/lambas do not pick up calling convention

2017-08-10 Thread Timon Gehr via Digitalmars-d-learn
On 10.08.2017 01:52, Johnson Jones wrote: given somethign like Threads.threadsAddIdle which takes an extern(C) int (void*) we can't seem to do threadsAddIdle((void*) { }, null); I think this is a compiler bug. Try: threadsAddIdle((x){ }, null); It seems that the calling convention is

Re: Avoid if statements for checking neighboring indexes in a 2D array

2017-07-16 Thread Timon Gehr via Digitalmars-d-learn
On 16.07.2017 19:10, Timon Gehr wrote: ... (This works even if there are * at the border.) Well, not really. :) Version that actually works if there are * at the border: import std.stdio, std.range, std.algorithm, std.array; char[][] arr; int componentSize(int row,int col){

Re: Avoid if statements for checking neighboring indexes in a 2D array

2017-07-16 Thread Timon Gehr via Digitalmars-d-learn
On 16.07.2017 12:37, kerdemdemir wrote: My goal is to find connected components in a 2D array for example finding connected '*' chars below. x x x x x x x x x x x x x x * * x x x x * * x x x x x * * x * x x x x x There are two connected '*' group in

Re: Avoid if statements for checking neighboring indexes in a 2D array

2017-07-16 Thread Timon Gehr via Digitalmars-d-learn
On 16.07.2017 18:55, Timon Gehr wrote: On 16.07.2017 12:37, kerdemdemir wrote: My goal is to find connected components in a 2D array for example finding connected '*' chars below. x x x x x x x x x x x x x x * * x x x x * * x x x x x * * x * x x x x x

Re: The Nullity Of strings and Its Meaning

2017-07-08 Thread Timon Gehr via Digitalmars-d-learn
On 08.07.2017 19:16, kdevel wrote: I wonder if this distinction is meaningful Not nearly as much as it would need to be to justify the current behavior. It's mostly a historical accident. and---if not---why it is exposed to the application programmer so prominently. I don't think there

Re: Best syntax for a diagonal and vertical slice

2017-07-24 Thread Timon Gehr via Digitalmars-d-learn
On 22.07.2017 22:55, kerdemdemir wrote: We have awesome way for creating slices like: a = new int[5]; int[] b = a[0..2]; But what about if I have 2D array and I don't want to go vertical. Something like : int[3][3] matrix = [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ];

Re: Why D have two function contains and canFind?

2017-07-24 Thread Timon Gehr via Digitalmars-d-learn
On 24.07.2017 20:19, Suliman wrote: Why D have two function `contains` and `canFind` `contains` guarantees logarithmic running time, while `canFind` can be linear.

Re: .sort vs sort(): std.algorithm not up to the task?

2017-06-08 Thread Timon Gehr via Digitalmars-d-learn
On 08.06.2017 14:06, Steven Schveighoffer wrote: The issue here is that arrays are special. Arrays allow foreach(i, v; arr) and foreach(v; arr). Ranges in general do not. So there is no way to forward this capability via the range interface. Not only that, but foreach(i, v; arr) is much

[OT] Converting booleans to numbers

2017-09-20 Thread Timon Gehr via Digitalmars-d-learn
On 19.09.2017 23:17, nkm1 wrote: ... OTOH, booleans converting to numbers is a very questionable feature. > I certainly have never seen any good use for it. ... Actually, it is useful enough to have a Wikipedia page: https://en.wikipedia.org/wiki/Iverson_bracket Example of a good use: void

Re: [OT] Converting booleans to numbers

2017-09-21 Thread Timon Gehr via Digitalmars-d-learn
On 20.09.2017 23:13, nkm1 wrote: Example of a good use: void floodFill(dchar[][] data,dchar c,int i,int j) {     void dfs(int a, int b) { Example of a good use: void floodFill(dchar[][] data,dchar c,int i,int j) { void dfs(int a, int b) { if (a<0 || a >= data.length) return;

Re: [OT] Converting booleans to numbers

2017-09-21 Thread Timon Gehr via Digitalmars-d-learn
On 21.09.2017 17:53, Steven Schveighoffer wrote: On 9/21/17 11:48 AM, Steven Schveighoffer wrote: On 9/21/17 11:06 AM, Timon Gehr wrote:     foreach(i; 0 .. 4){     dfs(a + (i==0) - (i==1),     b + (i==2) - (i==3));     } So am I, but I wasn't commenting on

Re: Assert and undefined behavior

2017-10-15 Thread Timon Gehr via Digitalmars-d-learn
On 14.10.2017 23:36, kdevel wrote: On Saturday, 14 October 2017 at 09:32:32 UTC, Timon Gehr wrote: Also, UB can and does sometimes mean that the program can execute arbitrary code. It's called "arbitrary code execution": https://en.wikipedia.org/wiki/Arbitrary_code_execution This confuses

Re: Assert and undefined behavior

2017-10-14 Thread Timon Gehr via Digitalmars-d-learn
On 14.10.2017 07:20, Jesse Phillips wrote: On Thursday, 12 October 2017 at 15:37:23 UTC, John Burton wrote: This is an example of what I mean :- undefined what it is meant to do anyway, so the compiler can "optimize" out the if condition as it only affects the case where the language

Re: Assert and undefined behavior

2017-10-12 Thread Timon Gehr via Digitalmars-d-learn
On 11.10.2017 11:27, John Burton wrote: The spec says this :- "As a contract, an assert represents a guarantee that the code must uphold. Any failure of this expression represents a logic error in the code that must be fixed in the source code. A program for which the assert contract is

Re: Why 2 ^^ 1 ^^ 2 = 2?

2017-10-22 Thread Timon Gehr via Digitalmars-d-learn
On 22.10.2017 16:20, Ilya Yaroshenko wrote: .. i thought it should be (2 ^^ 1) ^^ 2 = 4 2 ^^ (1 ^^ 2) == 2 It is standard for ^/**/^^ to be right-associative. (This is also the standard convention in mathematics.)

Re: Is old style compile-time foreach redundant?

2018-01-09 Thread Timon Gehr via Digitalmars-d-learn
On 09.01.2018 22:04, H. S. Teoh wrote: if (0 == 3) {} // all subsequent iterations deleted because the static break is unconditionally compiled (it has nothing to do with the runtime branch). You'd have to use static if to make it conditionally-compiled and thus not instantly

Re: How to imporve D-translation of these Python list comprehensions ?

2018-01-15 Thread Timon Gehr via Digitalmars-d-learn
On 15.01.2018 20:05, xenon325 wrote: I think, most clear code would be with tripple `foreach`, so I'll go with that. But probably someone will come up with something better and range-ier. Suggestion are welcome! import std.stdio, std.algorithm, std.range, std.array, std.conv, std.json,

Re: How to imporve D-translation of these Python list comprehensions ?

2018-01-15 Thread Timon Gehr via Digitalmars-d-learn
On 15.01.2018 22:51, Timon Gehr wrote: auto aa(R)(R r){     typeof(r.front[1])[typeof(r.front[0])] a;     foreach(x;r) a[x[0]] = x[1];     return a; } Actually, better to use std.array.assocArray. import std.stdio, std.algorithm, std.range, std.array, std.conv, std.json, std.typecons;

Re: Templated Binary Search Tree treats class as const, compiler complains

2018-01-21 Thread Timon Gehr via Digitalmars-d-learn
On 21.01.2018 21:20, Mark wrote: Just realized that I commented out the creation of the BST new link: https://dpaste.dzfl.pl/ce620cbee919 'in' means 'const scope', but it seems you need references that are allowed to mutate the incoming items. Remove the 'in' attribute from the parameters

Re: Negative index range violation

2018-02-22 Thread Timon Gehr via Digitalmars-d-learn
On 22.02.2018 01:26, Adam D. Ruppe wrote: On Thursday, 22 February 2018 at 00:13:43 UTC, SrMordred wrote: string x = "123"; auto c = x.ptr; c++; writeln(c[-1]); // 1 That's only happening because pointers bypass range checks. writeln(c[-1..0]); //BOOM Range violation But with a slice

Re: foreach on a tuple using aliases

2018-08-05 Thread Timon Gehr via Digitalmars-d-learn
On 05.08.2018 16:07, Steven Schveighoffer wrote: I have found something that looks like a bug to me, but also looks like it could simply be a limitation of the foreach construct. Consider this code: struct Foo {} enum isFoo(alias x) = is(typeof(x) == Foo); void main() {     Foo foo;    

Re: foreach on a tuple using aliases

2018-08-06 Thread Timon Gehr via Digitalmars-d-learn
On 06.08.2018 14:37, Steven Schveighoffer wrote: On 8/5/18 11:40 AM, Timon Gehr wrote: On 05.08.2018 16:07, Steven Schveighoffer wrote: So is this a bug? Is it expected? It's a bug. The two copies of 'item' are not supposed to be the same symbol. (Different types -> different symbols.)

Re: dynamically allocating on the stack

2018-04-21 Thread Timon Gehr via Digitalmars-d-learn
On 21.04.2018 12:08, Giles Bathgate wrote: On Saturday, 21 April 2018 at 07:57:41 UTC, Uknown wrote: The language itself doesn't have something, but you could use `alloca` I don't know if this little template function makes life easier: -- pragma(inline, true) ref T push(T)(size_t len) {

Re: Why does nobody seem to think that `null` is a serious problem in D?

2018-12-03 Thread Timon Gehr via Digitalmars-d-learn
On 22.11.18 16:19, Steven Schveighoffer wrote: In terms of language semantics, I don't know what the right answer is. If we want to say that if an optimizer changes program behavior, the code must be UB, then this would have to be UB. But I would prefer saying something like -- if a

Re: Strange closure behaviour

2019-06-15 Thread Timon Gehr via Digitalmars-d-learn
On 15.06.19 18:29, Rémy Mouëza wrote: On Saturday, 15 June 2019 at 01:21:46 UTC, Emmanuelle wrote: On Saturday, 15 June 2019 at 00:30:43 UTC, Adam D. Ruppe wrote: On Saturday, 15 June 2019 at 00:24:52 UTC, Emmanuelle wrote: Is it a compiler bug? Yup, a very longstanding bug. You can work

Re: Performance of tables slower than built in?

2019-05-23 Thread Timon Gehr via Digitalmars-d-learn
On 23.05.19 12:21, Alex wrote: On Wednesday, 22 May 2019 at 00:55:37 UTC, Adam D. Ruppe wrote: On Wednesday, 22 May 2019 at 00:22:09 UTC, JS wrote: I am trying to create some fast sin, sinc, and exponential routines to speed up some code by using tables... but it seems it's slower than the

Re: CTFE sort of tuples

2019-05-03 Thread Timon Gehr via Digitalmars-d-learn
On 02.05.19 09:28, Stefan Koch wrote: On Thursday, 2 May 2019 at 02:54:03 UTC, Andrey wrote: Hello, I have got this code:     [...] I want to sort array of tuples using "data" element in CTFE. But this code give me errors: [...] As I understand the function "sort" sometimes can't be run

Re: Abstract classes vs interfaces, casting from void*

2019-08-10 Thread Timon Gehr via Digitalmars-d-learn
On 10.08.19 16:29, John Colvin wrote: Ok. What would go wrong (in D) if I just replaced every interface with an abstract class? interface A{} interface B{} class C: A,B{ }

Re: accuracy of floating point calculations: d vs cpp

2019-07-22 Thread Timon Gehr via Digitalmars-d-learn
On 22.07.19 14:49, drug wrote: I have almost identical (I believe it at least) implementation (D and C++) of the same algorithm that uses Kalman filtering. These implementations though show different results (least significant digits). Before I start investigating I would like to ask if this

Re: How to invert bool false/true in alias compose?

2019-12-06 Thread Timon Gehr via Digitalmars-d-learn
On 07.12.19 05:00, Marcone wrote: import std; alias cmd = compose!(to!bool, wait, spawnShell, to!string); void main(){ writeln(cmd("where notepad.exe")); } Result: C:\Windows\System32\notepad.exe C:\Windows\notepad.exe false The result show "false" because good spawnshell command

  1   2   >