www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - To write such an expressive code D

reply "Dennis Ritchie" <dennis.ritchie mail.ru> writes:
Good evening.
Is it possible to D something to replace the container on the F#, 
which displays the values of the sine from 0 to 90 degrees with 
an interval of 10 degrees:
let pi = Math.PI
let sins = [for x in 0.0..pi / 2.0 / 9.0..pi / 2.0 -> sin x]
sins.Dump()

Output:
0
0,17364817766693
0,342020143325699
0,5
0,642787609686539
0,766044443118978
0,866025403784439
0,939692620785908
0,984807753012208
1

P.S. Interested in code that will be as impressive as this. In 
General, I would like to see something akin to D.
Feb 09 2015
parent reply "Tobias Pankrath" <tobias pankrath.net> writes:
On Monday, 9 February 2015 at 19:40:42 UTC, Dennis Ritchie wrote:
 Good evening.
 Is it possible to D something to replace the container on the 
 F#, which displays the values of the sine from 0 to 90 degrees 
 with an interval of 10 degrees:
 let pi = Math.PI
 let sins = [for x in 0.0..pi / 2.0 / 9.0..pi / 2.0 -> sin x]
 sins.Dump()

 Output:
 0
 0,17364817766693
 0,342020143325699
 0,5
 0,642787609686539
 0,766044443118978
 0,866025403784439
 0,939692620785908
 0,984807753012208
 1

 P.S. Interested in code that will be as impressive as this. In 
 General, I would like to see something akin to D.
iota(0, 91, 10).map!sin.writeln or something like that.
Feb 09 2015
parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 02/09/2015 11:45 AM, Tobias Pankrath wrote:

 iota(0, 91, 10).map!sin.writeln

 or something like that.
Yes: :) import std.math; import std.stdio; import std.range; import std.algorithm; void main() { const beg = 0.0L; const interval = PI_2 / 9; const end = PI_2 + interval; auto sins = iota(beg, end, interval).map!sin; writefln("%(%.15g\n%)", sins); } 0 0.17364817766693 0.342020143325669 0.5 0.642787609686539 0.766044443118978 0.866025403784439 0.939692620785908 0.984807753012208 1 Ali
Feb 09 2015
next sibling parent "Dennis Ritchie" <dennis.ritchie mail.ru> writes:
Thank you, Tobias Pankrath and Ali Çehreli.
Feb 09 2015
prev sibling parent reply "Vladimir Panteleev" <vladimir thecybershadow.net> writes:
On Monday, 9 February 2015 at 19:57:23 UTC, Ali Çehreli wrote:
     writefln("%(%.15g\n%)", sins);
In 2.067, you can write: iota(0, PI/2, PI/2/9).map!sin.each!writeln;
Feb 09 2015
parent reply "Dennis Ritchie" <dennis.ritchie mail.ru> writes:
On Monday, 9 February 2015 at 20:03:00 UTC, Vladimir Panteleev 
wrote:
 On Monday, 9 February 2015 at 19:57:23 UTC, Ali Çehreli wrote:
    writefln("%(%.15g\n%)", sins);
In 2.067, you can write: iota(0, PI/2, PI/2/9).map!sin.each!writeln;
March 1!
Feb 09 2015
parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 02/09/2015 12:05 PM, Dennis Ritchie wrote:
 On Monday, 9 February 2015 at 20:03:00 UTC, Vladimir Panteleev wrote:
 On Monday, 9 February 2015 at 19:57:23 UTC, Ali Çehreli wrote:
    writefln("%(%.15g\n%)", sins);
In 2.067, you can write: iota(0, PI/2, PI/2/9).map!sin.each!writeln;
March 1!
Yes, but apparently D's default precision for output is less than F#'s so how about the following? :p "%(%.15g\n%)".writefln(iota(0, PI/2, PI/2/9).map!sin); Just for demonstration, I would not write anything like that but the following is fine because now the format becomes the second parameter: :) iota(0, PI/2, PI/2/9).map!sin.writeF("%(%.15g\n%)"); void writeF(R)(R range, string format) { return writefln(format, range); } Ali
Feb 09 2015
parent reply "Dennis Ritchie" <dennis.ritchie mail.ru> writes:
On Monday, 9 February 2015 at 20:16:45 UTC, Ali Çehreli wrote:
 Yes, but apparently D's default precision for output is less 
 than F#'s so how about the following? :p

     "%(%.15g\n%)".writefln(iota(0, PI/2, PI/2/9).map!sin);

 Just for demonstration, I would not write anything like that 
 but the following is fine because now the format becomes the 
 second parameter: :)

     iota(0, PI/2, PI/2/9).map!sin.writeF("%(%.15g\n%)");

 void writeF(R)(R range, string format)
 {
     return writefln(format, range);
 }
