www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Am I doing it wrong?

reply Emil Madsen <sovende gmail.com> writes:
--0015174c16ac233c590491b43f5d
Content-Type: text/plain; charset=ISO-8859-1

So I wrote a program, to find prime numbers, just to check out this pure
thing;

http://gist.github.com/608493

However, the program has a runtime of about 5 seconds? - in my mind, if the
function is pure, shouldn't the compiler insure that it was evaluated at
compiletime? - or am I doing it wrong?

-- 
// Yours sincerely
// Emil 'Skeen' Madsen

--0015174c16ac233c590491b43f5d
Content-Type: text/html; charset=ISO-8859-1

<div>So I wrote a program, to find prime numbers, just to check out this pure
thing;<br clear="all"></div><div><br></div><div><a
href="http://gist.github.com/608493">http://gist.github.com/608493</a></div><div><br></div><div>
However, the program has a runtime of about 5 seconds? - in my mind, if the
function is pure, shouldn&#39;t the compiler insure that it was evaluated at
compiletime? - or am I doing it wrong?</div><br>-- <br>// Yours sincerely<br>
// Emil &#39;Skeen&#39; Madsen<br>

--0015174c16ac233c590491b43f5d--
Oct 03 2010
next sibling parent reply "Denis Koroskin" <2korden gmail.com> writes:
On Sun, 03 Oct 2010 14:54:06 +0400, Emil Madsen <sovende gmail.com> wrote:

 So I wrote a program, to find prime numbers, just to check out this pure
 thing;

 http://gist.github.com/608493

 However, the program has a runtime of about 5 seconds? - in my mind, if  
 the
 function is pure, shouldn't the compiler insure that it was evaluated at
 compiletime? - or am I doing it wrong?

Make your result an "enum" (i.e. compile-time constant) if you really want to calculate it in compile-time: enum primes = calcPrimes();
Oct 03 2010
next sibling parent reply =?UTF-8?B?IkrDqXLDtG1lIE0uIEJlcmdlciI=?= <jeberger free.fr> writes:
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

Emil Madsen wrote:
 can the enum be a float? if calcprimes returned a float? - and if so, w=

 the enum be a float or an int? (will it be casted, or will it work as a=

 auto type?)
 =20

(although the "auto" part can be replaced by an explicit type). Jerome --=20 mailto:jeberger free.fr http://jeberger.free.fr Jabber: jeberger jabber.fr
Oct 03 2010
parent =?UTF-8?B?IkrDqXLDtG1lIE0uIEJlcmdlciI=?= <jeberger free.fr> writes:
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

Simen kjaeraas wrote:
 J=C3=A9r=C3=B4me M. Berger <jeberger free.fr> wrote:
=20
 Emil Madsen wrote:
 can the enum be a float? if calcprimes returned a float? - and if so,=



 will
 the enum be a float or an int? (will it be casted, or will it work as=



 auto type?)

(although the "auto" part can be replaced by an explicit type).

Or, you know, just "compile-time const", as the lack of a specified typ=

 and const being a storage class, allows for type inference. Auto is not=

 necessary.
=20

felt it necessary to put the redundant "auto" in my explanation. Jerome --=20 mailto:jeberger free.fr http://jeberger.free.fr Jabber: jeberger jabber.fr
Oct 04 2010
prev sibling parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
Emil Madsen wrote:
 ah ofc! I shoulda know :) - So I were doing it wrong :)

Thanks for the question; I learned something too. :) But in case you don't already know, there is also the D.learn newsgroup. This question might be more useful there. Ali
 Say I'm doing that enum a = calcPrimes();
 then a will be an enum with 1 element, that I can use as an int right?
 - or is there something special to be aware of?
 
 On 3 October 2010 13:20, Torarin <torarind gmail.com> wrote:
 
 2010/10/3 Emil Madsen <sovende gmail.com>:
 Well the result is assigned to an immutable int, shouldn't that be a

 const too?

not change. You can still assign run-time values to it: void main(string[] args) { immutable string a = args[0]; writeln(a); }


Oct 03 2010
prev sibling next sibling parent Emil Madsen <sovende gmail.com> writes:
--00151748def4da099f0491b4729e
Content-Type: text/plain; charset=ISO-8859-1

Well the result is assigned to an immutable int, shouldn't that be a compile
const too?

2010/10/3 Denis Koroskin <2korden gmail.com>

 On Sun, 03 Oct 2010 14:54:06 +0400, Emil Madsen <sovende gmail.com> wrote:

  So I wrote a program, to find prime numbers, just to check out this pure
 thing;

 http://gist.github.com/608493

 However, the program has a runtime of about 5 seconds? - in my mind, if
 the
 function is pure, shouldn't the compiler insure that it was evaluated at
 compiletime? - or am I doing it wrong?

