Re: Can D interface with Free Pascal?

2016-01-28 Thread bearophile via Digitalmars-d-learn
FreeSlave: On Thursday, 28 January 2016 at 08:15:38 UTC, FreeSlave wrote: Not directly. You can declare cdecl function on Free Pascal side and call it as extern(C). What about extern(Pascal)? https://dlang.org/spec/attribute.html#linkage Bye, bearophile

Re: How to represent struct with trailing array member

2016-01-21 Thread bearophile via Digitalmars-d-learn
Dibyendu Majumdar: On Thursday, 21 January 2016 at 21:52:06 UTC, Dibyendu Majumdar wrote: How should this be translated to D? Will D's array access allow data elements to be accessed beyond the size declared? Take a look at the code I've written here:

Re: Convert some ints into a byte array without allocations?

2016-01-16 Thread bearophile via Digitalmars-d-learn
Yazan D: On Saturday, 16 January 2016 at 14:42:27 UTC, Yazan D wrote: ubyte[] b = (cast(ubyte*) )[0 .. int.sizeof]; Better to use the actual size: ubyte[] b = (cast(ubyte*) )[0 .. a.sizeof]; Bye, bearophile

Re: conver BigInt to string

2015-11-05 Thread bearophile via Digitalmars-d-learn
Namal: Hello I am trying to convert BigInt to string like that while trying to sort it: void main() { import std.stdio, std.algorithm, std.conv, std.bigint, std.string; auto n = 17.BigInt ^^ 179; n.text.dup.representation.sort().release.assumeUTF.writeln; } Bye, bearophile

Re: conver BigInt to string

2015-11-05 Thread bearophile via Digitalmars-d-learn
void main() { import std.stdio, std.algorithm, std.conv, std.bigint, std.string; auto n = 17.BigInt ^^ 179; n.text.dup.representation.sort().release.assumeUTF.writeln; } Better: n.to!(char[]).representation.sort().release.assumeUTF.writeln; Bye, bearophile

Re: array function

