Re: to auto or not to auto ( in foreach )

2016-07-16 Thread Dennis Ritchie via Digitalmars-d-learn
On Saturday, 16 July 2016 at 14:00:56 UTC, dom wrote: foreach(auto v; msg) writeln(v); gives an error that a basic type is expected foreach(v; msg) writeln(v); works .. but why? `Note: The ForeachTypeAttribute is implicit, and when a type is not specified, it is inferred. In that case

Visual D prematurely closes the console

2015-02-02 Thread Dennis Ritchie via Digitalmars-d-learn
Hi. Visual D are settings in the project parameter Subsystem mode Console and command line with output still closes prematurely. How to make the command line was not closed prematurely without using system("pause")?

Re: Visual D prematurely closes the console

2015-02-02 Thread Dennis Ritchie via Digitalmars-d-learn
On Monday, 2 February 2015 at 22:14:36 UTC, Ali Çehreli wrote: http://stackoverflow.com/questions/454681/how-to-keep-the-console-window-open-in-visual-c Not helped: http://i.imgur.com/4EG84YK.png

Re: Visual D prematurely closes the console

2015-02-02 Thread Dennis Ritchie via Digitalmars-d-learn
On Monday, 2 February 2015 at 23:08:13 UTC, FrankLike wrote: Use monoD do a hello world ,you will get the answer. And Mono-D good debugger?

Re: Visual D prematurely closes the console

2015-02-02 Thread Dennis Ritchie via Digitalmars-d-learn
I found the right option! http://imgur.com/KfkuBZi

To write such an expressive code D

2015-02-09 Thread Dennis Ritchie via Digitalmars-d-learn
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,3420

Re: To write such an expressive code D

2015-02-09 Thread Dennis Ritchie via Digitalmars-d-learn
Thank you, Tobias Pankrath and Ali Çehreli.

Re: To write such an expressive code D

2015-02-09 Thread Dennis Ritchie via Digitalmars-d-learn
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!

How to write similar code D?

2015-02-09 Thread Dennis Ritchie via Digitalmars-d-learn
Tell me, please, how to write similar С# code D: using System; using System.Linq; public class Test { public static void Main() { var query = Enumerable.Range(2, 10) .Select(c => new { Length = 2 * c, Height = c * c - 1, Hypotenuse = c * c + 1 }) .Select(x => str

Re: How to write similar code D?

2015-02-09 Thread Dennis Ritchie via Digitalmars-d-learn
On Tuesday, 10 February 2015 at 01:31:54 UTC, FG wrote: On 2015-02-10 at 01:41, bearophile wrote: auto query = iota(2, 12) .map!(c => Tuple!(int,"length", int,"height", int,"hypotenuse") (2 * c, c ^^ 2 - 1, c ^^ 2 + 1)) .

Re: To write such an expressive code D

2015-02-09 Thread Dennis Ritchie via Digitalmars-d-learn
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

Re: To write such an expressive code D

2015-02-09 Thread Dennis Ritchie via Digitalmars-d-learn
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

Re: To write such an expressive code D

2015-02-10 Thread Dennis Ritchie via Digitalmars-d-learn
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 Он просил меня написать такую программу с использованием толь

Re: To write such an expressive code D

2015-02-10 Thread Dennis Ritchie via Digitalmars-d-learn
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;

Re: To write such an expressive code D

2015-02-10 Thread Dennis Ritchie via Digitalmars-d-learn
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#.

Re: To write such an expressive code D

2015-02-10 Thread Dennis Ritchie via Digitalmars-d-learn
On Tuesday, 10 February 2015 at 11:41:20 UTC, ketmar wrote: On Tue, 10 Feb 2015 11:33:54 +, 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

Re: To write such an expressive code D

2015-02-10 Thread Dennis Ritchie via Digitalmars-d-learn
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 =

Re: To write such an expressive code D

2015-02-10 Thread Dennis Ritchie via Digitalmars-d-learn
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 i

DUSE_MYLIB12

2015-02-11 Thread Dennis Ritchie via Digitalmars-d-learn
Tell me, please, is it possible to set an arbitrary condition conditional compilation at the command prompt, type DUSE_MYLIB12 and in the code as: version(USE_MYLIB12) { . } And I didn't like Any DeclarationBlock or Statement that is not compiled in still must be syntactically corr

Re: DUSE_MYLIB12

2015-02-11 Thread Dennis Ritchie via Digitalmars-d-learn
Thanks.

dmd-2.067.0-b1

2015-02-13 Thread Dennis Ritchie via Digitalmars-d-learn
This is a bug? import std.stdio; void main() { int a = 0; writeln( (a < 10) ? a = 1 : a = 2 );// prints 2 writeln( (a < 10) ? a = 1 : (a = 2) ); // prints 1 } Even C++ output: 1 1

Re: dmd-2.067.0-b1

2015-02-13 Thread Dennis Ritchie via Digitalmars-d-learn
On Friday, 13 February 2015 at 13:25:55 UTC, Steven Schveighoffer wrote: On 2/13/15 7:38 AM, tcak wrote: On Friday, 13 February 2015 at 09:38:04 UTC, Dennis Ritchie wrote: This is a bug? import std.stdio; void main() { int a = 0; writeln( (a < 10) ? a = 1 : a = 2 );// prints 2

A specifier readf() for BigInt

2015-02-16 Thread Dennis Ritchie via Digitalmars-d-learn
Hi. And how to read Data from the input stream? import std.stdio; import std.bigint; void main() { BigInt n; readf(" %?", &n); writeln(n); }

Re: A specifier readf() for BigInt

2015-02-17 Thread Dennis Ritchie via Digitalmars-d-learn
On Tuesday, 17 February 2015 at 07:20:19 UTC, Ivan Kazmenko wrote: The readf function does not seem to support reading BigInts directly. However, you can read a string and construct a BigInt from it, either by std.conv.to or directly invoking the constructor: import std.algorithm, std.bigi

Dynamic scope in D

2015-02-25 Thread Dennis Ritchie via Digitalmars-d-learn
Good time of day. Tell me, please, does D dynamic scope? If there is, prevalite, please, an example of it's use, or give a link where you can read about dynamic scope.

Re: Dynamic scope in D

2015-02-25 Thread Dennis Ritchie via Digitalmars-d-learn
On Wednesday, 25 February 2015 at 16:04:54 UTC, Adam D. Ruppe wrote: No, D's variable scoping is always lexical. Thanks.

The site engine written in D

2015-03-01 Thread Dennis Ritchie via Digitalmars-d-learn
Prompt, please, where can I find the software engine written in D?

Re: The site engine written in D

2015-03-01 Thread Dennis Ritchie via Digitalmars-d-learn
On Monday, 2 March 2015 at 00:06:26 UTC, Ali Çehreli wrote: Do you mean vibe.d? http://vibed.org/ I was referring to the software engine written using the vibe.d. http://vibed.org/ written using the vibe.d?

Re: The site engine written in D

2015-03-02 Thread Dennis Ritchie via Digitalmars-d-learn
On Monday, 2 March 2015 at 08:49:04 UTC, Kagamin wrote: https://github.com/rejectedsoftware/vibelog https://github.com/rejectedsoftware/vibed.org - site itself https://github.com/rikkimax/Cmsed - CMS in D Thanks.

Incorrect display in Cyrillic Windows

2015-03-04 Thread Dennis Ritchie via Digitalmars-d-learn
Hi. It's normal for Windows? http://i.imgur.com/TEx4H3k.png import std.uni; import std.stdio; void main() { string s; foreach(ch; CodepointSet('А', 'Я' + 1, 'а', 'я' + 1).byCodepoint) s ~= ch; writeln(s); writeln("s[0] = ", s[0]); }

Re: Incorrect display in Cyrillic Windows

2015-03-04 Thread Dennis Ritchie via Digitalmars-d-learn
On Wednesday, 4 March 2015 at 12:14:01 UTC, Martin Krejcirik wrote: You have to set your console encoding to UTF-8 first. You can do it by command "chcp 65001" or by calling Windows API function: extern(Windows) BOOL SetConsoleOutputCP( UINT ); SetConsoleOutputCP( 65001 ); Also change your f

string-int[] array

2015-03-08 Thread Dennis Ritchie via Digitalmars-d-learn
Is it possible to create such an array in which you can store strings and numbers at the same time? string-int[] array = [4, "five"];

Re: string-int[] array

2015-03-08 Thread Dennis Ritchie via Digitalmars-d-learn
On Sunday, 8 March 2015 at 18:18:15 UTC, Baz wrote: import std.stdio; import std.typecons; alias T = Tuple!(string, int); void main(string[] args) { T[] tarr; tarr ~= T("a",65); tarr ~= T("b",66); writeln(tarr); } [Tuple!(string, int)("a", 65), Tuple!(string, int)("b", 66

Re: string-int[] array

2015-03-08 Thread Dennis Ritchie via Digitalmars-d-learn
On Sunday, 8 March 2015 at 18:38:02 UTC, Dennis Ritchie wrote: Thanks, will do. No, will not work. On Sunday, 8 March 2015 at 18:25:33 UTC, Baz wrote: mmmh maybe off-topic, you probably don't what pairs but either a string representing an int or an int, do you ? If so then an array of uni

Re: string-int[] array

2015-03-08 Thread Dennis Ritchie via Digitalmars-d-learn
On Sunday, 8 March 2015 at 18:54:43 UTC, Meta wrote: On Sunday, 8 March 2015 at 18:38:02 UTC, Dennis Ritchie wrote: On Sunday, 8 March 2015 at 18:18:15 UTC, Baz wrote: import std.stdio; import std.typecons; alias T = Tuple!(string, int); void main(string[] args) { T[] tarr; tarr ~= T("a",

Re: string-int[] array

2015-03-08 Thread Dennis Ritchie via Digitalmars-d-learn
On Sunday, 8 March 2015 at 21:18:31 UTC, Max Klyga wrote: OP is fighting a loosing battle in flame war on some obscure forum. F# enthusiast trolls OP into solving stupid puzzles that are trivial in F# (or any ML-family language) and clumsy in C-family languages. In language holy wars the only

Strange behavior of the function find() and remove()

2015-03-08 Thread Dennis Ritchie via Digitalmars-d-learn
This is normal behavior? import std.stdio; import std.algorithm; void main() { auto a = [3, 5, 8]; writeln(find(remove(a, 1), 5).length != 0); // prints false writeln(a); // prints [3, 8, 8] ??? }

Re: Strange behavior of the function find() and remove()

2015-03-08 Thread Dennis Ritchie via Digitalmars-d-learn
On Sunday, 8 March 2015 at 21:58:20 UTC, safety0ff wrote: On Sunday, 8 March 2015 at 21:34:25 UTC, Dennis Ritchie wrote: This is normal behavior? Yes it is normal, there are two potential points of confusion: - remove mutates the input range and returns a shortened slice to the range which e

std.stdio.writeln

2015-03-09 Thread Dennis Ritchie via Digitalmars-d-learn
Hi. Why prints only the last element? import std.stdio; void main() { (1, 2, 3).writeln; // prints 3 }

Re: std.stdio.writeln

2015-03-09 Thread Dennis Ritchie via Digitalmars-d-learn
On Monday, 9 March 2015 at 14:42:35 UTC, Adam D. Ruppe wrote: On Monday, 9 March 2015 at 14:38:41 UTC, Dennis Ritchie wrote: (1, 2, 3).writeln; // prints 3 that's not an array, use [1,2,3] for that. What you wrote is a parenthesis expression with comma expressions inside. The co

Parallelization of a large array

2015-03-10 Thread Dennis Ritchie via Digitalmars-d-learn
Hi. How to parallelize a large array to check for the presence of an element matching the value with the data? std.stdio; std.algorithm; std.parallelism; void main() { int[] a = new int[100]; foreach (i, ref elem; a) elem = i; /*if (find(parallel(

Re: Parallelization of a large array

2015-03-10 Thread Dennis Ritchie via Digitalmars-d-learn
On Tuesday, 10 March 2015 at 21:15:17 UTC, safety0ff wrote: On Tuesday, 10 March 2015 at 20:41:14 UTC, Dennis Ritchie wrote: Hi. How to parallelize a large array to check for the presence of an element matching the value with the data? Here's a simple method (warning: has pitfalls): import s

Re: Parallelization of a large array

2015-03-10 Thread Dennis Ritchie via Digitalmars-d-learn
On Tuesday, 10 March 2015 at 21:27:42 UTC, Dennis Ritchie wrote: Thanks. No, it does not suit me, because of the parallel array in a foreach loop there is no break. import std.stdio; import std.algorithm; import std.parallelism; void main() { int b = 2; auto a = [1, 2, 2,

Re: Parallelization of a large array

2015-03-10 Thread Dennis Ritchie via Digitalmars-d-learn
On Tuesday, 10 March 2015 at 22:11:57 UTC, Dennis Ritchie wrote: No, it does not suit me, because of the parallel array in a foreach loop there is no break. I already understood everything: found = true;

Re: Parallelization of a large array

2015-03-10 Thread Dennis Ritchie via Digitalmars-d-learn
On Tuesday, 10 March 2015 at 22:34:34 UTC, Ali Çehreli wrote: It is possible by accessing the actual range by chunks: import std.stdio; import std.algorithm; import std.parallelism; import std.range; import std.conv; void main() { const size_t elementCount = 895640; int[] a = iota(elem

Re: Parallelization of a large array

2015-03-10 Thread Dennis Ritchie via Digitalmars-d-learn
On Tuesday, 10 March 2015 at 22:43:08 UTC, Ali Çehreli wrote: The following is the program that does NOT use taskPool.map. I am also changing the name of a variable because the local 'chunks' looked like std.range.chunks. import std.stdio; import std.algorithm; import std.parallelism; import s

Re: chaining splitters

2015-03-10 Thread Dennis Ritchie via Digitalmars-d-learn
On Wednesday, 11 March 2015 at 00:00:39 UTC, dnoob wrote: Hello, I am parsing some text and I have the following; string text = "some very long text"; foreach(line; splitter(text, [13, 10])) { foreach(record; splitter(line, '*')) { foreach(field; splitter(record

Template. C++ to D

2015-03-11 Thread Dennis Ritchie via Digitalmars-d-learn
Hi. How to rewrite this code on D? #include #include template T foo(const T &val) { return val; } template T foo(const T &val, const U &...u) { return val + foo(u...); } int main() { std::cout << foo(std::string("some "), std::string("test")) << std::endl; // prints some test

Re: Template. C++ to D

2015-03-11 Thread Dennis Ritchie via Digitalmars-d-learn
Thank you all.

C++ to D

2015-03-11 Thread Dennis Ritchie via Digitalmars-d-learn
Hi. How to rewrite this in D to the handler method for the input parameter was determined on average in O(1)? #include #include #include #include class A { public: void foo(const std::string &s); protected: void foo1(const std::string &s); void foo2(const std::string &s);

Re: C++ to D

2015-03-11 Thread Dennis Ritchie via Digitalmars-d-learn
On Wednesday, 11 March 2015 at 16:08:22 UTC, Kagamin wrote: A hash table? See http://dlang.org/hash-map.html That is, the input is a string and, depending on what word it contains, is called one of the three methods of the class that this line handles. And this happens in average cons

Re: C++ to D

2015-03-11 Thread Dennis Ritchie via Digitalmars-d-learn
The same without classes in Lisp: (define (foo) (let ((foo1 (lambda (s) s)) (foo2 (lambda (s) (list->string (reverse (string->list s) (foo3 (lambda (s) (string-append s ", " s " " (lambda (in) (match in ("first" foo1) ("second" foo2) (

Re: C++ to D

2015-03-11 Thread Dennis Ritchie via Digitalmars-d-learn
Thank you very much, Ali Çehreli and FG.

Re: C++ to D

2015-03-11 Thread Dennis Ritchie via Digitalmars-d-learn
On Wednesday, 11 March 2015 at 18:10:55 UTC, FG wrote: And your point was...? I take it, "poor c++" is a hint. Don't compare apples to oranges. No, I forgot to remove from the foreign code.

Re: moving from c++ to D is easy?

2015-03-12 Thread Dennis Ritchie via Digitalmars-d-learn
On Thursday, 12 March 2015 at 13:01:31 UTC, ayush wrote: Is D a lot like c++? Enough. So should i focus on one or learn both together? You can study both together, although it is better to focus on one. Will I find learning D easy if I already know c++? Yes.

Re: moving from c++ to D is easy?

2015-03-12 Thread Dennis Ritchie via Digitalmars-d-learn
On Thursday, 12 March 2015 at 13:44:50 UTC, Daniel Kozák wrote: D is much easier to learn so I will start with it. And then you can try learn C++ if you still want and need it. Yes, but in D for beginners little literature, so I would recommend starting with C++.

Re: moving from c++ to D is easy?

2015-03-12 Thread Dennis Ritchie via Digitalmars-d-learn
On Thursday, 12 March 2015 at 14:47:22 UTC, ketmar wrote: there are alot of books on C++ 'cause C++ is insanely complicated and inconsistend. D design is *MUCH* better, so D doesn't need so many books teaching arcane art of programming. Well, in principle, can be started with a D, but persona

Re: moving from c++ to D is easy?

2015-03-12 Thread Dennis Ritchie via Digitalmars-d-learn
On Thursday, 12 March 2015 at 18:57:51 UTC, Ali Çehreli wrote: If you are a mortal like myself, you may find out years later that you are still at the midway point. Happened to me several times when I was learning C++. :) О, yeah.

Memoization in compile-time

2015-03-12 Thread Dennis Ritchie via Digitalmars-d-learn
Is it possible to run this code in compile-time? import std.stdio, std.functional; ulong fact(ulong n) { alias mfact = memoize!fact; return n < 2 ? 1 : n * mfact(n - 1); } void main() { writeln(fact(10)); } In CommonLisp variable *factorial-cache* available in CT, and

Re: Memoization in compile-time

2015-03-13 Thread Dennis Ritchie via Digitalmars-d-learn
On Friday, 13 March 2015 at 02:38:18 UTC, Rikki Cattermole wrote: You could assign it to e.g. an enum. Or force it over using meta-programming. And this code can be rewritten to D? template struct Factorial { enum { value = n * Factorial::value }; }; template <> struct Factorial<0> {

Re: Memoization in compile-time

2015-03-13 Thread Dennis Ritchie via Digitalmars-d-learn
On Friday, 13 March 2015 at 12:58:13 UTC, Meta wrote: On Friday, 13 March 2015 at 12:49:48 UTC, Dennis Ritchie wrote: On Friday, 13 March 2015 at 02:38:18 UTC, Rikki Cattermole wrote: You could assign it to e.g. an enum. Or force it over using meta-programming. And this code can be rewritten

Re: Memoization in compile-time

2015-03-13 Thread Dennis Ritchie via Digitalmars-d-learn
And you can somehow memoization stuff at compile time?

Re: Memoization in compile-time

2015-03-13 Thread Dennis Ritchie via Digitalmars-d-learn
On Friday, 13 March 2015 at 18:38:16 UTC, Ali Çehreli wrote: On 03/13/2015 11:28 AM, Ali Çehreli wrote: > enum int[] factorials = memoizeFactorials(N); Oops! That's generally a trap! The array better be 'static' because a manifest constant like 'enum factorials' would be inserted everywhere i

Re: Memoization in compile-time

2015-03-13 Thread Dennis Ritchie via Digitalmars-d-learn
On Friday, 13 March 2015 at 21:16:24 UTC, Ali Çehreli wrote: On 03/13/2015 12:09 PM, Dennis Ritchie wrote: > And you can make the same memoized variable was available at compile > time and at run time? :) Yes, run-time is always possible and if it can be computed at compile time, compile-time

A bug on the website dlang.org

2015-03-16 Thread Dennis Ritchie via Digitalmars-d-learn
I don't know where to write about a bug on the website, so I decided to write in this section. This page is not displayed correctly: http://dlang.org/phobos/std_format.html

Re: A bug on the website dlang.org

2015-03-16 Thread Dennis Ritchie via Digitalmars-d-learn
On Monday, 16 March 2015 at 20:03:36 UTC, Marc Schütz wrote: You can file bugs against component "websites" there. Thanks. This has already been fixed AFAIK, it's just not yet deployed. OK.

int-double auto array

2015-03-17 Thread Dennis Ritchie via Digitalmars-d-learn
Hi. To create int-double array I have to pre-initialize the auto array: import std.stdio; import std.algorithm; void main() { auto s = [5.31, 6]; s = s.remove(0, 1); double k; readf("%s", &k); // 17.32 s ~= k, s ~= 5, s ~= 1.125; writeln(s)

Re: int-double auto array

2015-03-17 Thread Dennis Ritchie via Digitalmars-d-learn
On Wednesday, 18 March 2015 at 04:00:04 UTC, Adam D. Ruppe wrote: So you can just do double[] s; Thanks. Yes, I overthink everything :)

Lazy functions, lazy arrays

2015-03-19 Thread Dennis Ritchie via Digitalmars-d-learn
Hi, Is it possible for D to create lazy functions, lazy arrays? Or in addition to the function arguments can't be lazy in D?

Re: Lazy functions, lazy arrays

2015-03-20 Thread Dennis Ritchie via Digitalmars-d-learn
For example, lazy int sum(int a = 3, int b = 5) { return a + b; } That is, if the function is not invoked, it should not be calculated at compile time.

Re: Lazy functions, lazy arrays

2015-03-20 Thread Dennis Ritchie via Digitalmars-d-learn
On Friday, 20 March 2015 at 10:38:17 UTC, John Colvin wrote: I don't understand what you mean. You mean a function that isn't compiled if it isn't used anywhere? Yes. That's exactly what I mean.

Re: Lazy functions, lazy arrays

2015-03-20 Thread Dennis Ritchie via Digitalmars-d-learn
Use case? No. I need to be able to make an array "factorials" is not evaluated, if I don't. import std.stdio; enum N = 15; static int[] factorials = memoizeFactorials(N); // lazy array? :) int[] memoizeFactorials(int n) { if (!__ctfe) { // Make sure that this function is never

Re: Lazy functions, lazy arrays

2015-03-20 Thread Dennis Ritchie via Digitalmars-d-learn
On Friday, 20 March 2015 at 14:27:06 UTC, John Colvin wrote: I made a mistake about the static variable and thread-local storage. immutable(int)[] factorials()() @property { static immutable int[N] results = memoizeFactorials(N); return results[]; } is the correct way to do it if you h

How to replace the keyword "nan" to check the contract assert?

2015-03-20 Thread Dennis Ritchie via Digitalmars-d-learn
Hi, How do I replace double.init? import std.stdio : writeln; import std.algorithm : uninitializedFill; void main() { double[] arr = new double[6]; uninitializedFill(arr[0 .. $ - 2], 3.25); writeln(arr); assert(arr == [3.25, 3.25, 3.25, 3.25, double.init, double.init])

Re: How to replace the keyword "nan" to check the contract assert?

2015-03-20 Thread Dennis Ritchie via Digitalmars-d-learn
On Saturday, 21 March 2015 at 04:53:20 UTC, Ali Çehreli wrote: nan cannot be used in comparisons. It is neither greater than nor less than any value. You cannot even compare it against itself. nan==nan would always be false. The solution is to call std.math.isNaN. In this case, you have to sp

The difference in string and char[], readf() and scanf()

2015-03-21 Thread Dennis Ritchie via Digitalmars-d-learn
Hi, Tell me, please, why this code works correctly always: import std.stdio; int n; readf("%s\n", &n); string s, t; readf("%s\n%s\n", &s, &t); And this code works correctly is not always: import std.stdio; readf("%s\n", &n); char[200010] s, t; scanf("%s%s", s.ptr, t.ptr); Data is entered o

Re: The difference in string and char[], readf() and scanf()

2015-03-21 Thread Dennis Ritchie via Digitalmars-d-learn
On Saturday, 21 March 2015 at 12:08:05 UTC, anonymous wrote: Please go into more detail about how it doesn't work. Task: http://codeforces.com/contest/527/problem/B?locale=en It works: char[200010] s, t; s = readln.strip; t = readln.strip; http://codeforces.com/contest/527/submission/1037739

Re: The difference in string and char[], readf() and scanf()

2015-03-21 Thread Dennis Ritchie via Digitalmars-d-learn
In C++ it is fully working: char s[25], t[25]; scanf("%s%s", s, t); http://codeforces.com/contest/527/submission/10376381?locale=en

Re: The difference in string and char[], readf() and scanf()

2015-03-21 Thread Dennis Ritchie via Digitalmars-d-learn
On Saturday, 21 March 2015 at 15:05:56 UTC, Ivan Kazmenko wrote: On Saturday, 21 March 2015 at 14:31:20 UTC, Dennis Ritchie wrote: In C++ it is fully working: char s[25], t[25]; scanf("%s%s", s, t); Indeed. And why in D copied only the first 32767 characters of the string? I'm more

Re: The difference in string and char[], readf() and scanf()

2015-03-21 Thread Dennis Ritchie via Digitalmars-d-learn
On Saturday, 21 March 2015 at 19:09:59 UTC, FG wrote: In what universe?! Which OS, compiler and architecture? On Saturday, 21 March 2015 at 19:09:59 UTC, FG wrote: In what universe?! Which OS, compiler and architecture? Windows 8.1 x64, dmd 2.066.1: import std.range, std.stdio; void main (

Re: The difference in string and char[], readf() and scanf()

2015-03-21 Thread Dennis Ritchie via Digitalmars-d-learn
On Saturday, 21 March 2015 at 23:00:46 UTC, Ivan Kazmenko wrote: On Saturday, 21 March 2015 at 16:34:44 UTC, Dennis Ritchie wrote: And why in D copied only the first 32767 characters of the string? I'm more days couldn't understand what was going on... To me, it looks like a bug somewhere, tho

BigInt and xor

2015-03-24 Thread Dennis Ritchie via Digitalmars-d-learn
Tell me, please, how can I replace this code? import std.conv : to; import std.bigint : BigInt; import std.string : format; import std.stdio : writeln; void main() { BigInt[10] bitArr; ulong n = 18_446_724_073_709_551_614U; bitArr[0] = format("%b", n).to!BigInt;

Re: BigInt and xor

2015-03-24 Thread Dennis Ritchie via Digitalmars-d-learn
On Tuesday, 24 March 2015 at 16:35:04 UTC, Ivan Kazmenko wrote: What exactly is not working? Everything works. I'm just a little forgotten properties of the operation xor. I just wanted to xor 1 each digit in the number of type BigInt, while I would like to store each number in the binary

Re: BigInt and xor

2015-03-24 Thread Dennis Ritchie via Digitalmars-d-learn
On Tuesday, 24 March 2015 at 17:35:14 UTC, matovitch wrote: xor it with -1 instead of 1. (-1 is store as 0xfff..f with the classic modular arithmetic) Thanks.

C# to D

2015-03-25 Thread Dennis Ritchie via Digitalmars-d-learn
Hi, Can you please tell how to rewrite this code to D? using System; using System.Linq; class Sort { static void Main() { 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.C

Re: C# to D

2015-03-25 Thread Dennis Ritchie via Digitalmars-d-learn
On Wednesday, 25 March 2015 at 19:01:43 UTC, bearophile wrote: One solution: Thanks. On Wednesday, 25 March 2015 at 19:03:27 UTC, bearophile wrote: 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. A mo

Re: C# to D

2015-03-25 Thread Dennis Ritchie via Digitalmars-d-learn
On Wednesday, 25 March 2015 at 20:09:53 UTC, bearophile wrote: This is still not very efficient (perhaps the last sorting has to be stable): void main() { import std.stdio, std.algorithm, std.typecons, std.array; [7, 5, 7, 3, 3, 5, 3, 3, 0, 3, 1, 1, 5, 1, 1, 1, 2, 2, 8, 5, 8, 8]

Re: gdc and ldc command line examples?

2015-03-30 Thread Dennis Ritchie via Digitalmars-d-learn
On Tuesday, 31 March 2015 at 04:59:35 UTC, Jeremy DeHaan wrote: Hey all, I am finally working on moving out of dmd territory and playing with gdc and ldc. I was hoping that I could get some links to some example command lines. I'm mainly interested command lines regarding linking to libraries

Mutual optimization of tail recursion does not work in D

2015-03-31 Thread Dennis Ritchie via Digitalmars-d-learn
Hi, This code does not work: import std.stdio; bool odd(int n); bool even(int n); bool even(int n) { if (n == 0) return true; else return odd(n - 1); } bool odd(int n) { if (n == 0) return false; else

Re: Mutual optimization of tail recursion does not work in D

2015-03-31 Thread Dennis Ritchie via Digitalmars-d-learn
On Tuesday, 31 March 2015 at 12:01:52 UTC, ketmar wrote: gdc does, as this is gcc backend optimisation. Thanks.

C++ to D

2015-04-01 Thread Dennis Ritchie via Digitalmars-d-learn
Hi, Please help rewrite this code to D: #include // Peano Arithmetic struct zero; template struct succ { }; template struct increment { using result = succ; }; template struct decrement; template struct decrement> { using result = T; }; template struct addition; temp

Re: C++ to D

2015-04-01 Thread Dennis Ritchie via Digitalmars-d-learn
On Wednesday, 1 April 2015 at 15:22:10 UTC, John Colvin wrote: Compile Time Function Evaluation (CTFE) is a very powerful tool to avoid having to enter in to all that C++ style mess. Yes, CTFE in D really cool. Thanks. I need to implement arithmetic (addition / subtraction) only use the type

Re: C++ to D

2015-04-02 Thread Dennis Ritchie via Digitalmars-d-learn
On Wednesday, 1 April 2015 at 17:51:40 UTC, John Colvin wrote: Don't really see the point. Here's a neat thing that's definitely cheating because although it stores the results in the type system, the arithmetic is done in constant-folding: struct Integer(int a){} template Value(T) { stati

C++ to D - recursion with std.variant

2015-04-03 Thread Dennis Ritchie via Digitalmars-d-learn
Hi, Is it possible to write on D recursion using std.variant? - #include #include struct Nil {}; auto nil = Nil{}; template struct Cons; template using List = boost::variant>>; template struct Cons { Cons(T val, List list) : head(val), tail(list) {} T head; L

Re: C++ to D - recursion with std.variant

2015-04-05 Thread Dennis Ritchie via Digitalmars-d-learn
On Sunday, 5 April 2015 at 09:48:01 UTC, thedeemon wrote: On Friday, 3 April 2015 at 16:46:08 UTC, Dennis Ritchie wrote: Hi, Is it possible to write on D recursion using std.variant? Using Algebraic from std.variant and some additional templates: http://dpaste.dzfl.pl/65afd3a7ce52 (taken from

Mixin and map

2015-04-05 Thread Dennis Ritchie via Digitalmars-d-learn
Hi, Can you please tell how to make map worked correctly. I want to program published [2, 3, 4, 5, 6]. - import std.stdio; import std.algorithm; string print(string s) { return `writeln(` ~ s ~ `);`; } void main() { auto arr = [1, 2, 3, 4, 5]; mixin(print(`arr.map