to calculate it in compile-time: enum primes = calcPrimes();

-- // Yours sincerely // Emil 'Skeen' Madsen --00151748def4da099f0491b4729e Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Well the result is assigned to an immutable int, shouldn&#39;t that be a co= mpile const too?<br><br><div class=3D"gmail_quote">2010/10/3 Denis Koroskin= <span dir=3D"ltr">&lt;<a href=3D"mailto:2korden gmail.com">2korden gmail.c= om</a>&gt;</span><br> <blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p= x #ccc solid;padding-left:1ex;"><div class=3D"im">On Sun, 03 Oct 2010 14:54= :06 +0400, Emil Madsen &lt;<a href=3D"mailto:sovende gmail.com" target=3D"_= blank">sovende gmail.com</a>&gt; wrote:<br> <br> <blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p= x #ccc solid;padding-left:1ex"> So I wrote a program, to find prime numbers, just to check out this pure<br=

<br> <a href=3D"http://gist.github.com/608493" target=3D"_blank">http://gist.git= hub.com/608493</a><br> <br> However, the program has a runtime of about 5 seconds? - in my mind, if the= <br> function is pure, shouldn&#39;t the compiler insure that it was evaluated a= t<br> compiletime? - or am I doing it wrong?<br> <br> </blockquote> <br></div> Make your result an &quot;enum&quot; (i.e. compile-time constant) if you re= ally want to calculate it in compile-time:<br> <br> enum primes =3D calcPrimes();<br> </blockquote></div><br><br clear=3D"all"><br>-- <br>// Yours sincerely<br>/= / Emil &#39;Skeen&#39; Madsen<br> --00151748def4da099f0491b4729e--
Oct 03 2010
prev sibling next sibling parent "Denis Koroskin" <2korden gmail.com> writes:
On Sun, 03 Oct 2010 15:08:33 +0400, Emil Madsen <sovende gmail.com> wrote:

 Well the result is assigned to an immutable int, shouldn't that be a  
 compile
 const too?

No
Oct 03 2010
prev sibling next sibling parent Torarin <torarind gmail.com> writes:
2010/10/3 Emil Madsen <sovende gmail.com>:
 Well the result is assigned to an immutable int, shouldn't that be a compile
 const too?

Immutable means that the variable, or the memory it points to, will not change. You can still assign run-time values to it: void main(string[] args) { immutable string a = args[0]; writeln(a); }
Oct 03 2010
prev sibling next sibling parent Emil Madsen <sovende gmail.com> writes:
--00c09f8c21b110cdef0491b4a94a
Content-Type: text/plain; charset=ISO-8859-1

ah ofc! I shoulda know :) - So I were doing it wrong :)
Say I'm doing that enum a = calcPrimes();
then a will be an enum with 1 element, that I can use as an int right?
- or is there something special to be aware of?

On 3 October 2010 13:20, Torarin <torarind gmail.com> wrote:

 2010/10/3 Emil Madsen <sovende gmail.com>:
 Well the result is assigned to an immutable int, shouldn't that be a

 const too?

Immutable means that the variable, or the memory it points to, will not change. You can still assign run-time values to it: void main(string[] args) { immutable string a = args[0]; writeln(a); }

-- // Yours sincerely // Emil 'Skeen' Madsen --00c09f8c21b110cdef0491b4a94a Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable <div>ah ofc! I shoulda know :) - So I were doing it wrong :)<br></div><div>= Say I&#39;m doing that enum a =3D calcPrimes();</div><div>then a will be an= enum with 1 element, that I can use as an int right?</div><div>- or is the= re something special to be aware of?</div> <br><div class=3D"gmail_quote">On 3 October 2010 13:20, Torarin <span dir= =3D"ltr">&lt;<a href=3D"mailto:torarind gmail.com">torarind gmail.com</a>&g= t;</span> wrote:<br><blockquote class=3D"gmail_quote" style=3D"margin:0 0 0= .8ex;border-left:1px #ccc solid;padding-left:1ex;"> 2010/10/3 Emil Madsen &lt;<a href=3D"mailto:sovende gmail.com">sovende gmai= l.com</a>&gt;:<br> <div class=3D"im">&gt; Well the result is assigned to an immutable int, sho= uldn&#39;t that be a compile<br> &gt; const too?<br> <br> </div>Immutable means that the variable, or the memory it points to, will<b= r> not change. You can still assign run-time values to it:<br> void main(string[] args)<br> {<br> =A0immutable string a =3D args[0];<br> =A0writeln(a);<br> }<br> </blockquote></div><br><br clear=3D"all"><br>-- <br>// Yours sincerely<br>/= / Emil &#39;Skeen&#39; Madsen<br> --00c09f8c21b110cdef0491b4a94a--
Oct 03 2010
prev sibling next sibling parent Torarin <torarind gmail.com> writes:
2010/10/3 Emil Madsen <sovende gmail.com>:
 ah ofc! I shoulda know :) - So I were doing it wrong :)
 Say I'm doing that enum a = calcPrimes();
 then a will be an enum with 1 element, that I can use as an int right?