Ali, and you can write it without using the function "iota()" and map? "%(%.15g\n%)".writefln(iota(0, PI/2, PI/2/9).map!sin); I just need that code was only used features of the language without using library functions. You may only use the function sin().
Feb 09 2015
next sibling parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 02/09/2015 08:17 PM, Dennis Ritchie wrote:

 Ali, and you can write it without using the function "iota()" and map?
No because the a..b syntax is not a D language construct that we can use anywhere that it makes sense. It only works as number ranges inside foreach loops, when indexing slices, and case value ranges. Also no, because there is no equivalent of F#'s -> syntax, we have to use map.
 "%(%.15g\n%)".writefln(iota(0, PI/2, PI/2/9).map!sin);

 I just need that code was only used features of the language without
 using library functions. You may only use the function sin().
I am waiting to see a language solution that does not use even sin(). :o) Ali
Feb 09 2015
parent "Dennis Ritchie" <dennis.ritchie mail.ru> writes:
On Tuesday, 10 February 2015 at 06:17:17 UTC, Ali Çehreli wrote:
 On 02/09/2015 08:17 PM, Dennis Ritchie wrote:

 Ali, and you can write it without using the function "iota()"
and map? No because the a..b syntax is not a D language construct that we can use anywhere that it makes sense. It only works as number ranges inside foreach loops, when indexing slices, and case value ranges. Also no, because there is no equivalent of F#'s -> syntax, we have to use map.
 "%(%.15g\n%)".writefln(iota(0, PI/2, PI/2/9).map!sin);

 I just need that code was only used features of the language
without
 using library functions. You may only use the function sin().
I am waiting to see a language solution that does not use even sin(). :o) Ali
Thank you.
Feb 09 2015
prev sibling parent reply "Vladimir Panteleev" <vladimir thecybershadow.net> writes:
On Tuesday, 10 February 2015 at 04:17:48 UTC, Dennis Ritchie 
wrote:
 I just need that code was only used features of the language 
 without using library functions. You may only use the function 
 sin().
Why is that? Although D has a lot of language features, D tries to push functionality into the library as often as possible. This is better than having language features for everything, because you can then reimplement, tweak or replace said features by simply writing D code.
Feb 10 2015
parent reply "Dennis Ritchie" <dennis.ritchie mail.ru> writes:
On Tuesday, 10 February 2015 at 08:12:00 UTC, Vladimir Panteleev 
wrote:
 Why is that?
Потому что я спорил с одним упёртым человеком, которому не нравится D, на этом форуме: http://www.cyberforum.ru/holywars/thread1367892-page13.html Он просил меня написать такую программу с использованием только возможностей языка и функции sin(). Because I was arguing with one quiet a stubborn person who does not like D, on this forum: http://www.cyberforum.ru/holywars/thread1367892-page13.html He asked me to write such a program using only the language features and functions sin().
Feb 10 2015
next sibling parent "Tobias Pankrath" <tobias pankrath.net> writes:
On Tuesday, 10 February 2015 at 08:40:38 UTC, Dennis Ritchie 
wrote:
 On Tuesday, 10 February 2015 at 08:12:00 UTC, Vladimir 
 Panteleev wrote:
 Why is that?
Потому что я спорил с одним упёртым человеком, которому не нравится D, на этом форуме: http://www.cyberforum.ru/holywars/thread1367892-page13.html Он просил меня написать такую программу с использованием только возможностей языка и функции sin(). Because I was arguing with one quiet a stubborn person who does not like D, on this forum: http://www.cyberforum.ru/holywars/thread1367892-page13.html He asked me to write such a program using only the language features and functions sin().
How to win the holy language war: 1. Pick a feature that only one of the languages has 2. Pick a task that this feature solves neatly 3. Solve it using that feature 4. Forbid every other solution not involving the features that only your preferred language has. Done.
Feb 10 2015
prev sibling next sibling parent "Dicebot" <public dicebot.lv> writes:
On Tuesday, 10 February 2015 at 08:40:38 UTC, Dennis Ritchie 
wrote:
 Because I was arguing with one quiet a stubborn person who does 
 not like D, on this forum:
 http://www.cyberforum.ru/holywars/thread1367892-page13.html
 He asked me to write such a program using only the language 
 features and functions sin().