Re: Mixin and map

2015-04-05 Thread Dennis Ritchie via Digitalmars-d-learn
On Sunday, 5 April 2015 at 21:28:27 UTC, Gary Willoughby wrote: Post incrementing t is not storing the increments value. http://ideone.com/1gGnvP Thanks.

Strange behavior std.range.takeNone

2015-04-06 Thread Dennis Ritchie via Digitalmars-d-learn
Hi, Is it OK? - import std.stdio : writeln; import std.range : takeNone; void main() { auto s = takeNone("test"); s ~= 5; writeln(s); // prints ♣ } - Windows 8.1 x64, DMD 2.067.0

Re: Strange behavior std.range.takeNone

2015-04-06 Thread Dennis Ritchie via Digitalmars-d-learn
On Tuesday, 7 April 2015 at 02:24:00 UTC, Dennis Ritchie wrote: Is it OK? Although, perhaps, everything is fine. I just thought that creates takeNone not string type string, and the string array of type string[]. import std.stdio : writeln; void main() { string s; s ~= 5;

Re: Strange behavior std.range.takeNone

2015-04-07 Thread Dennis Ritchie via Digitalmars-d-learn
On Tuesday, 7 April 2015 at 08:49:58 UTC, Andrea Fontana wrote: Yes it is. takeNone() take a char from a string. So you are going to append a char (with code 5) on the next line. If you replace that line with: s ~= 65; it will print "A". (65 is ascii code for letter 'A') Thanks. I am awa

  1   2   3   >