2015-08-31 Thread bearophile via Digitalmars-d-learn
Namal: std::vector foo(int N){ std::vector V(N); int some_array[N]; VLAs are not present in D. Bye, bearophile

[Rosettacode] sum of powers conjecture

2015-07-26 Thread bearophile via Digitalmars-d-learn
I've translated the C++ entry to D as third D entry, but it's not a good translation, I've just converted iterators to pointers instead of using ranges (the resulting speed is acceptable). You're welcome to improve it:

Re: Ada to D - an array for storing values of each of the six bits which are sufficient

2015-05-01 Thread bearophile via Digitalmars-d-learn
Dennis Ritchie: Anybody can write a packed array on the D? I once badly represent the means by which we can write a packed array. Maybe for this you should use core.simd or unions? SIMD could be useful for some fancy bulk operations. But you should be able to write a good basic packed array

Re: Ada to D - an array for storing values of each of the six bits which are sufficient

2015-04-30 Thread bearophile via Digitalmars-d-learn
Dennis Ritchie: There is an array of values to store each of which sufficiently 6 bits. As it is written down on the D? You can't do it directly in D. Someone has to write a packed array data structure to do it. Bye, bearophile

Re: Implicit conversion error

2015-04-30 Thread bearophile via Digitalmars-d-learn
Paul: When compiled on a 64 bit machine, this line int r = uniform(0, mobs.length); .length returns a size_t, and 0 is an int. uniform() probably decides to unify those types to a size_t. A size_t is 32 bit on 32 bit machines and 64 bits on 64 bit machines. But D int is always a 32 bit

Re: Possible to write a classic fizzbuzz example using a UFCS chain?

2015-04-28 Thread bearophile via Digitalmars-d-learn
Gary Willoughby: I wondered if it was possible to write a classic fizzbuzz[1] example using a UFCS chain? I've tried and failed. Is this OK? void main() { import std.stdio, std.algorithm, std.range, std.conv, std.functional; 100 .iota .map!(i = ((i + 1) % 15).predSwitch(

Re: function ref param vs pointer param

2015-04-24 Thread bearophile via Digitalmars-d-learn
On Friday, 24 April 2015 at 13:39:35 UTC, ref2401 wrote: processPointer(ms); I think doing this way is more descriptive. Now all readers know that ms might be changed inside the function. C# avoids that problem requiring (in most cases) the usage of ref at the calling point too. But this

Re: function ref param vs pointer param

2015-04-24 Thread bearophile via Digitalmars-d-learn
ref2401: void processRef(ref MyStruct ms) { writeln(processRef: , ms); } void processPointer(MyStruct* ms) { writeln(processPointer: , *ms); ref params don't need the * every time you use them inside the function, and don't need the when you call the function. Bye,

Re: multiSort for sorting AA by value

2015-04-21 Thread bearophile via Digitalmars-d-learn
Chris: I'm happy with it, but maybe there is a more concise implementation? This is a bit shorter and a bit better (writefln is not yet able to format tuples nicely): void main() { import std.stdio: writeln; import std.algorithm.sorting: multiSort; import std.array: array;

Re: Structural exhaustive matching

2015-04-21 Thread bearophile via Digitalmars-d-learn
Jadbox: I'm curious on what the best way to do ADTs in D. Sometimes there's no best way, there are several alternative ways with different tradeoffs. D isn't a functional language and there's no really good way to do ADTs in D. You can use plus a final switch. Or you can use Algebraic from

Re: Converting Java code to D

2015-04-20 Thread bearophile via Digitalmars-d-learn
John Colvin: struct LineStyle { enum NONE = None; enum SOLID = Solid; enum DASH = Dash; enum DOT = Dot; enum DASHDOT = Dash Dot; enum DASHDOTDOT = Dash Dot Dot; string label; private this(string label) { this.label = label; } } The constructor

Re: Function name from function pointer

2015-04-11 Thread bearophile via Digitalmars-d-learn
Paul D Anderson: Is there a way to return the name of a function (a string) from a pointer to that function? Perhaps creating a string[void*] AA and initializing with all the function pointers you care about. Bye, bearophile

Re: Generating all combinations of length X in an array

2015-04-09 Thread bearophile via Digitalmars-d-learn
wobbles: Have just tested, it is! But with the current D front-end it's not a good idea to generate too many combinations at compile-time. Efficient code doesn't save you from bad usages. Bye, bearophile

Re: return the other functions of the void main()

2015-04-09 Thread bearophile via Digitalmars-d-learn
Jack Applegame: writeln(a.find(4).empty ? No : Yes); canFind? Bye, bearophile

Re: Generating all combinations of length X in an array

2015-04-08 Thread bearophile via Digitalmars-d-learn
wobbles: While trying to generate all combinations of length X in an array, I came across the question on stackoverflow. [1] Theres a couple good answers there, but one that caught my eye shows a C# code snippet that is quite nice and short: Often short code is not the best code. Take a

Re: Implementing Iterator to support foreach

2015-04-08 Thread bearophile via Digitalmars-d-learn
tcak: I am planning to implement Iterator class. But looking at foreach statement, it takes a range only. Unless you are just experimenting, it's better to not go against a language and its std lib. Bye, bearophile

Re: function shadowed

2015-04-08 Thread bearophile via Digitalmars-d-learn
ddos: same behavior when overriding methods of base classes This is by design. Bye, bearophile

Re: Shall I use immutable or const while passing parameters to functions

2015-04-07 Thread bearophile via Digitalmars-d-learn
tcak: void dataProcessor( string giveMeAllYourData ){} dataProcessor( cast( immutable )( importantData[5 .. 14] ) ); With Const, void dataProcessor( in char[] giveMeAllYourData ){} dataProcessor( cast( const )( importantData[5 .. 14] ) ); Don't cast to const/immutable unless you have a

Re: Issue with free() for linked list implementation

2015-04-04 Thread bearophile via Digitalmars-d-learn
Namespace: I've written a straight forward linked list implementation here: https://github.com/nomad-software/etcetera/blob/master/source/etcetera/collection/linkedlist.d Even though I'm using the GC to manage memory, maybe it will help you. Good idea to link to some existing code. Here

Re: Speed of horizontal flip

2015-04-01 Thread bearophile via Digitalmars-d-learn
tchaloupka: Am I doing something utterly wrong? If you have to perform performance benchmarks then use ldc or gdc. Also disable bound tests with your compilation switches. Sometimes reverse() is not efficient, I think, it should be improved. Try to replace it with a little function written

Re: What ?

2015-03-30 Thread bearophile via Digitalmars-d-learn
Brian Schott: Do this instead: ulong u = 1L 63; I suggest a more explicit: ulong u = 1UL 63; Alternative: ulong u = 2UL ^^ 63; Bye, bearophile

Re: Associative Array of Const Objects?

2015-03-29 Thread bearophile via Digitalmars-d-learn
bitwise: I'm a little confused at this point why this doesn't work either: const and immutable are rather different between C++ and D, I suggest you to take a look at the documentation: http://dlang.org/const-faq.html Bye, bearophile

Re: Associative Array of Const Objects?

2015-03-28 Thread bearophile via Digitalmars-d-learn
bitwise: class Test{} void main() { const(Test)[string] tests; tests[test] = new Test(); } This code used to work, but after upgrading to dmd 2.067, it no longer does. --Error: cannot modify const expression tests[test] How do I insert an item into an associative array of

Re: D's type classes pattern ?

2015-03-25 Thread bearophile via Digitalmars-d-learn
matovitch: I am curious to know how isInputRange is implemented since I wanted to do kind of the same but I am afraid it's full of (ugly) traits and template trickeries where haskell type classes are quite neat and essentially a declaration of an interface. Take a look at the sources and

Re: Keep Track of the Best N Nodes in a Graph Traversal Algorithm

2015-03-25 Thread bearophile via Digitalmars-d-learn
Nordlöw: I have graph traversal algorithm that needs to keep track of the N best node visit. std.algorithm.topNCopy? Bye, bearophile

Re: C# to D

2015-03-25 Thread bearophile via Digitalmars-d-learn
Dennis Ritchie: int[] arr = { 7, 5, 7, 3, 3, 5, 3, 3, 0, 3, 1, 1, 5, 1, 1, 1, 2, 2, 8, 5, 8, 8 }; Console.WriteLine(string.Join( , arr.OrderByDescending(x = arr.Count(y = y == x)).ThenBy(x = x))); // prints 1 1 1 1 1 3 3 3 3 3 5 5 5 5 8 8 8 2 2 7 7 0 One solution:

Re: C# to D

2015-03-25 Thread bearophile via Digitalmars-d-learn
.schwartzSort!(x = tuple(-arr.count!(y = y == x), x)) But calling count for each item is not efficient (in both C# and D). If your array is largish, then you need a more efficient solution. Bye, bearophile

Re: C# to D

2015-03-25 Thread bearophile via Digitalmars-d-learn
Ali Çehreli: Do you know the story about groupBy? It's a long messy story. Look for it with another name, like chunkBy or something like that. Bye, bearophile

Re: C# to D

2015-03-25 Thread bearophile via Digitalmars-d-learn
Ivan Kazmenko: (1) For me, the name of the function is obscure. Something like sortBy would be a lot easier to find than schwartzSort. I've asked to change the name of that function for years. But Andrei Alexandrescu is a adamantly against changing that pet name he has chosen. This is

Re: C# to D

2015-03-25 Thread bearophile via Digitalmars-d-learn
Dennis Ritchie: A more effective solution for C ++: #include iostream #include vector #include range/v3/all.hpp int main() { using namespace ranges; auto rng = istreamint( std::cin ) | to_vector | action::sort | view::group_by( std::equal_toint() )

Re: C# to D

2015-03-25 Thread bearophile via Digitalmars-d-learn
Ivan Kazmenko: arr.map !(to !(string)) .join ( ) .writeln; I suggest to not put a space before the bang (!), because it's confusing for me. Also, arr.map !(to !(string)) is better written arr.map!text. But even better is to use the range formatting of writefln,

Re: Keep Track of the Best N Nodes in a Graph Traversal Algorithm

2015-03-25 Thread bearophile via Digitalmars-d-learn
Nordlöw: Ahh, a Binary Heap perfectly matches my needs. https://en.wikipedia.org/wiki/Binary_heap http://dlang.org/phobos/std_container_binaryheap.html But isn't topNCopy using a heap? Bye, bearophile

Re: Do strings with enum allocate at usage point?

2015-03-18 Thread bearophile via Digitalmars-d-learn
岩倉 澪: However, if enum implies allocation at the usage point for strings, There are two ways to see if something allocates: there is a compiler switch, and an annotation: void foo() @nogc { // Your code here } If the compiler doesn't have a bug it will complain if you put something

Re: How to generate a random string ...

2015-03-16 Thread bearophile via Digitalmars-d-learn
Robert burner Schadek: ... from all Unicode characters in an idiomatic D way? Perhaps by rejection? I mean, generating a uint, test if it's a character and repeat until the result is true. Bye, bearophile

Re: get from tuple by type

2015-03-15 Thread bearophile via Digitalmars-d-learn
Charles Cooper: Yes, I could say external_api1_react_to_event(event_t[1], event_t[0]) .. but that is barbaric. It's a productivity sink because I have to go back to the original definition, align the arguments, and then context switch back to whatever I was working on before. If you are

Re: Dlang seems like java now,but why not let d more like C# Style?

2015-03-14 Thread bearophile via Digitalmars-d-learn
dnewer: but,C# cant compiled to native code. Soon you will be able to compile C# natively. Bye, bearophile

Re: Compilation changes

2015-03-13 Thread bearophile via Digitalmars-d-learn
Ali Çehreli: That may be related to the recent changes in the build system. Right. Have you been following the following threads? (I haven't been; so, I am sure whether they apply.) I have not. [dmd-internals] DMD now requires a working D compiler to be build Proposal : aggregated

Compilation changes

2015-03-13 Thread bearophile via Digitalmars-d-learn
Time ago Dmitry Olshansky gave me a nice script to compile dmd+Phobos from github, starting like this: My recipe on Windows, by Dmitry Olshansky: 0. Make sure there are no other DMD in the Windows path. 1. Get the latest DMD release zip you can find, unzip to some drive root(! otherwise get's

Re: Int to float?

2015-03-06 Thread bearophile via Digitalmars-d-learn
Ola Fosheim Grøstad: D claims to follow C, so using unions for type punning is ultimately implementation defined. I am not sure if D is the same as C regarding this. Bye, bearophile

Re: is expression and type tuples

2015-03-04 Thread bearophile via Digitalmars-d-learn
Jack Applegame: On Tuesday, 3 March 2015 at 17:49:24 UTC, bearophile wrote: That's 1 + n-1 :-) Could you please explain what does '1 + n-1' mean? This is your code: template Is(ARGS...) if(ARGS.length % 2 == 0) { enum N = ARGS.length/2; static if(N == 1) enum Is = is(ARGS[0] :

[rosettacode] std.container.DList problems

2015-03-04 Thread bearophile via Digitalmars-d-learn
This code used to work in DMD 2.065: http://rosettacode.org/wiki/Strand_sort#D import std.stdio, std.container; DList!T strandSort(T)(DList!T list) { static DList!T merge(DList!T left, DList!T right) { DList!T result; while (!left.empty !right.empty) { if

Re: Implicit fall through not detected (Example from lex.html)

2015-03-03 Thread bearophile via Digitalmars-d-learn
Andre: I am also not really happy with the actual behavor (w / wi switch needed) You shall always compile your D code with warnings active, unless you need them disabled for some real reason. Eventually the fall through warning will become a deprecation and then an error. It's meant to be

Re: is expression and type tuples

2015-03-03 Thread bearophile via Digitalmars-d-learn
Jack Applegame: Seems like is expression doesn't support type tuples: pragma(msg, is(short : int)); // true enum Test(ARGS...) = is(ARGS[0..2] : ARGS[2..4]); pragma(msg, is(Test!(int, int, int, int))); // false pragma(msg, Test!(int, short, int, int)); // false Is it

Re: is expression and type tuples

2015-03-03 Thread bearophile via Digitalmars-d-learn
Jack Applegame: or use recursion (with splitting in two, and not 1 + n-1). Bye, bearophile I already have one: template Is(ARGS...) if(ARGS.length % 2 == 0) { enum N = ARGS.length/2; static if(N == 1) enum Is = is(ARGS[0] : ARGS[1]); else enum Is = is(ARGS[0] : ARGS[N])

Re: UFCS on template alias ?

2015-02-21 Thread bearophile via Digitalmars-d-learn
Baz: Is this a normal behaviour ? Try to move the definition of poly to module-level scope. This is a design decision to avoid other troubles. Bye, bearophile

Re: D : dmd vs gdc : which one to choose?

2015-02-19 Thread bearophile via Digitalmars-d-learn
Mayuresh Kathe: Should I choose DMD or go with GDC? It's a good idea to use all available compilers. LDC and DMD are both useful. Every one of them has advantages and disadvantages. Bye, bearophile

Re: Mimicking C++'s indexing behavior in D associative arrays

2015-02-18 Thread bearophile via Digitalmars-d-learn
Rikki Cattermole: Foo*[string] bar; Foo v = *bar.grab(mykey); Is this the setdefault of Python dicts? If this need is strong a new function could be added to Phobos (or even druntime if you want to reduce the number of hash computations). Bye, bearophile

Re: what is the offical way to handle multiple list in map() ?

2015-02-16 Thread bearophile via Digitalmars-d-learn
Baz: is this the official way ? It seems a way to perform nested mapping in D. --- auto fruits = [apple, banana, orange][]; auto vegies = [grass, salad][]; Those trailing [] are unneded. auto youreallygonna = map!( `map!(a = eat ~ a)(a)` )([fruits, vegies]); Better to use another

Re: ranges reading garbage

2015-02-15 Thread bearophile via Digitalmars-d-learn
John Colvin: prints things like [0, 4, 5, 1, 1, 1459971595, 1459971596, 2, 2, 1459971596, 1459971597, 3, 4, 8, 9, 5, 5, 4441427819, 4441427820, 6, 6, 4441427820, 4441427821, 7] but the output isn't consistent, the big numbers change on each run. Try to replace the only() with: [y,

Re: ranges reading garbage

2015-02-15 Thread bearophile via Digitalmars-d-learn
FG: Odd... Still something is wrong. It prints: [0, 4, 5, 1, 1, 5, 6, 2, 2, 6, 7, 3, 4, 8, 9, 5, 5, 5, 6, 6, 6, 6, 7, 7] instead of this: [0, 4, 5, 1, 1, 5, 6, 2, 2, 6, 7, 3, 4, 8, 9, 5, 5, 9, 10, 6, 6, 10, 11, 7] This is less lazy and gives another result: import std.range,

Re: Data-Flow (Escape) Analysis to Aid in Avoiding GC

2015-02-13 Thread bearophile via Digitalmars-d-learn
Per Nordlöw: Then how does the GC know when to release when there are multiple references? The mark phase counts what's reachable and what can't be reached. If an object has one pointer to it, or one hundred pointers, it is not removed. If nothing points to it, it is removed. I suggest

Re: Number of Bits Needed to Represent a Zero-Offset Integer

2015-02-13 Thread bearophile via Digitalmars-d-learn
H. S. Teoh: Maybe that could be the basis of a better name? Right. Bye, bearophile

Re: Number of Bits Needed to Represent a Zero-Offset Integer

2015-02-13 Thread bearophile via Digitalmars-d-learn
H. S. Teoh: So it could be called ilog2? Perhaps floorIlog2? Isn't ilog2 a different function? Bye, bearophile

Re: What is the Correct way to Malloc in @nogc section?

2015-02-13 Thread bearophile via Digitalmars-d-learn
Foo: I'm regret that I tried to help, I will delete this repo as far as possible. :) Language communities aren't perfect, but the success of a language comes from the help of many little hands :) Perhaps Rust will win over D in the end, but there's no reason to throw away your work just

Re: Number of Bits Needed to Represent a Zero-Offset Integer

2015-02-13 Thread bearophile via Digitalmars-d-learn
Dominikus Dittes Scherkl: I would recommend to use something like this: /// returns the number of the highest set bit +1 in the given value or 0 if no bit is set size_t bitlen(T)(const(T) a) pure @safe @nogc nothrow if(isUnsigned!T) { static if(T.sizeof = size_t.sizeof) // doesn't work

Re: Data-Flow (Escape) Analysis to Aid in Avoiding GC

2015-02-13 Thread bearophile via Digitalmars-d-learn
Tobias Pankrath: Why should splitter.front allocate? I think that front was able to throw Unicode exceptions, that require the GC. But I think later they have become asserts, that don't require the GC. Bye, bearophile

Re: How to write similar code D?

2015-02-10 Thread bearophile via Digitalmars-d-learn
FG: auto query = iota(2, 2 + 10) .map!(c = [Length: 2 * c, Height: c * c - 1, Hypotenuse: c * c + 1]) .map!(x = format(%4d%4d%4d, x[Height], Unlike other languages like JavaScript, the D front-end is very weak in optimizing well such kind of code... I think D compilers handle

Re: cannot deduce function from argument types issue.

2015-02-10 Thread bearophile via Digitalmars-d-learn
ted: Could someone enlighten me ? This works: import std.range: ElementType, isInputRange; ElementType!R testFunc(R, T)(R range, T foo) if (is(ElementType!R == T)) { static assert(isInputRange!R); typeof(return) retVal = foo ^^ 2; // More DRY. return retVal; } void main() {

Re: cannot deduce function from argument types issue.

2015-02-10 Thread bearophile via Digitalmars-d-learn
Ali Çehreli: If think it is a little too much to ask from the template system of D. I remember hitting a similar problem with code like this bar() function: // OK void foo(size_t N1, size_t N2)(int[N1] a, int[N2] b) if (N2 == N1 ^^ 2) {} // Not OK void bar(size_t N)(int[N] a, int[N ^ 2]

Re: cannot deduce function from argument types issue.

2015-02-10 Thread bearophile via Digitalmars-d-learn
void bar(size_t N)(int[N] a, int[N ^ 2] b) {} I meant: void bar(size_t N)(int[N] a, int[N ^^ 2] b) {}

Re: Classes and @disable this()

2015-02-10 Thread bearophile via Digitalmars-d-learn
I think this can be filed in Bugzilla as diagnostic enhancement: class Foo { @disable this(); this(int i) {} } void main() {} https://issues.dlang.org/show_bug.cgi?id=14163 Bye, bearophile

Re: cannot deduce function from argument types issue.

2015-02-10 Thread bearophile via Digitalmars-d-learn
ted: ... where you say 'More DRY' above, are you referring to I was referring to both, but mostly to the typeof. It's more DRY (http://en.wikipedia.org/wiki/Don%27t_repeat_yourself ). You are stating only once the type of the return variable. This is less bug-prone. Bye, bearophile

Re: To write such an expressive code D

2015-02-10 Thread bearophile via Digitalmars-d-learn
Dennis Ritchie: Please help. This starts to look like homework :-) Bye, bearophile

Re: Compilation with dub + dmd: out of memory

2015-02-10 Thread bearophile via Digitalmars-d-learn
Vlasov Roman: I have the quite computer with 2 GB RAM. At compilation with dub and dmd of small project this pair eating about 1.4~1.5 GB RAM. I solve this probleb by connecting swap partition, but it calls some freezes + it take ~10% of swap, and after compilation swap not released. At

Re: To write such an expressive code D

2015-02-10 Thread bearophile via Digitalmars-d-learn
Dennis Ritchie: Output: 0 xor 0 xor 0 = 0 0 xor 0 xor 1 = 1 0 xor 1 xor 0 = 1 0 xor 1 xor 1 = 0 1 xor 0 xor 0 = 1 1 xor 0 xor 1 = 0 1 xor 1 xor 0 = 0 1 xor 1 xor 1 = 1 This man again took advantage of the fact that in D there is no such operation - (analog switch). A natural solution in D:

Re: How to write similar code D?

2015-02-09 Thread bearophile via Digitalmars-d-learn
Dennis Ritchie: Tell me, please, how to write similar С# code D: This is more or less exactly the same: void main() { import std.stdio, std.range, std.algorithm, std.typecons, std.format; auto query = iota(2, 12) .map!(c = Tuple!(int,length, int,height,

Re: Classes and @disable this()

2015-02-08 Thread bearophile via Digitalmars-d-learn
fra: However making it a compiler error would be far, far better I think this can be filed in Bugzilla as diagnostic enhancement: class Foo { @disable this(); this(int i) {} } void main() {} Bye, bearophile

Re: Template constructor in a non-template struct.

2015-02-08 Thread bearophile via Digitalmars-d-learn
ChrisG: I don't really understand how I'd differentiate a constructor template from a class/struct template. One solution is to use a template struct (struct/class names start with an upper case in D, while typed enum members usually start with a lower case): enum E { option1, option2 }

Re: primitive type variables not nullable ?

2015-02-08 Thread bearophile via Digitalmars-d-learn
Tobias Pankrath: Check for null with (x is null) not via printing to stdout. In most cases instead of checking dynamic arrays for null, it's better to use std.array.empty. Bye, bearophile

Re: Do you have a better way to remove element from a array?

2015-02-05 Thread bearophile via Digitalmars-d-learn
Tobias Pankrath: Works as designed: http://dlang.org/phobos/std_algorithm.html#.remove Unfortunately it's one of the worst designed functions of Phobos: https://issues.dlang.org/show_bug.cgi?id=10959 Bye, bearophile

Re: how can I get a reference of array?

2015-02-05 Thread bearophile via Digitalmars-d-learn
zhmt: Will arr.ptr change in the future? As the array add more members , it need more memroy, then remalloc may be called, the pointer maybe change, then the stored pointer will be invalid. Will this happen? Yes, it can happen. Bye, bearophile

Re: Do you have a better way to remove element from a array?

2015-02-05 Thread bearophile via Digitalmars-d-learn
bachmeier: It seems your argument is that remove is poorly designed because it's not destructive. Or am I missing your argument? It has to be a void function (or perhaps bettter it can return true/false if it has removed the item, so it becomes @nogc and nothrow). And it has to remove the

Re: Conway's game of life

2015-02-03 Thread bearophile via Digitalmars-d-learn
Paul: Regarding the immutable loop variable, I've conditioned myself never to interfere with loop control values But adding immutable you don't risk modifying the variable by mistake. It's another design mistake of D. Variables (like foreach loop indexes) must be immutable by default

Re: Conway's game of life

2015-02-03 Thread bearophile via Digitalmars-d-learn
Paul: enum WORLDSIZE = 20; enum INITIALPOP = 70; //experimental enum DEAD = 0; enum ALIVE = 1; D enums don't need to be ALL UPPERCASE :-) int world[WORLDSIZE][WORLDSIZE]; Don't forget to compile with warnings active (it's a design error of the D

Re: how convert the range to slice ?

2015-02-02 Thread bearophile via Digitalmars-d-learn
Nordlöw: Is started digging a bit... The magic happens at line 103 in cast.c. How do I most conveniently figure out which members (functions) a type (e-type) has? I figured I could check for typical InputRange members and issue a hint about using .array if e-type has them. It's probably

Re: Conway's game of life

2015-02-02 Thread bearophile via Digitalmars-d-learn
gedaiu: https://github.com/gedaiu/Game-Of-Life-D A bare-bones implementation: http://rosettacode.org/wiki/Conway%27s_Game_of_Life#Faster_Version The quality of the D GC is not important for a simple Life implementation, you just need two arrays. Bye, bearophile

Re: std.algorithm sort() and reverse() confusion

2015-02-02 Thread bearophile via Digitalmars-d-learn
Jonathan M Davis: arr.reverse.map!sqrt Yes, but arguably, chaining calls in this case is bad, We have discussed this some time... and I'd like reverse() to return the original array (like the deprecated array .reverse property). It's not a perfect design, but allowing UFCS chains is

Re: Deducing a template retrun parameter type based on an assignment?

2015-01-30 Thread bearophile via Digitalmars-d-learn
Jeremy DeHaan: I figured that it would be smart enough to deduce the parameter type based on the type that it was trying to be assigned to. For that you need languages like Haskell/Rust. D type inference doesn't work from the type something is assigned to. Bye, bearophile

Re: Check if type is from specific template?

2015-01-29 Thread bearophile via Digitalmars-d-learn
Tofu Ninja: Basically what the title says, how do I check if a type T is an instantiation of a specific template? If you have an updated Phobos std.traits.isInstanceOf could be what you look for. Bye, bearophile

Re: how convert the range to slice ?

2015-01-28 Thread bearophile via Digitalmars-d-learn
Chris Williams: Range is not castable to array. See std.array.array to generate an array from a Range. Currently this program: void main() { import std.range; int[] a = iota(10); } Gives an error like: test.d(3,19): Error: cannot implicitly convert expression (iota(10)) of type

Re: how convert the range to slice ?

2015-01-28 Thread bearophile via Digitalmars-d-learn
Nordlöw: Is there any chance we could add logic to dmd+phobos that hints user about this? It's such a fundamental part of D+Phobos that newbies are forced to learn this quickly. On the other hand an informative error message could be useful... What error message do you suggest? Bye,

Re: I left my program open for 9 hours and it used up 700mb of ram, could someone review it?

2015-01-27 Thread bearophile via Digitalmars-d-learn
Gan: Is there some special stuff I gotta do extra with structs? Do they need manually allocated and released? Most of your usages of tiny structs should be by value. So just keep in mind they are values. Even when you iterate with a foreach on a mutable array of them :-) On a second

Re: Array List object?

2015-01-27 Thread bearophile via Digitalmars-d-learn
Gan: //Initializing the array tiles = new SBTile[](0); This is often useless. //Clearing the array tiles = []; This doesn't clear the array, it rebinds it to a null pointer. Bye, bearophile

Re: Array List object?

2015-01-27 Thread bearophile via Digitalmars-d-learn
And it's named dynamic array, instead of Array List object, it's not a class instance. Bye, bearophile

Re: Virtual functions and inheritance

2015-01-27 Thread bearophile via Digitalmars-d-learn
Baz: doesn't work. And similarly to the the orginal post: I suggest to read some D documentation first, and program later. Bye, bearophile

Re: Classical bug

2015-01-27 Thread bearophile via Digitalmars-d-learn
Vladimir Panteleev: But the check seems very simple, and is easily circumvented. This compiles: byte[] func() { byte[1024] buffer; auto p = buffer[0..3]; return p; } I guess such bugs will be detected (in safe code only!) after the implementation of: http://wiki.dlang.org/DIP69

Re: I left my program open for 9 hours and it used up 700mb of ram, could someone review it?

2015-01-27 Thread bearophile via Digitalmars-d-learn
Gan: How can I make it use less CPU/RAM? Most tiny classes probably should be structs. More generally, use a struct every time you don't need a class. You can start with those two: struct SBRange { double left = 0.0, right = 0.0, top = 0.0, bottom = 0.0; } struct Point(T) { T x,

How to tell an identifier is a module?

2015-01-26 Thread bearophile via Digitalmars-d-learn
__traits(allMembers, mixin(__MODULE__)) also yields a module name like object, but then how can you find out that object is a module? This doesn't work: void main() { pragma(msg, is(int == int)); pragma(msg, is(object == module)); } Bye, bearophile

Re: static class vs. static struct

2015-01-26 Thread bearophile via Digitalmars-d-learn
On Monday, 26 January 2015 at 14:02:54 UTC, ref2401 wrote: What's the difference between static class and static struct? What should i use? Non-static structs/classes have an extra pointer. Static ones don't have it, so their differences are the usual ones: a class is used by reference and

Re: using the full range of ubyte with iota

2015-01-25 Thread bearophile via Digitalmars-d-learn
Vlad Levenfeld: What's this about !`[]` and std.range.uniform?? It's not in the documentation. It's an enhancement I have proposed. Bye, bearophile

Re: using the full range of ubyte with iota

2015-01-25 Thread bearophile via Digitalmars-d-learn
Dominikus Dittes Scherkl: Because this is useful in more situations, Right, but it's still a cast. And in D you want to minimize the number of usages of casts. The proposed syntax iota![] is cast-safe. Bye, bearophile

Re: Difference between concatenation and appendation

2015-01-25 Thread bearophile via Digitalmars-d-learn
Laeeth Isharc: I think concatenation and append are used as synonyms (the same meaning is meant). a~=b or a=a~b a=a~b always allocates a new array, while a~=b sometimes re-allocates in place. Bye, bearophile

Re: using the full range of ubyte with iota

2015-01-24 Thread bearophile via Digitalmars-d-learn
Dominikus Dittes Scherkl: Has anyone any idea how to work around this? In Bugzilla I have proposed to solve this problem with this syntax taken from std.range.uniform: iota![](ubyte.min, ubyte.max) Bye, bearophile

Re: Initialization of structure field w/o default ctor

2015-01-22 Thread bearophile via Digitalmars-d-learn
drug: Also can I avoid dummy non-default ctor for Bar? One solution: struct Foo { int foo; @disable this(); this(int foo_) pure nothrow @safe @nogc { this.foo = foo_; } } struct Bar { enum arraySize = 3; Foo[arraySize] foo = Foo(1); } void main() @safe {

Re: Defining a static array with values in a range

2015-01-22 Thread bearophile via Digitalmars-d-learn
tcak: Well, that's just disguising what we can't do. When the a..b syntax was added to foreach() someone criticized that syntax saing it's a one trick pony, and indeed I don't know why Walter didn't make it a little more first-class. But note that in D the a..b ranges are always open on

  1   2   3   4   5   6   >