If someone makes stupid demands like this one to justify his dislike for the language, such person is either deliberate troll or has strong enough prejudice no never like language anyway, arguments or not. Language features don't magically appear from nowhere - those come at cost of extra code in compiler and/or runtime library making it very hard to use language with smaller runtime (D is actually guilty of that). It is a common practice to treat standard language library as part of language. Both C and C++ include detailed spec on standard library in official language spec for example. As such making any distinction between two is impractical.
Feb 10 2015
prev sibling parent reply ketmar <ketmar ketmar.no-ip.org> writes:
On Tue, 10 Feb 2015 08:40:36 +0000, Dennis Ritchie wrote:

 On Tuesday, 10 February 2015 at 08:12:00 UTC, Vladimir Panteleev wrote:
 Why is that?
=20 =D0=9F=D0=BE=D1=82=D0=BE=D0=BC=D1=83 =D1=87=D1=82=D0=BE =D1=8F =D1=81=D0=
=BF=D0=BE=D1=80=D0=B8=D0=BB =D1=81 =D0=BE=D0=B4=D0=BD=D0=B8=D0=BC =D1=83=D0= =BF=D1=91=D1=80=D1=82=D1=8B=D0=BC =D1=87=D0=B5=D0=BB=D0=BE=D0=B2=D0=B5=D0= =BA=D0=BE=D0=BC, =D0=BA=D0=BE=D1=82=D0=BE=D1=80=D0=BE=D0=BC=D1=83 =D0=BD=D0= =B5 =D0=BD=D1=80=D0=B0=D0=B2=D0=B8=D1=82=D1=81=D1=8F D,
 =D0=BD=D0=B0 =D1=8D=D1=82=D0=BE=D0=BC =D1=84=D0=BE=D1=80=D1=83=D0=BC=D0=
=B5:
 http://www.cyberforum.ru/holywars/thread1367892-page13.html =D0=9E=D0=BD =
=D0=BF=D1=80=D0=BE=D1=81=D0=B8=D0=BB
 =D0=BC=D0=B5=D0=BD=D1=8F =D0=BD=D0=B0=D0=BF=D0=B8=D1=81=D0=B0=D1=82=D1=8C=
=D1=82=D0=B0=D0=BA=D1=83=D1=8E =D0=BF=D1=80=D0=BE=D0=B3=D1=80=D0=B0=D0=BC= =D0=BC=D1=83 =D1=81 =D0=B8=D1=81=D0=BF=D0=BE=D0=BB=D1=8C=D0=B7=D0=BE=D0=B2= =D0=B0=D0=BD=D0=B8=D0=B5=D0=BC =D1=82=D0=BE=D0=BB=D1=8C=D0=BA=D0=BE =D0=B2= =D0=BE=D0=B7=D0=BC=D0=BE=D0=B6=D0=BD=D0=BE=D1=81=D1=82=D0=B5=D0=B9 =D1=8F= =D0=B7=D1=8B=D0=BA=D0=B0
 =D0=B8 =D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8=D0=B8 sin().
=20
 Because I was arguing with one quiet a stubborn person who does not like
 D, on this forum:
 http://www.cyberforum.ru/holywars/thread1367892-page13.html He asked me
 to write such a program using only the language features and functions
 sin().
'cause he is a dumb asshead, that's it.=
Feb 10 2015
parent reply "Dennis Ritchie" <dennis.ritchie mail.ru> writes:
Please help.

import std.stdio;
import std.stdio;

void main()
{
	/* return (a xor b xor c) */
	int nobitxor(int a, int b, int c) {
		return (a + b + c == 2 || a + b + c == 0) ? 0 : 1;
	}
	
	int a, b, c;
	
	a = b = c = 0;
	
	foreach (i; 0 .. 8) {
		if (i > 3)
			a = 1;
		if (i == 2 || i == 3 || i == 6 || i == 7)
			b = 1;
		if (i % 2)
			c = 1;
		writeln(a, b, c, ' ', nobitxor(a, b, c));
		a = b = c = 0;
	}
}

Output:

000 0
001 1
010 1
011 0
100 1
101 0
110 0
111 1

You need to function nobitxor(int a, int b, int c) not used 
bitwise/logical and mathematical operations.
Feb 10 2015
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Dennis Ritchie:

 Please help.
This starts to look like homework :-) Bye, bearophile
Feb 10 2015
next sibling parent "Dennis Ritchie" <dennis.ritchie mail.ru> writes:
On Tuesday, 10 February 2015 at 11:33:54 UTC, bearophile wrote:
 Dennis Ritchie:

 Please help.
This starts to look like homework :-) Bye, bearophile
This is not homework - this is a war of code on C#/F# and D. I've been programming in D, my opponent on F#/C#.
Feb 10 2015
prev sibling parent reply ketmar <ketmar ketmar.no-ip.org> writes:
On Tue, 10 Feb 2015 11:33:54 +0000, bearophile wrote:

 Dennis Ritchie:
