[Prev][Next][Index][Thread]
Representing percentage
-
To: info-dylan@ai.mit.edu
-
Subject: Representing percentage
-
From: "Nimi" <nimi@better.tv>
-
Date: Sun, 28 Jan 2001 09:15:01 -0500 (EST)
-
Organization: Internet Gold, ISRAEL
-
Xref: traf.lcs.mit.edu comp.lang.dylan:13041
Hi,
I'm trying to find a simple way to represent percentage. I thought of doing
it like this:
define constant <percentage> = limited(<integer>, min: 0, max: 100);
But this is no good if I want to make a generic function that specializes on
both <integer> (for absolute values, say, the number of elements in in a
collection passed as another argument) and <percentage> (the relative size
of the other argument) -
define method foo(c :: <collection>, n :: <integer>)
return n elements of c;
end method;
define method foo(c :: <collection>, p :: <percentage>)
return p% of the size of c;
end method;
This problem is solved if I do it this way:
define class <percentage> (<number>)
slot p :: limited(<integer>, min: 0, max: 100);
end class;
but it doesn't seem very efficient. Is there a better way to do it?
Thanks,
Nimi Peleg
Follow-Ups: