Path: utzoo!attcan!uunet!lll-winken!ames!ucsd!orion.cf.uci.edu!oberon!sm.unisys.com!randvax!florman From: florman@randvax.UUCP (Bruce Florman) Newsgroups: comp.lang.eiffel Subject: Re: Posting on behalf of CNET users [Answer, part 1] Message-ID: <1910@randvax.UUCP> Date: 18 Mar 89 02:07:49 GMT References: <112@eiffel.UUCP> <114@eiffel.UUCP> Reply-To: florman@rand-unix.UUCP (Bruce Florman) Organization: Rand Corp., Santa Monica, Ca. Lines: 84 In article jwg1@bunny.gte.com (James W. Gish) writes: >Bertrand, I think you missed one part of the problem: what if I want >to have more than one action that may be applied to one type. For example, >"action" may be either "double" or "halve" or "triple" on sets of integers. > >I want to be able to do this without repeating the basic form of the >iteration through the set. >-- >Jim Gish >GTE Laboratories, Inc., Waltham, MA >CSNET: jgish@gte.com UUCP: ..!harvard!bunny!jwg1 The class defined below implements a variation on the solution that I proposed last week. It knows how to apply an arbitrary action to every item in a list. DEFERRED CLASS APPLICATOR [T] EXPORT apply_to_all FEATURE apply_to_one (item: T): T IS DEFERRED END; apply_to_all (set: LIST [T]) IS REQUIRE NOT set.Void; DO FROM set.start UNTIL set.offright LOOP set.change_value(apply_to_one(set.value)); set.forth; END; END; END -- CLASS APPLICATOR Now, in order to implement your "actions" you will define heirs to APPLICATOR and define apply_to_one routines which do the appropriate thing. For example, a class that knows how to perform your doubling action would look like this: CLASS INT_DOUBLER EXPORT apply_to_all INHERIT APPLICATOR [INTEGER] FEATURE apply_to_one (item: INTEGER): INTEGER IS DO Result := 2 * item END; END -- CLASS INT_DOUBLER To use this class you will have to declare some entities like these somewhere in your system: doubler: INT_DOUBLER; list1: LINKED_LIST [INTEGER]; list2: FIXED_LIST [INTEGER]; And you will then have instructions like the following somewhere within your system's executable code: doubler.apply_to_all(list1); doubler.apply_to_all(list2); As I said before, creating a whole new class to perform each action is a more verbose solution than that which could be achieved in languages which support first class routines. However, since these classes will each be quite small, it's not all that bad. Disclaimer: I ran these two classes through the compiler and got no error messages, but I haven't bothered to use them anywhere. -Bruce Florman florman@rand.org