Re: std.file.read returns void[] why?

2014-04-17 Thread monarch_dodra via Digitalmars-d-learn
On Thursday, 17 April 2014 at 12:59:20 UTC, Steven Schveighoffer wrote: It was never possible. You must explicitly cast to void[]. void[] makes actually little sense as the result of whole-file read that allocates. byte[] is at least usable and more accurate. In fact, it's a little dangerous

Re: std.file.read returns void[] why?

2014-04-18 Thread monarch_dodra via Digitalmars-d-learn
On Thursday, 17 April 2014 at 21:27:44 UTC, Steven Schveighoffer wrote: On Thu, 17 Apr 2014 17:04:25 -0400, monarch_dodra monarchdo...@gmail.com wrote: void[] will only make sense once you've accepted that void.sizeof == 1. It is already accepted that when we talk about length in a void[],

Re: std.file.read returns void[] why?

2014-04-18 Thread monarch_dodra via Digitalmars-d-learn
On Friday, 18 April 2014 at 13:08:04 UTC, Steven Schveighoffer wrote: I admit, I didn't think C's void had a size ;) I'm pretty sure it doesn't in D, but then again... -Steve Yeah... static assert(void.sizeof == 1); passes :/ So in any case, long story short: void[]: This is an un-typed

Re: Reducing an array

2014-04-18 Thread monarch_dodra via Digitalmars-d-learn
On Friday, 18 April 2014 at 20:27:20 UTC, Steven Schveighoffer wrote: Note also that what it returns is not an array, but a lazily iterated range over the array. If you want another array, this is the code I would use: arr.sort(); arr = arr.uniq.array(); -Steve Out of curiosity, if the

Re: Reducing an array

2014-04-19 Thread monarch_dodra via Digitalmars-d-learn
On Friday, 18 April 2014 at 22:11:17 UTC, bearophile wrote: monarch_dodra: Out of curiosity, if the requirement was to *also* preserve ordering (eg: remove all non-first elements), how would you go at it? [2, 1, 1, 3, 2, 3] = [2, 1, 3]; Maybe std.algorithm's `makeIndex` would help here?

Re: Reducing an array

2014-04-19 Thread monarch_dodra via Digitalmars-d-learn
On Friday, 18 April 2014 at 22:11:17 UTC, bearophile wrote: This preserves ordering and it's in-place. Not tested much: void main() { import std.stdio, std.traits; auto data = [2, 1, 1, 3, 2, 3]; bool[ForeachType!(typeof(data))] seen; size_t pos = 0; foreach (immutable i;

Re: Template method and type resolution of return type

2014-04-20 Thread monarch_dodra via Digitalmars-d-learn
On Sunday, 20 April 2014 at 07:52:08 UTC, matovitch wrote: struct S { ubyte get() { return 0 ; } float get() { return 0.; } } void main() { S s; float x = s.get(); // does'nt know which overload, does'nt compile. } What I do find interesting though, is that you are allowed

Re: number formatting

2014-04-20 Thread monarch_dodra via Digitalmars-d-learn
On Sunday, 20 April 2014 at 12:53:11 UTC, steven kladitis wrote: Note sure if you can edit messages once sent. $13,456.67 245,678,541 On Sunday, 20 April 2014 at 12:50:52 UTC, steven kladitis wrote: How do you format numbers to have things like. Leading $ or , or CR with or without leading

Re: string - string literal

2014-04-20 Thread monarch_dodra via Digitalmars-d-learn
On Sunday, 20 April 2014 at 17:55:25 UTC, Ellery Newcomer wrote: is there a function in phobos anywhere that takes a string and escapes it into a string literal suitable for string mixins? something like assert (f(abc\ndef) == \abc\\ndef\); It's a bit hackish, but it avoids deploying code

Re: Function to print a diamond shape

2014-04-21 Thread monarch_dodra via Digitalmars-d-learn
On Monday, 21 April 2014 at 00:11:14 UTC, Jay Norwood wrote: So this printDiamonde2b example had the fastest time of the solutions, and had similar times on all three builds. The ldc2 compiler build is performing best in most examples on ubuntu. void printDiamonde2b(in uint N) { uint N2 =

Re: Named template constraints

2014-04-22 Thread monarch_dodra via Digitalmars-d-learn
On Tuesday, 22 April 2014 at 15:06:34 UTC, Steven Schveighoffer wrote: Note, is the r2 = R.init needed? Not sure. Yes: It R2 has no default init, or is an immutable, then that line will fail to compile.

Re: Function to print a diamond shape

2014-04-22 Thread monarch_dodra via Digitalmars-d-learn
On Tuesday, 22 April 2014 at 05:05:30 UTC, Jay Norwood wrote: On Monday, 21 April 2014 at 08:26:49 UTC, monarch_dodra wrote: The two key points here, first, is to avoid using appender. Second, instead of having two buffer: and **\n, and two do two slice copies, to only have 1 buffer

Re: Function to print a diamond shape

2014-04-22 Thread monarch_dodra via Digitalmars-d-learn
On Tuesday, 22 April 2014 at 11:41:41 UTC, Jay Norwood wrote: Wow, joiner is much slower than join. Such a small choice can make this big of a difference. Not at all expected, since the lazy calls, I thought, were considered to be more efficient. This is with ldc2 -O2. Yeah, that's

Re: Named template constraints

2014-04-22 Thread monarch_dodra via Digitalmars-d-learn
On Tuesday, 22 April 2014 at 15:30:36 UTC, Steven Schveighoffer wrote: On Tue, 22 Apr 2014 11:15:14 -0400, monarch_dodra monarchdo...@gmail.com wrote: On Tuesday, 22 April 2014 at 15:06:34 UTC, Steven Schveighoffer wrote: Note, is the r2 = R.init needed? Not sure. Yes: It R2 has no default

Re: The lifetime of reduce's internal seed

2014-04-22 Thread monarch_dodra via Digitalmars-d-learn
On Tuesday, 22 April 2014 at 17:31:22 UTC, Ali Çehreli wrote: I opened the following bug before reading reduce's documentation carefully: https://issues.dlang.org/show_bug.cgi?id=12610 import std.stdio; import std.algorithm; void main() { int[] arr = [ 0 ]; int[1] seed; int[]

Re: The lifetime of reduce's internal seed

2014-04-22 Thread monarch_dodra via Digitalmars-d-learn
On Tuesday, 22 April 2014 at 18:34:47 UTC, Steven Schveighoffer wrote: On Tue, 22 Apr 2014 14:17:57 -0400, Ali Çehreli acehr...@yahoo.com wrote: I don't think there is slicing an rvalue though. (?) reduce() is taking a copy of the seed and then returning a slice to it because the user slices

Re: The lifetime of reduce's internal seed

2014-04-23 Thread monarch_dodra via Digitalmars-d-learn
On Tuesday, 22 April 2014 at 18:49:41 UTC, Steven Schveighoffer wrote: On Tue, 22 Apr 2014 14:47:19 -0400, monarch_dodra monarchdo...@gmail.com wrote: In this case no, but; // int[1] foo(); int[] a = foo(); // *is* slicing an rvalue, and it *does* compile. I don't think there needs

Re: Partial ordering of constructors with type parameters

2014-04-23 Thread monarch_dodra via Digitalmars-d-learn
On Wednesday, 23 April 2014 at 16:44:37 UTC, Charles McAnany wrote: Friends, I have a class that needs two constructors: class Foo{ this(int x){} this(T)(T x) if(!is(T == int)){} } void main(){} But this does not compile because the two constructors conflict: buggy.d(3): Error:

Re: Partial ordering of constructors with type parameters

2014-04-24 Thread monarch_dodra via Digitalmars-d-learn
On Wednesday, 23 April 2014 at 22:07:32 UTC, John Colvin wrote: On Wednesday, 23 April 2014 at 18:04:05 UTC, monarch_dodra wrote: If you can't update your compiler, an alternative is to make your non-template version an actual template: class Foo{ this(T : int)(T x){} this(T)(T x) {} }

Re: Partial ordering of constructors with type parameters

2014-04-24 Thread monarch_dodra via Digitalmars-d-learn
On Thursday, 24 April 2014 at 10:12:15 UTC, Andrej Mitrovic via Digitalmars-d-learn wrote: On 4/24/14, monarch_dodra via Digitalmars-d-learn digitalmars-d-learn@puremagic.com wrote: *That* creates a conflict though :/ Are you sure? I can't reproduce. Weird. I can't either. I probably

Re: The lifetime of reduce's internal seed

2014-04-26 Thread monarch_dodra via Digitalmars-d-learn
On Saturday, 26 April 2014 at 06:24:26 UTC, Ali Çehreli wrote: On 04/22/2014 11:45 AM, monarch_dodra wrote: Reduce returns the seed. It's actually doing something more like this: int[1] foo() { int[1] sum sum = sum[]; //The lambda operates, and the //result is

Re: Can I circumvent nothrow?

2014-04-27 Thread monarch_dodra via Digitalmars-d-learn
On Sunday, 27 April 2014 at 08:04:54 UTC, Andrej Mitrovic via Digitalmars-d-learn wrote: On 4/27/14, Damian Day via Digitalmars-d-learn digitalmars-d-learn@puremagic.com wrote: So I have this procedure. Have a look at std.exception.assumeWontThrow:

Re: Trailing commas in constructor arguments?

2014-05-04 Thread monarch_dodra via Digitalmars-d-learn
On Sunday, 4 May 2014 at 10:04:26 UTC, Gary Willoughby wrote: In the following snippet is the line marked WOAH legal? The compiler doesn't complain about the trailing comma in the constructor arguments. import std.stdio; class Foo { public this(string foo) { } } void

Re: Is this a bug?

2014-05-04 Thread monarch_dodra via Digitalmars-d-learn
On Sunday, 4 May 2014 at 09:42:17 UTC, Alex wrote: Hello, I am trying to use the std.log module that is here: https://github.com/linkrope/log.d And I encountered a segmentation fault using dmd 2.065 on a Linux 64 platform. The reduced test case is this:

Re: Is this a bug?

2014-05-04 Thread monarch_dodra via Digitalmars-d-learn
On Sunday, 4 May 2014 at 10:28:30 UTC, Mike Parker wrote: The current implementation of the GC will run destructors on any objects still resident on the heap during termination. There is no way to guarantee the order in which those destructors will be run. Most likely, what you're seeing is

Is my T.init elaborate (eg: Not 0)

2014-05-05 Thread monarch_dodra via Digitalmars-d-learn
I'm looking for a way, to *statically* know if my type's .init value is nothing but zeros. I need this to initialize a dynamic array: If the T.init is nothing but 0, then I can just memset the whole array in a single call. Otherwise, I have to memcpy T.init individually N times. Currently,

Re: Is my T.init elaborate (eg: Not 0)

2014-05-06 Thread monarch_dodra via Digitalmars-d-learn
On Monday, 5 May 2014 at 21:54:33 UTC, Tobias Pankrath wrote: On Monday, 5 May 2014 at 21:51:14 UTC, Tobias Pankrath wrote I'm trying to find the same result, without a runtime check. Anybody know how? I'd write a function that tests the bits of a T.init copy, but reinterpreting is not allowed

Re: Implicit static-dynamic arr and modifying

2014-05-07 Thread monarch_dodra via Digitalmars-d-learn
On Wednesday, 7 May 2014 at 20:09:22 UTC, H. S. Teoh via Digitalmars-d-learn wrote: On Wed, May 07, 2014 at 06:31:15PM +, Rene Zwanenburg via Digitalmars-d-learn wrote: On Wednesday, 7 May 2014 at 15:41:19 UTC, Nick Sabalausky wrote: On 5/6/2014 6:46 PM, Rene Zwanenburg wrote: [...]

Re: throws Exception in method

2014-05-08 Thread monarch_dodra via Digitalmars-d-learn
On Thursday, 8 May 2014 at 13:06:05 UTC, amehat wrote: Okay. Thank you for these explanations, I understand a little better the exceptions D. Keep in mind that D also has the concept of Error. Both Exception and Error derive from Throwable. nothrow only means the function will not throw an

Re: why can't I call const methods on shared objects?

2014-05-11 Thread monarch_dodra via Digitalmars-d-learn
On Friday, 9 May 2014 at 21:58:41 UTC, Steven Schveighoffer wrote: On Fri, 09 May 2014 17:45:37 -0400, Vlad Levenfeld vlevenf...@gmail.com wrote: Is there any way to declare a method as safe regardless of shared/mutability/etc (or some other way to avoid cast(Type)object.property every time

Re: Why std.algorithm.sort can't be applied to char[]?

2014-05-14 Thread monarch_dodra via Digitalmars-d-learn
On Monday, 12 May 2014 at 18:44:22 UTC, Jonathan M Davis via Digitalmars-d-learn wrote: Sure, you can cast char[] to ubyte[] and sort that if you know that the array only holds pure ASCII. In fact, you can use std.string.representation to do it - e.g. auto ascii = str.representation; and if

Re: Array!T and find are slow

2014-05-14 Thread monarch_dodra via Digitalmars-d-learn
On Wednesday, 14 May 2014 at 14:54:57 UTC, David Nadlinger wrote: On Wednesday, 14 May 2014 at 14:24:28 UTC, Damian Day wrote: I've written some search functions, which are many times faster, is it worth making a pull request? Generally, we strive to make the algorithms in Phobos as fast as

Re: Array!T and find are slow

2014-05-14 Thread monarch_dodra via Digitalmars-d-learn
On Wednesday, 14 May 2014 at 15:42:13 UTC, Damian Day wrote: On Wednesday, 14 May 2014 at 14:54:57 UTC, David Nadlinger wrote: Could you post a short benchmark snippet explicitly showing the problem? Benchmark found here: http://dpaste.dzfl.pl/0058fc8341830 FYI, that implementation is

Re: Array!T and find are slow

2014-05-14 Thread monarch_dodra via Digitalmars-d-learn
On Wednesday, 14 May 2014 at 14:24:28 UTC, Damian Day wrote: I've found bench-marking my program that std.algorithm.find is very slow on Array!T, due to the fact it iterates on a range instead of a plain array. I've written some search functions, which are many times faster, is it worth

Re: Array!T and find are slow

2014-05-14 Thread monarch_dodra via Digitalmars-d-learn
On Wednesday, 14 May 2014 at 20:29:52 UTC, David Nadlinger wrote: On Wednesday, 14 May 2014 at 17:36:35 UTC, monarch_dodra wrote: Adding a special case in Array.Range allows bypassing the repeated indexing costs, but at the very least, should be implemented in terms of find itself, eg:

Re: Array!T and find are slow

2014-05-14 Thread monarch_dodra via Digitalmars-d-learn
On Wednesday, 14 May 2014 at 21:20:06 UTC, Kapps wrote: That pull shows that the previous behaviour was to use enforce? Isn't this very expensive, particularly considering that enforce uses lazy non-scope arguments? Yes.

Re: Array!T and find are slow

2014-05-14 Thread monarch_dodra via Digitalmars-d-learn
On Thursday, 15 May 2014 at 04:37:16 UTC, Jonathan M Davis via Digitalmars-d-learn wrote: enforce(cond, failure); really should just translate to something close to if(!cond) throw new Exception(failure); but it doesn't do anything close to that. And as long as it doesn't, enforce is of

Re: Array!T and find are slow

2014-05-15 Thread monarch_dodra via Digitalmars-d-learn
On Thursday, 15 May 2014 at 06:52:44 UTC, Jonathan M Davis via Digitalmars-d-learn wrote: On Thu, 15 May 2014 05:53:45 + monarch_dodra via Digitalmars-d-learn digitalmars-d-learn@puremagic.com wrote: As a workaround, I'm sure we could specialize enforce without lazy for built-in types

Re: Why std.algorithm.sort can't be applied to char[]?

2014-05-15 Thread monarch_dodra via Digitalmars-d-learn
On Wednesday, 14 May 2014 at 09:01:23 UTC, John Colvin wrote: Why would anyone ever want to sort code-points? Why not? To remove duplicate characters? They might want to sort graphemes, but that's difficult to do in-place (needs O(n) memory, I think...). If out-of-place is good enough

Re: Why std.algorithm.sort can't be applied to char[]?

2014-05-15 Thread monarch_dodra via Digitalmars-d-learn
On Thursday, 15 May 2014 at 13:26:45 UTC, Steven Schveighoffer wrote: On Wed, 14 May 2014 05:13:42 -0400, Jonathan M Davis via Digitalmars-d-learn digitalmars-d-learn@puremagic.com wrote: On Wed, 14 May 2014 08:27:45 + monarch_dodra via Digitalmars-d-learn digitalmars-d-learn

Re: [Rosettacode] Growable slices

2014-05-15 Thread monarch_dodra via Digitalmars-d-learn
On Thursday, 15 May 2014 at 09:00:13 UTC, bearophile wrote: This task asks for an basic implementation of the the Lempel-Ziv-Welch (LZW) compression/decompression algorithm. I am keeping two D versions, the first one is minimal, and the second is a little more optimized:

Re: Why std.algorithm.sort can't be applied to char[]?

2014-05-15 Thread monarch_dodra via Digitalmars-d-learn
On Thursday, 15 May 2014 at 17:46:52 UTC, Steven Schveighoffer wrote: As far as I'm concerned, if we *can* do it in n.log(n), and somebody provides the implementation, then there is no reason to not offer dchar sorting for char[]/wchar. I think there is nothing wrong with requiring the steps

Re: [Rosettacode] Growable slices

2014-05-16 Thread monarch_dodra via Digitalmars-d-learn
On Friday, 16 May 2014 at 15:20:04 UTC, bearophile wrote: Artur Skawina: Ugh. So how does it perform wrt the D version that I wrote for you last time? [1] I have done a benchmark with the various version (the first 3 are the ones on the Rosettacode site, and the #4 is yours): lzw1: 0.39

Re: [Rosettacode] Growable slices

2014-05-16 Thread monarch_dodra via Digitalmars-d-learn
On Friday, 16 May 2014 at 15:49:10 UTC, bearophile wrote: monarch_dodra: Arguably, your code allocates a lot. What version (1-2-3) do you mean? Any of the versions where you can see [c] or [b] where c/b is a char/byte

Re: Is it possible to assumeSafeAppend malloced memory?

2014-05-19 Thread monarch_dodra via Digitalmars-d-learn
On Monday, 19 May 2014 at 06:08:18 UTC, Ali Çehreli wrote: We know that most of the time memory is allocated more than the requested amount. Is there a way to take advantage of that extra trailing space? (And potentially the pages that come after that.) import core.memory; void main() {

Re: Is it possible to assumeSafeAppend malloced memory?

2014-05-19 Thread monarch_dodra via Digitalmars-d-learn
On Monday, 19 May 2014 at 13:55:00 UTC, Steven Schveighoffer wrote: On Monday, 19 May 2014 at 06:08:18 UTC, Ali Çehreli wrote: This issue puts std.array.array to a disadvantage compared to proper slices because array() involves the following call chain, the last of which does call GC.malloc:

Re: Is it possible to assumeSafeAppend malloced memory?

2014-05-19 Thread monarch_dodra via Digitalmars-d-learn
On Monday, 19 May 2014 at 18:51:31 UTC, Dicebot wrote: If it still resorts to GC in this case, utility of such addition sounds questionable. It's not really an addition as much as it is a necessary building block to make higher order GC functions work: For example, dup was recently made a

Re: Question about @nogc

2014-05-20 Thread monarch_dodra via Digitalmars-d-learn
On Tuesday, 20 May 2014 at 12:25:11 UTC, Dominikus Dittes Scherkl wrote: Did I understand correct that a function can only be @nogc if also all functions that it calls are @nogc too (and of course it doesn't use the GC itself)? If so, should this be possible: string foo() { // use GC to

Re: byCodePoint for a range of chars

2014-05-20 Thread monarch_dodra via Digitalmars-d-learn
On Tuesday, 20 May 2014 at 17:59:09 UTC, John Colvin wrote: Given a range with element type char, what's the best way of iterating over it by code-point, without filling an array first? Related to this: What's the status of std.utf and std.encoding? The comments in std.encoding say that some

Re: byCodePoint for a range of chars

2014-05-20 Thread monarch_dodra via Digitalmars-d-learn
On Tuesday, 20 May 2014 at 18:06:09 UTC, Justin Whear wrote: Foreach on narrow strings automatically decodes, so it's as simple as: // assume UTF-8 encoded char[] myData = ... foreach (dchar codePoint; myData) ... I think the point of his question is if you have an actual non-array range

Re: Question about @nogc

2014-05-20 Thread monarch_dodra via Digitalmars-d-learn
On Tuesday, 20 May 2014 at 21:04:37 UTC, 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

Re: How to get struct's members ?

2014-05-23 Thread monarch_dodra via Digitalmars-d-learn
On Friday, 23 May 2014 at 01:17:18 UTC, bioinfornatics wrote: Dear, I would like to get struct's members and zip them with an action as struct A { int a; int b; } std.range.zip( __traits( allmembers, A ), [(x) = x == 0, (y) = y 3] ); like this i could apply an action to each field. I

Re: How to get struct's members ?

2014-05-23 Thread monarch_dodra via Digitalmars-d-learn
On Friday, 23 May 2014 at 08:20:05 UTC, Philippe Sigaud via Digitalmars-d-learn wrote: On Fri, May 23, 2014 at 8:44 AM, monarch_dodra via Digitalmars-d-learn digitalmars-d-learn@puremagic.com wrote: On Friday, 23 May 2014 at 01:17:18 UTC, bioinfornatics wrote: I would like to get struct's

Re: How to handle try-catch blocks, nothrow and logfiles

2014-05-24 Thread monarch_dodra via Digitalmars-d-learn
On Saturday, 24 May 2014 at 17:09:24 UTC, Tim wrote: Imagine I've an application where I want log all thrown exceptions. Arguably, that's not something you'd want to do. In a normal application, exceptions get thrown around, and it is completely normal. In particular, the *thrower* has no

Re: What are the best std algo for testing a range implementation ?

2014-05-27 Thread monarch_dodra via Digitalmars-d-learn
On Tuesday, 27 May 2014 at 10:50:54 UTC, BicMedium wrote: Let's say I have a set of containers, using a D-unfriendly-semantic. They rather use a kind of ADA vocabulary (according to https://en.wikipedia.org/wiki/Deque). I want to make them range-aware. If the input/output ranges are easy to

Re: What are the best std algo for testing a range implementation ?

2014-05-27 Thread monarch_dodra via Digitalmars-d-learn
On Tuesday, 27 May 2014 at 12:18:15 UTC, BicMedium wrote: I mean that those tests are just like testing an interface...). If your interface isn't complete, than it is irrelevant what your implementations are, since the algorithms can't use your ranges anyways. BTW the 2nd and the 3rd

Re: What are the best std algo for testing a range implementation ?

2014-05-27 Thread monarch_dodra via Digitalmars-d-learn
On Tuesday, 27 May 2014 at 16:49:42 UTC, BicMedium wrote: But there could be a templated-unittest for those kind of things...Ranges are relatively straightforward in to use, but when you want to implement one, it's another thing...So it's just about indexes ? And a kind of State machine for

Re: Cost of .dup vs. instantiation

2014-05-28 Thread monarch_dodra via Digitalmars-d-learn
On Wednesday, 28 May 2014 at 14:36:25 UTC, Chris wrote: I use Appender to fill an array. The Appender is a class variable and is not instantiated with each function call to save instantiation. However, the return value or the function must be dup'ed, like so: Appender!(MyType[]) append;

Re: std.algorithm range violation

2014-05-28 Thread monarch_dodra via Digitalmars-d-learn
On Wednesday, 28 May 2014 at 11:40:05 UTC, Wanderer wrote: Sorry about typo, I meant providor_symbol_map.sort!((x,y)={x.value.lengthy.value.length}) above. providor_symbol_map is an Associative Array, so you can't sort that. *Usually*, you want to do what the OP did, which is to get the

Re: std.algorithm range violation

2014-05-28 Thread monarch_dodra via Digitalmars-d-learn
On Wednesday, 28 May 2014 at 17:39:15 UTC, monarch_dodra wrote: On Wednesday, 28 May 2014 at 11:40:05 UTC, Wanderer wrote: Sorry about typo, I meant providor_symbol_map.sort!((x,y)={x.value.lengthy.value.length}) above. providor_symbol_map is an Associative Array, so you can't sort that.

Re: @safe @nogc memory allocation

2014-05-28 Thread monarch_dodra via Digitalmars-d-learn
On Wednesday, 28 May 2014 at 19:43:53 UTC, Nordlöw wrote: I would like my radix sort function radixSortImpl() at https://github.com/nordlow/justd/blob/master/intsort.d to not use the GC. However, when I tag with @nogc I get the error: intsort.d(195,47): Error: @nogc function

Re: @safe @nogc memory allocation

2014-05-28 Thread monarch_dodra via Digitalmars-d-learn
On Wednesday, 28 May 2014 at 20:00:17 UTC, safety0ff wrote: I think malloc isn't @safe and alloca doesn't work if your function can throw. Yeah, uninitializedArray is also *only* trusted if the type in question has no indirections. I've heard of several bugs with alloca, but I don't know

Re: Cost of .dup vs. instantiation

2014-05-29 Thread monarch_dodra via Digitalmars-d-learn
On Thursday, 29 May 2014 at 08:49:10 UTC, Chris wrote: monarch_dodra: Hm. This last point might be an issue. If I process a large input (text in this case) then I might run into trouble with append as a class variable. I also had a weird bug, because I didn't clear the memory for overwrite.

Re: enums

2014-05-30 Thread monarch_dodra via Digitalmars-d-learn
On Friday, 30 May 2014 at 15:30:15 UTC, Russel Winder via Digitalmars-d-learn wrote: I think I have no idea what D enums are about. Bearophile's example of some code in an email on another thread uses: enum double p0 = 0.0045; Now I would have written: immutable double p0 =

Re: Different random shuffles generated when compiled with gdc than with dmd

2014-05-30 Thread monarch_dodra via Digitalmars-d-learn
On Friday, 30 May 2014 at 13:39:18 UTC, Andrew Brown wrote: Hi there, The following code: void main(){ import std.array : array; import std.stdio : writeln; import std.random : rndGen, randomShuffle; import std.range : iota; rndGen.seed(12); int[] temp = iota(10).array;

Re: Different random shuffles generated when compiled with gdc than with dmd

2014-05-30 Thread monarch_dodra via Digitalmars-d-learn
On Friday, 30 May 2014 at 18:41:55 UTC, Joseph Rushton Wakeling via Digitalmars-d-learn wrote: On 30/05/14 18:13, monarch_dodra via Digitalmars-d-learn wrote: Are you sure you are compiling with the same version of dmd and gdc? Fixes were made to the rand.d library in the latest release, which

Re: enums

2014-05-31 Thread monarch_dodra via Digitalmars-d-learn
On Saturday, 31 May 2014 at 21:21:59 UTC, Paul D Anderson wrote: 'enum' as a manifest constant keyword has been an unpopular decision from its introduction. Everybody agrees that it should be changed. Everybody but Walter I find enum makes sense.

Re: How to sort a 2D array by column

2014-06-07 Thread monarch_dodra via Digitalmars-d-learn
On Saturday, 7 June 2014 at 20:47:31 UTC, katuday wrote: On Saturday, 7 June 2014 at 19:18:34 UTC, Chris Cain wrote: On Saturday, 7 June 2014 at 19:14:01 UTC, Chris Cain wrote: This is my attemot to create a compare object. But I don't know how to use it together with .sort member function

Re: Conversion string-int

2014-06-07 Thread monarch_dodra via Digitalmars-d-learn
On Saturday, 7 June 2014 at 20:53:03 UTC, Paul wrote: I can not understand, why this code works: char s[2] = ['0', 'A']; string ss = to!string(s); writeln(parse!uint(ss, 16)); but this can deduces template: char s[2] = ['0', 'A']; writeln(parse!uint(to!string(s), 16));

Re: array as parameter

2014-06-07 Thread monarch_dodra via Digitalmars-d-learn
On Saturday, 7 June 2014 at 20:56:14 UTC, Paul wrote: Dynamic array is really reference. Right? But why modification of parameter in this case does not work: void some_func(string[] s) { s ~= xxx; s ~= yyy; } but this works: void some_fun(ref string[] s) { s ~= xxx; s ~= yyy; } In the 1st

Re: Permutation Sort Algorithm Very Slow

2014-06-08 Thread monarch_dodra via Digitalmars-d-learn
On Saturday, 7 June 2014 at 22:01:25 UTC, Ali GOREN wrote: Thank you. I can not resolve it in quicker time, right? Depends what exactly you want to achieve. This will achieve the same result in a fraction of the time. void main() { auto data = [2, 7, 4, 3, 5, 1, 0, 9, 8, 6, -1];

Re: Interesting bug with std.random.uniform and dchar

2014-06-08 Thread monarch_dodra via Digitalmars-d-learn
On Sunday, 8 June 2014 at 08:54:30 UTC, Joseph Rushton Wakeling via Digitalmars-d-learn wrote: I think it should suffice to forbid uniform!T from accepting dchar parameters and to tweak the integral-type uniform()'s internal check to avoid calling that specialization with dchar. Thoughts ...

Re: Interesting bug with std.random.uniform and dchar

2014-06-08 Thread monarch_dodra via Digitalmars-d-learn
On Sunday, 8 June 2014 at 13:55:48 UTC, H. S. Teoh via Digitalmars-d-learn wrote: On Sun, Jun 08, 2014 at 11:17:41AM +0200, Joseph Rushton Wakeling via Digitalmars-d-learn wrote: On 08/06/14 11:02, monarch_dodra via Digitalmars-d-learn wrote: Why would we ban uniform!T from accepting dchar? I

Re: scope exit in mixin template

2014-06-08 Thread monarch_dodra via Digitalmars-d-learn
On Sunday, 8 June 2014 at 18:28:25 UTC, Byron wrote: Can we not use scope(..) in a mixin template? struct bar {} bar* c_make() { return new bar(); } void c_free(bar* b) { b = null; } mixin template Foo() { auto b = c_make; scope(exit) if(b) c_free(b); } void main() { mixin Foo; } I

Re: scope exit in mixin template

2014-06-08 Thread monarch_dodra via Digitalmars-d-learn
On Sunday, 8 June 2014 at 18:48:03 UTC, monarch_dodra wrote: Mixin templates can only insert declarations, not arbitrary code. When it sees scope, it's expecting it to be the attribute, not the declaration. To add to that, if you want to mixin arbitrary code, then you can use a string mixin:

Re: scope exit in mixin template

2014-06-08 Thread monarch_dodra via Digitalmars-d-learn
On Sunday, 8 June 2014 at 18:28:25 UTC, Byron wrote: void c_free(bar* b) { b = null; } Heads up: This code does nothing. You are passing the pointer by value, so b = null; will have no effect at the end of the call. Use pass by ref: void c_free(ref bar* b) { b = null; }

Re: splitter for strings

2014-06-09 Thread monarch_dodra via Digitalmars-d-learn
On Monday, 9 June 2014 at 10:23:16 UTC, Chris wrote: Ok, thanks. I'll keep that in mind for the next version. Seems to me to also work with 2.065 and 2.064.

Re: splitter for strings

2014-06-09 Thread monarch_dodra via Digitalmars-d-learn
On Monday, 9 June 2014 at 11:04:12 UTC, Chris wrote: From the library reference: assert(equal(splitter(hello world, ' '), [ hello, , world ])); and If a range with one separator is given, the result is a range with two empty elements. My problem was that if I have input like auto word

Re: splitter for strings

2014-06-09 Thread monarch_dodra via Digitalmars-d-learn
On Monday, 9 June 2014 at 11:40:24 UTC, Chris wrote: On Monday, 9 June 2014 at 11:16:18 UTC, monarch_dodra wrote: On Monday, 9 June 2014 at 11:04:12 UTC, Chris wrote: From the library reference: assert(equal(splitter(hello world, ' '), [ hello, , world ])); and If a range with one

Re: splitter for strings

2014-06-09 Thread monarch_dodra via Digitalmars-d-learn
On Monday, 9 June 2014 at 14:21:21 UTC, Steven Schveighoffer wrote: Just looked at std.string for a strip function that allows custom character strippage, but apparently not there. The above is quite awkward. -Steve It's in algorithm, because it's more generic than just strings.

Re: splitter for strings

2014-06-09 Thread monarch_dodra via Digitalmars-d-learn
On Monday, 9 June 2014 at 15:19:05 UTC, Chris wrote: On Monday, 9 June 2014 at 14:47:45 UTC, Steven Schveighoffer wrote: On Mon, 09 Jun 2014 10:39:39 -0400, Chris wend...@tcd.ie wrote: Atm, I have auto parts = appender!(string[]); w.splitter('-').filter!(a = !a.empty).copy(parts); Which

Re: splitter for strings

2014-06-09 Thread monarch_dodra via Digitalmars-d-learn
On Monday, 9 June 2014 at 15:54:29 UTC, Steven Schveighoffer wrote: On Mon, 09 Jun 2014 11:49:29 -0400, monarch_dodra monarchdo...@gmail.com wrote: On Monday, 9 June 2014 at 14:21:21 UTC, Steven Schveighoffer wrote: Just looked at std.string for a strip function that allows custom character

Re: hijacking override from template mixin

2014-06-09 Thread monarch_dodra via Digitalmars-d-learn
On Monday, 9 June 2014 at 15:54:21 UTC, Ivan Kazmenko wrote: I'd expect a multiple overrides of same function error, much like if I just paste the mixin code by hand. Is that a bug or working by design? In the latter case, please explain the reasoning. AFAIK, the rationale is that *should*

Re: splitter for strings

2014-06-09 Thread monarch_dodra via Digitalmars-d-learn
On Monday, 9 June 2014 at 17:57:24 UTC, Steven Schveighoffer wrote: I think we are confusing things here, I was talking about strip :) strip and split are actually both pretty much in the same boat actually in regards to that, so just 's/split/strip/g', and the same answer will apply.

Re: splitter for strings

2014-06-09 Thread monarch_dodra via Digitalmars-d-learn
On Monday, 9 June 2014 at 19:47:29 UTC, Chris wrote: Uh, I see, I misread the signature of std.string.strip(). So that's one option now, to strip all trailing hyphens with std.string.strip(). Well, I'll give it a shot tomorrow. No, you read the documentation of std.*STRING*.strip correctly.

Re: splitter for strings

2014-06-09 Thread monarch_dodra via Digitalmars-d-learn
On Monday, 9 June 2014 at 19:54:08 UTC, Chris wrote: I think it makes sense to put any generic range based algorithms (split and so forth) into std.algorithm. It's always my first port of call, when I have a range. However, that you can do std.string.split([1, 2, 3], 2); is not exactly a

Re: D aliases vs. C typedefs

2014-06-10 Thread monarch_dodra via Digitalmars-d-learn
On Tuesday, 10 June 2014 at 20:33:03 UTC, Ali Çehreli wrote: I think they are actually legal: This is D's support of C-style array declaration acting the same in alias declaration: Ali C-style array declaration has got to be one of C's *worst* abominations. There's no real *technical*

Re: Splitting Ranges using Lambda Predicates

2014-06-10 Thread monarch_dodra via Digitalmars-d-learn
On Tuesday, 10 June 2014 at 21:11:17 UTC, Nordlöw wrote: 1. someName = SomeName My example is dumb and incorrect. I actually want this to do the following 2. _someGreatVariableName = Some Great Varible Name The current splitter works on the notion of splitter tokens, eg, it splits when it

Re: Splitting Ranges using Lambda Predicates

2014-06-10 Thread monarch_dodra via Digitalmars-d-learn
On Tuesday, 10 June 2014 at 21:26:50 UTC, monarch_dodr What exactly are you requesting though? - Split on the edge lowercase to uppercase? - Split on uppercase but keep the uppercase element? Thinking about this more: Do you *actually* have two different predicates, or are they mutually

Re: Splitting Ranges using Lambda Predicates

2014-06-11 Thread monarch_dodra via Digitalmars-d-learn
On Tuesday, 10 June 2014 at 22:31:37 UTC, Nordlöw wrote: Either way, it shouldn't be too hard to implement. Base it off splitter!pred, which is actually quite trivial. AFAIK, your What do you mean by basing it off splitter!pred - should I start with some existing splitter algorithm in Phobos

Re: Basic dynamic array question. Use of new versus no new.

2014-06-11 Thread monarch_dodra via Digitalmars-d-learn
On Wednesday, 11 June 2014 at 05:46:07 UTC, Ali Çehreli wrote: On 06/10/2014 08:06 PM, Matt wrote: On Wednesday, 11 June 2014 at 02:30:01 UTC, WhatMeWorry wrote: int[] array; // initially empty array.length = 5; // now has 5 elements while in Mr. Alexandrescu's book, it says To create a

Re: Splitting Ranges using Lambda Predicates

2014-06-11 Thread monarch_dodra via Digitalmars-d-learn
On Wednesday, 11 June 2014 at 11:42:42 UTC, Artur Skawina via Digitalmars-d-learn wrote: On 06/11/14 00:31, Nordlöw via Digitalmars-d-learn wrote: Either way, it shouldn't be too hard to implement. Base it off splitter!pred, which is actually quite trivial. AFAIK, your What do you mean by

Re: Splitting Ranges using Lambda Predicates

2014-06-11 Thread monarch_dodra via Digitalmars-d-learn
On Wednesday, 11 June 2014 at 13:44:25 UTC, Artur Skawina via Digitalmars-d-learn wrote: There is a reason why I never use D's std lib. artur Well, (IMO) it's a problem with no real solution. But for what it's worth, most (if not all) of the algorithms in the standard lib know how to handle

Re: Nameless Static Array

2014-06-12 Thread monarch_dodra via Digitalmars-d-learn
On Thursday, 12 June 2014 at 15:58:25 UTC, Taylor Hillegeist wrote: So, Lately I have been avoiding the NEW keyword. Why? Is malloc OK? I have recently given up static allocation of classes using CTFE. I guess they must be const or immutable? Funny, because you *are* allowed to default

Re: Cannot alias null

2014-06-12 Thread monarch_dodra via Digitalmars-d-learn
On Thursday, 12 June 2014 at 20:44:16 UTC, H. S. Teoh via Digitalmars-d-learn wrote: On Thu, Jun 12, 2014 at 03:26:13PM -0500, Tom Browder via Digitalmars-d-learn wrote: This will not compile: alias blah = null; [...] 'null' is a value, not a type. Try: alias blah = typeof(null);

Re: Cannot alias null

2014-06-12 Thread monarch_dodra via Digitalmars-d-learn
On Thursday, 12 June 2014 at 21:58:32 UTC, Adam D. Ruppe wrote: since null is a value maybe you want enum blah = null; you may also give it a type after the enum word I *think* the issue might be that null is an rvalue? Because you can alias variable names all you want. I do it all the time

Re: Cannot alias null

2014-06-13 Thread monarch_dodra via Digitalmars-d-learn
On Thursday, 12 June 2014 at 22:54:20 UTC, Ali Çehreli wrote: On 06/12/2014 03:38 PM, monarch_dodra wrote: So there's something special about null. The difference is that null is an expression. It is the same limitation as not being able to alias a literal. alias zero = 0; alias

Re: Cannot alias null

2014-06-13 Thread monarch_dodra via Digitalmars-d-learn
On Friday, 13 June 2014 at 15:05:49 UTC, Tom Browder via Digitalmars-d-learn wrote: So I'm not sure how to translate that into D. I do know my first attempt here doesn't work, even with it being surrounded by extern (C) {}: $ cat chdr.d struct t; struct t* t_ptr = null; This seems to work

Re: User defined forward range - foreach initializes f.r.

2014-06-13 Thread monarch_dodra via Digitalmars-d-learn
On Friday, 13 June 2014 at 18:14:10 UTC, Andre wrote: Hi, I have a template class List which should provide generic list functionality. In my use case, using a class instead a struct is more comfortable. Following unittest fails, although the forward range implements the save method.

Re: User defined forward range - foreach initializes f.r.

2014-06-13 Thread monarch_dodra via Digitalmars-d-learn
On Friday, 13 June 2014 at 19:03:12 UTC, Ali Çehreli wrote: I am making the following comment to have others confirm, as well as remind others about a potential problem. On 06/13/2014 11:14 AM, Andre wrote: unittest { auto intList = new List!int(1,2,3); [...] class List(T) {

  1   2   3   >