www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Component Programming example

reply "Jonathan A Dunlap" <jdunlap outlook.com> writes:
The example:
http://www.drdobbs.com/architecture-and-design/component-programming-in-d/240008321?pgno=4

import std.stdio;
import std.array;
import std.algorithm;

     void main() {
         stdin.byLine(KeepTerminator.yes)    // 1
         map!(a => a.idup).                  // 2
         array.                              // 3
         sort.                               // 4
         copy(                               // 5
             stdout.lockingTextWriter());    // 6
     }

I don't understand what happens to the output. On windows, I can 
keep entering lines but no output gets displayed. Also, can 
someone explain a bit more about lockingTextWriter?

Thanks!
Aug 02 2013
next sibling parent reply Justin Whear <justin economicmodeling.com> writes:
On Fri, 02 Aug 2013 18:59:12 +0200, Jonathan A Dunlap wrote:

 The example:
 http://www.drdobbs.com/architecture-and-design/component-programming-in-
d/240008321?pgno=4
 
 import std.stdio;
 import std.array;
 import std.algorithm;
 
      void main() {
          stdin.byLine(KeepTerminator.yes)    // 1 map!(a => a.idup).    
                       // 2 array.                              // 3
          sort.                               // 4 copy(                 
                       // 5
              stdout.lockingTextWriter());    // 6
      }
 
 I don't understand what happens to the output. On windows, I can keep
 entering lines but no output gets displayed. Also, can someone explain a
 bit more about lockingTextWriter?
 
 Thanks!
1) The example has a typo; there should be a '.' between the byLine call and the array call. 2) The example collects all input before writing anything (so that it can sort). Hit your end-of-file character (Ctrl-D) for me to end the input. Or direct a file into the process' stdin (not sure how to do this on Windows, it's been so long).
Aug 02 2013
parent "Andre Artus" <andre.artus gmail.com> writes:
On Friday, 2 August 2013 at 17:03:44 UTC, Justin Whear wrote:
 On Fri, 02 Aug 2013 18:59:12 +0200, Jonathan A Dunlap wrote:

 The example:
 http://www.drdobbs.com/architecture-and-design/component-programming-in-
d/240008321?pgno=4
 
 import std.stdio;
 import std.array;
 import std.algorithm;
 
      void main() {
          stdin.byLine(KeepTerminator.yes)    // 1 map!(a => 
 a.idup).
                       // 2 array.                              
 // 3
          sort.                               // 4 copy(
                       // 5
              stdout.lockingTextWriter());    // 6
      }
 
 I don't understand what happens to the output. On windows, I 
 can keep
 entering lines but no output gets displayed. Also, can someone 
 explain a
 bit more about lockingTextWriter?
 
 Thanks!
1) The example has a typo; there should be a '.' between the byLine call and the array call.
void main() { stdin.byLine(KeepTerminator.yes) // 1 .map!(a => a.idup) // 2 '.' was missing start of this line .array // 3 .sort // 4 .copy( // 5 stdout.lockingTextWriter()); // 6 }
 2) The example collects all input before writing anything (so 
 that it can
 sort).  Hit your end-of-file character (Ctrl-D) for me to end 
 the input.
 Or direct a file into the process' stdin (not sure how to do 
 this on
 Windows, it's been so long).
On Windows you send EOF by using the Ctrl-Z key sequence.
Aug 03 2013
prev sibling parent Justin Whear <justin economicmodeling.com> writes:
On Fri, 02 Aug 2013 18:59:12 +0200, Jonathan A Dunlap wrote:
 I don't understand what happens to the output. On windows, I can keep
 entering lines but no output gets displayed. Also, can someone explain a
 bit more about lockingTextWriter?
 
 Thanks!
lockingTextWriter wraps stdout (or any other file) with an OutputRange that locks the file while writing. This ensures that thread-shared files (which stdout is) don't accidentally interleave; if you write a line, you'll get the same line unbroken in your output, even if other threads are trying to write to stdout.
Aug 02 2013