Yes, effectively you are declaring an anonymous enum with one element. By default an int.
Oct 03 2010
prev sibling next sibling parent Emil Madsen <sovende gmail.com> writes:
--00c09f8c21b1acc0dd0491b4cf93
Content-Type: text/plain; charset=ISO-8859-1

can the enum be a float? if calcprimes returned a float? - and if so, will
the enum be a float or an int? (will it be casted, or will it work as an
auto type?)

thanks btw :)

On 3 October 2010 13:28, Torarin <torarind gmail.com> wrote:

 2010/10/3 Emil Madsen <sovende gmail.com>:
 ah ofc! I shoulda know :) - So I were doing it wrong :)
 Say I'm doing that enum a = calcPrimes();
 then a will be an enum with 1 element, that I can use as an int right?

Yes, effectively you are declaring an anonymous enum with one element. By default an int.

-- // Yours sincerely // Emil 'Skeen' Madsen --00c09f8c21b1acc0dd0491b4cf93 Content-Type: text/html; charset=ISO-8859-1 <div>can the enum be a float? if calcprimes returned a float? - and if so, will the enum be a float or an int? (will it be casted, or will it work as an auto type?)<br></div><div><br></div><div>thanks btw :)</div><br><div class="gmail_quote"> On 3 October 2010 13:28, Torarin <span dir="ltr">&lt;<a href="mailto:torarind gmail.com">torarind gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"> <div class="im">2010/10/3 Emil Madsen &lt;<a href="mailto:sovende gmail.com">sovende gmail.com</a>&gt;:<br> </div><div class="im">&gt; ah ofc! I shoulda know :) - So I were doing it wrong :)<br> &gt; Say I&#39;m doing that enum a = calcPrimes();<br> &gt; then a will be an enum with 1 element, that I can use as an int right?<br> <br> </div>Yes, effectively you are declaring an anonymous enum with one element.<br> By default an int.<br> </blockquote></div><br><br clear="all"><br>-- <br>// Yours sincerely<br>// Emil &#39;Skeen&#39; Madsen<br> --00c09f8c21b1acc0dd0491b4cf93--
Oct 03 2010
prev sibling next sibling parent Jonathan M Davis <jmdavisProg gmx.com> writes:
On Sunday 03 October 2010 04:34:31 Emil Madsen wrote:
 can the enum be a float? if calcprimes returned a float? - and if so, will
 the enum be a float or an int? (will it be casted, or will it work as an
 auto type?)

auto, enum, immutable, and const all use type inference. So, you can declare auto a = 7; enum b = 7.7; immutable c = "hello"; const d = false; enum, however, is the only one which is a compile-time constant. - Jonathan M Davis
Oct 03 2010
prev sibling next sibling parent Emil Madsen <sovende gmail.com> writes:
--0015174c3e4a87aa860491b51042
Content-Type: text/plain; charset=ISO-8859-1

perfect :), thanks :)

On 3 October 2010 13:39, Jonathan M Davis <jmdavisProg gmx.com> wrote:

 On Sunday 03 October 2010 04:34:31 Emil Madsen wrote:
 can the enum be a float? if calcprimes returned a float? - and if so,

 the enum be a float or an int? (will it be casted, or will it work as an
 auto type?)

auto, enum, immutable, and const all use type inference. So, you can declare auto a = 7; enum b = 7.7; immutable c = "hello"; const d = false; enum, however, is the only one which is a compile-time constant. - Jonathan M Davis

-- // Yours sincerely // Emil 'Skeen' Madsen --0015174c3e4a87aa860491b51042 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable perfect :), thanks :)<br><br><div class=3D"gmail_quote">On 3 October 2010 1= 3:39, Jonathan M Davis <span dir=3D"ltr">&lt;<a href=3D"mailto:jmdavisProg = gmx.com">jmdavisProg gmx.com</a>&gt;</span> wrote:<br><blockquote class=3D"= gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-= left:1ex;"> <div class=3D"im">On Sunday 03 October 2010 04:34:31 Emil Madsen wrote:<br> &gt; can the enum be a float? if calcprimes returned a float? - and if so, = will<br> &gt; the enum be a float or an int? (will it be casted, or will it work as = an<br> &gt; auto type?)<br> <br> <br> </div>auto, enum, immutable, and const all use type inference. So, you can = declare<br> <br> auto a =3D 7;<br> enum b =3D 7.7;<br> immutable c =3D &quot;hello&quot;;<br> const d =3D false;<br> <br> enum, however, is the only one which is a compile-time constant.<br> <font color=3D"#888888"><br> - Jonathan M Davis<br> </font></blockquote></div><br><br clear=3D"all"><br>-- <br>// Yours sincere= ly<br>// Emil &#39;Skeen&#39; Madsen<br> --0015174c3e4a87aa860491b51042--
Oct 03 2010
prev sibling next sibling parent Emil Madsen <sovende gmail.com> writes:
--0015174bf1fe8f54f10491bd455b
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

