Sets in Delphi
A bike repair kit held as a set of TTool, edited and then questioned with in.
program SetsMembership;
type
TTool = (TireLever, Pump, ChainTool, Patch, Wrench);
TKit = set of TTool; // a set holds any combination of those values
var
Kit: TKit;
begin
Kit := [TireLever, Patch];
Include(Kit, Pump); // Include adds one value, Exclude removes one
Exclude(Kit, Patch);
Kit := Kit + [Wrench]; // + unions two sets, - subtracts one
if Pump in Kit then // in asks whether a value is a member
WriteLn('can reinflate');
if not (ChainTool in Kit) then
WriteLn('no chain repairs');
end.
How it works
TKit = set of TTool;builds a set type over the enumeration above it.Include(Kit, Pump)adds a value,Exclude(Kit, Patch)drops one,+unions two sets.if Pump in Kit thenandnot (ChainTool in Kit)decide what gets printed.
Keywords and builtins used here
ExcludeIncludeWriteLnbeginendifinnotofprogramsetthentypevar
The run, in numbers
- Lines
- 16
- Characters to type
- 520
- Tokens
- 95
- Three-star pace
- 70 tpm
At the three-star pace of 70 tokens a minute, this run takes about 81 seconds.
Step 2 of 3 in Enums & sets, step 17 of 27 in Language basics.