On 06/08/2010 22:57, Andrej Mitrovic wrote:
// Offtopic Template errors are so hard to grasp, most of the time
it's best to just ignore them and take some logical steps to fix the
errors. At least that's in my case true..
lol, that's true. I've been basing out c++ & d template code for years
I'm new to D2, so please.. :)
The spec on Array Operations,
http://www.digitalmars.com/d/2./arrays.html says:
"""A vector operation is indicated by the slice operator appearing as
the lvalue of an =, +=, -=, *=, /=, %=, ^=, &= or |= operator."""
The following tests works fine as I expected:
Variant in phobos doesn't work well with being const, toHash and opEquals for
example are not marked as const methods. I have tried to work around it with
careful casting, but it is too unsafe and just too complex for something that
is
supposed to make life easier.
Does anybody know if this i
I understand this is an unsafe operation, I just have a quick question.
Here, b points outside a's memory, so I get garbage.
{
int[] a = [3,3,3];
auto p = a.ptr;
auto b = p[0..3];
assert(b == [3,3,3]);
b = p[
Am 07.08.2010 14:10, schrieb simendsjo:
I'm new to D2, so please.. :)
The spec on Array Operations,
http://www.digitalmars.com/d/2./arrays.html says:
"""A vector operation is indicated by the slice operator appearing as
the lvalue of an =, +=, -=, *=, /=, %=, ^=, &= or |= operator."""
The follo
Since default constructors for structs are not allowed, how do I go
about making all the elements of this Matrix struct default to zero?
(floats default to nan by default).
struct Matrix(int M, int N)
{
float[M][N] elements;
}
Matrix(3, 3) m;
assert(m[0][0] == 0);
?
Peter Alexander:
> Since default constructors for structs are not allowed, how do I go
> about making all the elements of this Matrix struct default to zero?
> (floats default to nan by default).
This doesn't work, I don't know why:
float[M][N] elements = 0.0;
A bad looking solution:
struct Ma
simendsjo:
> When I do the same with a static array, I get 0. But this is just
> actually garbage, right? It might contain other data, and not always 0?
Generally it contains "garbage".
In string literals there is one more item that's empty (zero). I don't know if
normal fixed-sized arrays too h
On 07/08/2010 13:45, simendsjo wrote:
When I do the same with a static array, I get 0. But this is just
actually garbage, right? It might contain other data, and not always 0?
Yes. It's entirely undefined what will happen, you might find that the
extra bit of memory you try and access is a no
On 7/08/10 3:44 PM, bearophile wrote:
Peter Alexander:
Bye,
bearophile
Thanks for your detailed reply.
However, I'm not particularly satisfied with any of those :(
I have already gone with the single parameter init constructor in my
code until I find something better, but really, I just wa
Is there a difference (performance) or something else between the two solutions?
Is the following equalent?
int[] a;
int[] b = a;
assert(a is b);
assert(a.ptr == b.ptr);
On 7/08/10 4:33 PM, simendsjo wrote:
Is the following equalent?
int[] a;
int[] b = a;
assert(a is b);
assert(a.ptr == b.ptr);
No.
(a is b) implies (a.ptr == b.ptr)
but
(a.ptr == b.ptr) does not imply (a is b)
For example:
int[] a = [1, 2, 3];
int[] b = a[0..1];
Here, a.ptr == b.ptr, but
On 07.08.2010 18:04, Peter Alexander wrote:
On 7/08/10 4:33 PM, simendsjo wrote:
Is the following equalent?
int[] a;
int[] b = a;
assert(a is b);
assert(a.ptr == b.ptr);
No.
(a is b) implies (a.ptr == b.ptr)
but
(a.ptr == b.ptr) does not imply (a is b)
For example:
int[] a = [1, 2, 3];
i
Hey, here Mafi again,
I thought about your snippets and here's what I have.
Am 07.08.2010 14:10, schrieb simendsjo:
{
double[3] a = [1,1,1];
double[3] b;
b[] = a[] + 3;
assert(a == [1,1,1]);
assert(b == [4,4,4]);
}
{
double[3] a = [1,1,1];
auto b = a;
b[] = a[] + 3;
assert(a == [1,1,1]);
assert
Hi, I'm a newbie, hope the attached example works for you.
Peter Alexander wrote:
Since default constructors for structs are not allowed, how do I go
about making all the elements of this Matrix struct default to zero?
(floats default to nan by default).
struct Matrix(int M, int N)
{
floa
The spec for array says:
Static arrays are value types. Unlike in C and D version 1, static
arrays are passed to functions by value. Static arrays can also be
returned by functions.
I don't get the "static arrays are passed to functions by value" part.
Here I am passing in a static and dynam
oops :p correction:
in template Zero
const float[M][N] ==> const T[M][N]
rouge wrote:
Hi, I'm a newbie, hope the attached example works for you.
Peter Alexander wrote:
Since default constructors for structs are not allowed, how do I go
about making all the elements of this Matrix struct defaul
On 07/08/2010 16:25, Blonder wrote:
Is there a difference (performance) or something else between the two solutions?
I doubt it, templates are applied at compile time in the front end of
the compiler so the generated code should be the same.
The if style syntax allows much more comprehensive
On 2010-08-07 9:26, simendsjo wrote:
The spec for array says:
Static arrays are value types. Unlike in C and D version 1, static
arrays are passed to functions by value. Static arrays can also be
returned by functions.
I don't get the "static arrays are passed to functions by value" part.
Here
Hello,
I am trying to understand the template mechanism in D, but I don't get it
working.
I want to compute the dot product as follows:
int[] a = [1, 2, 3];
int[] b = [5, 6, 7];
double dd = ???.dot_product( a, b );
with classes like this:
class DotProduct(DIM, T)
{
Am 07.08.2010 18:40, schrieb Blonder:
Hello,
I am trying to understand the template mechanism in D, but I don't get it
working.
I want to compute the dot product as follows:
int[] a = [1, 2, 3];
int[] b = [5, 6, 7];
double dd = ???.dot_product( a, b );
with classes li
simendsjo wrote:
Ok, thanks. Does this mean this equivalent then?
int[] a = [1,2,3];
int[] b = a[0..1];
assert(a !is b);
assert(a.ptr == b.ptr && a.length == b.length);
Well, no. But what you probably mean, is.
( a is b ) == ( a.ptr == b.ptr && a.length == b.length )
--
Simen
simendsjo wrote:
The spec for array says:
Static arrays are value types. Unlike in C and D version 1, static
arrays are passed to functions by value. Static arrays can also be
returned by functions.
I don't get the "static arrays are passed to functions by value" part.
Here I am passing
module test;
void main()
{
int[] foos;
func1(foos);
}
void func1(T)(const T[] foos)
{
T afoo;
func2(foos, afoo);
}
void func2(T)(const T[] foos, out T afoo)
{
}
$ dmd -c test.d
test.d(12): Error: template test.func2(T) does not match any function template
declaration
test.d(12):
Hello,
the template!(...) mechanism I understand.
But I think I need the two classes, the first is the primary template
and the second the partial specialization (this is the end of the "loop"), or
can
I do this in D with functions?
The normal computation of the dot product is normally done in a
Mafi wrote:
Hey, here Mafi again,
I thought about your snippets and here's what I have.
Am 07.08.2010 14:10, schrieb simendsjo:
{ // Like the previous example, but with dynamic arrays..
double[] a = [1,1,1];
auto b = a;
assert(a is b);
b = a[] + 3;
assert(a == [1,1,1]);
//writeln(b); // acces
Brad Roberts wrote:
module test;
void main()
{
int[] foos;
func1(foos);
}
void func1(T)(const T[] foos)
{
T afoo;
func2(foos, afoo);
}
void func2(T)(const T[] foos, out T afoo)
{
}
$ dmd -c test.d
test.d(12): Error: template test.func2(T) does not match any function template
d
On 8/7/2010 12:42 PM, Don wrote:
> Brad Roberts wrote:
>> module test;
>>
>> void main()
>> {
>> int[] foos;
>> func1(foos);
>> }
>>
>> void func1(T)(const T[] foos)
>> {
>> T afoo;
>> func2(foos, afoo);
>> }
>>
>> void func2(T)(const T[] foos, out T afoo)
>> {
>> }
>>
>> $ dmd -c t
John Travers wrote:
Hi,
I'm investigating if D would be useful to me as a numerical programming
language to replace my current mix of fortran and python.
Welcome! There are quite a few of us who are numerical programmers here,
and it's one of D's target areas.
I'm stuck with a problem whi
On 07.08.2010 23:04, Blonder wrote:
Hello,
the template!(...) mechanism I understand.
But I think I need the two classes, the first is the primary template
and the second the partial specialization (this is the end of the "loop"), or
can
I do this in D with functions?
The normal computation of
Am 07.08.2010 21:04, schrieb Blonder:
Hello,
the template!(...) mechanism I understand.
But I think I need the two classes, the first is the primary template
and the second the partial specialization (this is the end of the "loop"), or
can
I do this in D with functions?
The normal computation o
Dmitry Olshansky:
> Probably, this could be a starting point (BTW In D you can write
> templates very easy):
>
> T dot_product(size_t N, T)(T[] a, T[] b){
> static if (N == 1){
> return a[0] * b[0];
> }else{
> return a[0] * b[0] + dot_product!(N-1)(a[1..$],b[1..$]);
>
On Saturday 07 August 2010 05:42:53 Lutger wrote:
> Variant in phobos doesn't work well with being const, toHash and opEquals
> for example are not marked as const methods. I have tried to work around
> it with careful casting, but it is too unsafe and just too complex for
> something that is suppo
Jonathan M Davis wrote:
> On Saturday 07 August 2010 05:42:53 Lutger wrote:
>> Variant in phobos doesn't work well with being const, toHash and opEquals
>> for example are not marked as const methods. I have tried to work around
>> it with careful casting, but it is too unsafe and just too complex
Hello Simen,
simendsjo wrote:
Ok, thanks. Does this mean this equivalent then?
int[] a = [1,2,3];
int[] b = a[0..1];
assert(a !is b);
assert(a.ptr == b.ptr && a.length == b.length);
Well, no.
both asserts will fail (a.length == 3 != b.length == 1)
But what you probably mean, is.
( a is
Hello simendsjo,
The spec for array says:
Static arrays are value types. Unlike in C and D version 1, static
arrays are passed to functions by value. Static arrays can also be
returned by functions.
I don't get the "static arrays are passed to functions by value" part.
Here I am passing in a s
Hello!
I'm learning D and I was wondering on what can be optimized of my
implementation of a palindrome function:
bool isPalindrome(int num) {
string s = text(num);
int len = s.length;
for (int i = 0; i < len / 2; ++i) {
if (s[i] != s[len - 1 - i]) {
return false;
Sorry I forg to paste my last version"
bool isPalindrome(int num) {
string s = text(num);
int length = s.length;
int limit = length / 2;
for (int i = 0; i < limit; ++i) {
if (s[i] != s[$ - 1 - i]) {
return false;
}
}
return true;
}
On Saturday 07 August 2010 16:56:08 ex wrote:
> Hello!
> I'm learning D and I was wondering on what can be optimized of my
> implementation of a palindrome function:
>
> bool isPalindrome(int num) {
> string s = text(num);
> int len = s.length;
> for (int i = 0; i < len / 2; ++i) {
>
As a side note, I'd suggest making your function take a string of some kind, if
not outright making it a template to deal with multiple string types. That way,
you can check for a palindrome regardless of whether you're dealing with
numbers, and if you need to do it with a number, you just conve
ex:
Welcome to D :-) Few more comments beside the ones written by Jonathan M Davis.
> I'm learning D and I was wondering on what can be optimized of my
> implementation of a palindrome function:
See below.
> 2) Does using a variable to store the string length is more efficient than
> calling th
42 matches
Mail list logo