=20
 Please help.
=20 This starts to look like homework :-)
it's much worse: meaningless pseudocomparison of different languages for=20 nothing.=
Feb 10 2015
parent reply "Dennis Ritchie" <dennis.ritchie mail.ru> writes:
On Tuesday, 10 February 2015 at 11:41:20 UTC, ketmar wrote:
 On Tue, 10 Feb 2015 11:33:54 +0000, bearophile wrote:

 Dennis Ritchie:
 
 Please help.
This starts to look like homework :-)
it's much worse: meaningless pseudocomparison of different languages for nothing.
This task can be solved for D?
Feb 10 2015
parent reply "Dennis Ritchie" <dennis.ritchie mail.ru> writes:
F#:

let f = function
     | 0 , 0 , 0 -> 0
     | 0 , 1 , 1 -> 0
     | 1 , 0 , 1 -> 0
     | 1 , 1 , 0 -> 0
     | _         -> 1

for a in 0..1 do
     for b in 0..1 do
         for c in 0..1 do
             printfn "%i xor %i xor %i = %i" a b c (f (a, b, c))

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).
Feb 10 2015
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
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: void main() { import std.stdio; foreach (immutable a; 0 .. 2) foreach (immutable b; 0 .. 2) foreach (immutable c; 0 .. 2) writefln("%d xor %d xor %d = %d", a, b, c, (a + b + c) % 2); } Alternative solution closer to the F# code: import std.stdio, std.algorithm, std.typecons; int f(T)(T t) if (isTuple!T) { return t.predSwitch( tuple(0, 0, 0), 0, tuple(0, 1, 1), 0, tuple(1, 0, 1), 0, tuple(1, 1, 0), 0, /*else*/ 1); } void main() { foreach (immutable a; 0 .. 2) foreach (immutable b; 0 .. 2) foreach (immutable c; 0 .. 2) writefln("%d xor %d xor %d = %d", a, b, c, tuple(a, b, c).f); } Bye, bearophile
Feb 10 2015
next sibling parent "Dennis Ritchie" <dennis.ritchie mail.ru> writes:
On Wednesday, 11 February 2015 at 00:56:03 UTC, bearophile wrote:
 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: void main() { import std.stdio; foreach (immutable a; 0 .. 2) foreach (immutable b; 0 .. 2) foreach (immutable c; 0 .. 2) writefln("%d xor %d xor %d = %d", a, b, c, (a + b + c) % 2); } Alternative solution closer to the F# code: import std.stdio, std.algorithm, std.typecons; int f(T)(T t) if (isTuple!T) { return t.predSwitch( tuple(0, 0, 0), 0, tuple(0, 1, 1), 0, tuple(1, 0, 1), 0, tuple(1, 1, 0), 0, /*else*/ 1); } void main() { foreach (immutable a; 0 .. 2) foreach (immutable b; 0 .. 2) foreach (immutable c; 0 .. 2) writefln("%d xor %d xor %d = %d", a, b, c, tuple(a, b, c).f); } Bye, bearophile
Thanks.
Feb 10 2015
prev sibling parent FG <home fgda.pl> writes:
On 2015-02-11 at 01:56, bearophile wrote:
 Alternative solution closer to the F# code:

 import std.stdio, std.algorithm, std.typecons;

 int f(T)(T t) if (isTuple!T) {
      return t.predSwitch(
          tuple(0, 0, 0), 0,
          tuple(0, 1, 1), 0,
          tuple(1, 0, 1), 0,
          tuple(1, 1, 0), 0,
          /*else*/ 1);
 }

 void main() {
      foreach (immutable a; 0 .. 2)
          foreach (immutable b; 0 .. 2)
              foreach (immutable c; 0 .. 2)
                  writefln("%d xor %d xor %d = %d", a, b, c, tuple(a, b, c).f);
 }
Why bend over and try to make it F#? Screw the F# guy. He was cheating with a switch, so why can't we cheat? foreach(i;0..8)writefln("%d xor %d xor %d = %s",!!(i&4),!!(i&2),!!(i&1),"01101001"[i]); Assimilate this! Oh wait, you needed a function. OK, here's a function (and just replace "01101001"[i] with xxor(i&4,i&2,i&1)): int xxor(int a, int b, int c) {return (a&&b&&c)||(!a&&!b&&c)||(!a&&b&&!c)||(a&&!b&&!c);} If it makes him dislike D even more, great! Mission accomplished. :)
Feb 10 2015