typestar

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

  1. TKit = set of TTool; builds a set type over the enumeration above it.
  2. Include(Kit, Pump) adds a value, Exclude(Kit, Patch) drops one, + unions two sets.
  3. if Pump in Kit then and not (ChainTool in Kit) decide what gets printed.

Keywords and builtins used here

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.

Type this snippet

Step 2 of 3 in Enums & sets, step 17 of 27 in Language basics.

← Previous Next →

Sets in other languages