So I've been told, but havn't been able to find it, so I rechecked, and
there it was, apperently I'm getting blind >.<
I'll make sure not to pollute this mailing list with these questions again
then :) - Thanks once again

On 3 October 2010 23:28, Ali =C7ehreli <acehreli yahoo.com> wrote:

 Emil Madsen wrote:

 ah ofc! I shoulda know :) - So I were doing it wrong :)

Thanks for the question; I learned something too. :) But in case you don't already know, there is also the D.learn newsgroup. This question might be more useful there. Ali Say I'm doing that enum a =3D calcPrimes();
 then a will be an enum with 1 element, that I can use as an int right?
 - or is there something special to be aware of?

 On 3 October 2010 13:20, Torarin <torarind gmail.com> wrote:

  2010/10/3 Emil Madsen <sovende gmail.com>:
 Well the result is assigned to an immutable int, shouldn't that be a

 const too?

not change. You can still assign run-time values to it: void main(string[] args) { immutable string a =3D args[0]; writeln(a); }



--=20 // Yours sincerely // Emil 'Skeen' Madsen --0015174bf1fe8f54f10491bd455b Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable <div>So I&#39;ve been told, but havn&#39;t been able to find it, so I reche= cked, and there it was, apperently I&#39;m getting blind &gt;.&lt;<br></div=
<div>I&#39;ll make sure not to pollute this mailing list with these questi=

<br><div class=3D"gmail_quote">On 3 October 2010 23:28, Ali =C7ehreli <span= dir=3D"ltr">&lt;<a href=3D"mailto:acehreli yahoo.com">acehreli yahoo.com</= a>&gt;</span> wrote:<br><blockquote class=3D"gmail_quote" style=3D"margin:0= 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"> <div class=3D"im">Emil Madsen wrote:<br> <blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p= x #ccc solid;padding-left:1ex"> ah ofc! I shoulda know :) - So I were doing it wrong :)<br> </blockquote> <br></div> Thanks for the question; I learned something too. :)<br> <br> But in case you don&#39;t already know, there is also the D.learn newsgroup= . This question might be more useful there.<br><font color=3D"#888888"> <br> Ali<br> <br> </font><blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-= left:1px #ccc solid;padding-left:1ex"><div class=3D"im"> Say I&#39;m doing that enum a =3D calcPrimes();<br> then a will be an enum with 1 element, that I can use as an int right?<br><= /div><div class=3D"im"> - or is there something special to be aware of?<br> <br></div><div><div class=3D"h5"> On 3 October 2010 13:20, Torarin &lt;<a href=3D"mailto:torarind gmail.com" = target=3D"_blank">torarind gmail.com</a>&gt; wrote:<br> <br> <blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p= x #ccc solid;padding-left:1ex"> 2010/10/3 Emil Madsen &lt;<a href=3D"mailto:sovende gmail.com" target=3D"_b= lank">sovende gmail.com</a>&gt;:<br> <blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p= x #ccc solid;padding-left:1ex"> Well the result is assigned to an immutable int, shouldn&#39;t that be a<br=

compile<br> <blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p= x #ccc solid;padding-left:1ex"> const too?<br> </blockquote> Immutable means that the variable, or the memory it points to, will<br> not change. You can still assign run-time values to it:<br> void main(string[] args)<br> {<br> =A0immutable string a =3D args[0];<br> =A0writeln(a);<br> }<br> <br> </blockquote> <br> <br> <br> </div></div></blockquote> </blockquote></div><br><br clear=3D"all"><br>-- <br>// Yours sincerely<br>/= / Emil &#39;Skeen&#39; Madsen<br> --0015174bf1fe8f54f10491bd455b--
Oct 03 2010
prev sibling parent "Simen kjaeraas" <simen.kjaras gmail.com> writes:
J=C3=A9r=C3=B4me M. Berger <jeberger free.fr> wrote:

 Emil Madsen wrote:
 can the enum be a float? if calcprimes returned a float? - and if so,=


 will
 the enum be a float or an int? (will it be casted, or will it work as=


 auto type?)

(although the "auto" part can be replaced by an explicit type).

Or, you know, just "compile-time const", as the lack of a specified type= , and const being a storage class, allows for type inference. Auto is not necessary. -- = Simen
Oct 03 2010