Logo

Programming-Idioms

  • Dart
  • D

Idiom #71 Echo program implementation

Basic implementation of the Echo program: Print all arguments except the program name, separated by space, followed by newline.
The idiom demonstrates how to skip the first argument if necessary, concatenate arguments as strings, append newline and print it to stdout.

import std.stdio : writeln;
import std.algorithm.iteration : joiner;
import std.range : dropOne;
void main(string[] args)
{
	writeln(args.dropOne.joiner(" "));
}

.dropOne removes the first element of args, which usually is the program name.

.joiner does not allocate memory like .join, but loses features like random-access to the string and the result has to be written char-by-char to stdout.
import std.string, std.stdio;
void main(string[] args)
{
	args[1..$].join(" ").writeln;
}

Because we're guaranteed to have a least one argument (the program name), we can slice from index 1 to the end of the array, possibly returning an empty slice.
main(List<String> args) {
  print(args.join(' '));
}
with Ada.Command_Line; use Ada.Command_Line;
with Ada.Text_IO; use Ada.Text_IO;
procedure Main is
begin
   for I in 1 .. Argument_Count loop
      Put (Argument (I) & (if I = Argument_Count then "" else " "));
   end loop;
   
   New_Line;
end Main;

New implementation...
< >
christianhujer