comp.lang.ada
 help / color / mirror / Atom feed
* Avoiding side effects
@ 2008-09-30  5:50 Anders Wirzenius
  2008-09-30  7:48 ` Jean-Pierre Rosen
                   ` (2 more replies)
  0 siblings, 3 replies; 28+ messages in thread
From: Anders Wirzenius @ 2008-09-30  5:50 UTC (permalink / raw)


Another thread about functions and side effects triggered me to
ask advice how to solve a specific problem without using side effects.

Problem:
Two files A and B containing one code per line (actually a
material number). I want to have the difference between those
files (A-B), i.e. remove those lines in A that are mentioned in
B.
B may contain codes that are not present in A.
A and B contain up to 1.5 million lines each.

My solution:
I sort the files alphabetically and remove possible duplicates
(Unix commands sort and uniq).
I create two functions, Strip and Advance.
function Strip (Remove_This : in String) return String
   -- Displays rows until row > Remove_This.
function Advance (Candidate : in String) return String
   --  Advance in file until row > Candidate.
The main program is then merely a loop where I alternate between
advancing lines in the two files until the row in the file is
greater than the value to compare against:
...
loop
   Trans_String := To_Unbounded_String (Strip (To_String
    (Trans_String)));
   Trans_String := To_Unbounded_String (Advance (To_String
    (Trans_String)));
end loop;
The problem is how to deal with the situation when I reach the
end of the file. 
I have done it using a side effect:
EOF_Reached     : Boolean := False;
loop
   Trans_String...
   exit when EOF_Reached;
   Trans_String...
   exit when EOF_Reached;
end loop;
--  Display possible tail of file A.
EOF_Reached is set to True in the functions Strip and Advance as
a side effect.

Is there a better way without using the side effect EOF_Reached?

-- 
Anders



^ permalink raw reply	[flat|nested] 28+ messages in thread

end of thread, other threads:[~2008-10-14 14:58 UTC | newest]

Thread overview: 28+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-09-30  5:50 Avoiding side effects Anders Wirzenius
2008-09-30  7:48 ` Jean-Pierre Rosen
2008-09-30  9:20   ` Anders Wirzenius
2008-09-30 11:36     ` Jean-Pierre Rosen
2008-09-30 12:43       ` Anders Wirzenius
2008-09-30 10:42 ` Ludovic Brenta
2008-09-30 11:12   ` Anders Wirzenius
2008-10-02 12:56 ` Ole-Hjalmar Kristensen
2008-10-06 12:09   ` Anders Wirzenius
2008-10-07 11:08     ` Ole-Hjalmar Kristensen
2008-10-07 14:24       ` (see below)
2008-10-07 14:47         ` Anders Wirzenius
2008-10-07 14:51           ` (see below)
2008-10-08  5:16             ` Anders Wirzenius
2008-10-08  7:35               ` Dmitry A. Kazakov
2008-10-08 15:32               ` (see below)
2008-10-13 12:27                 ` Anders Wirzenius
2008-10-13 13:21                   ` Marco
2008-10-13 18:55                     ` (see below)
2008-10-14  7:30                       ` Dmitry A. Kazakov
2008-10-14 11:44                         ` Colin Paul Gloster
2008-10-13 18:49                   ` (see below)
2008-10-13 21:22                     ` Adam Beneschan
2008-10-13 21:53                       ` (see below)
2008-10-14  6:17                         ` mockturtle
2008-10-14 14:58                           ` Adam Beneschan
2008-10-14  6:22                     ` Avoiding side effects and other dogma Anders Wirzenius
2008-10-14 13:48                       ` (see below)

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox