srfi.209
- Enums and enum sets ¶Enums are objects to represent a finite set of options. Traditionally Lisp-family languages have used symbols or keywords for the purpose, but it is error prone that you can pass arbitrary symbols which can only be checked at runtime.
The enums in this srfi allows the user to define and use enums statically, that is, checked at macro-expansion time.
There are three kind of objects involved in the enum system:
An enum type defines all possible enums that can belong to the type.
It can be created at macro-expansion time with define-enum
,
or at run-time with make-enum-type
. It is an immutable object.
An enum is an object that belong to a certain enum type. Each enum has a name (a symbol unique within the enum type), an ordinal (nonnegative contiguous exact integer), and a value (arbitrary Scheme value, defaults to the name). Enums are instantiated at the same time as an enum type is instantiated. It is also an immutable object.
An enum set is a subset of enums from the enums in a certain enum type. An efficient set operation over an enum set is provided.
[SRFI-209]{srfi.209
}
The high-level declarative interface. This defines a new
enum type and interface macros.
The enums the enum type contains are specified with name-value …,
each of which must be either a symbol, or a form (symbol value)
.
If it is a symbol, it is used both as the enum name and its value.
If it is (symbol value)
, the symbol is used as the enum name
and the value is used as its value.
The type-name argument must be an identifier and is bound to a macro, which takes a single symbol as an argument. When invoked, it expands into an enum object.
The make-set argument must also be an identifier and is bound to a macro, which takes zero or more symbols and expands into an enum set object.
Since type-name and make-set are macros, the given enum name(s) can be statically checked at expansion time.
(define-enum fruits (apple orange banana mango papaya) fruit-set) (fruits apple) ⇒ #<enum apple> (fruits mango) ⇒ #<enum mango> (fruit-set oragne mango papaya) ⇒ #<enum-set fruits (orange mango papaya)>
[SRFI-209]{srfi.209
}
A procedural interface to create enum type and associated enums.
This procdure first creates enums according to list.
Each element
must be either a symbol, or a form (symbol value)
.
If it’s the former, the symbol is used both as the enum name and
its value. If it’s the latter, the symbol is used as the enum name
and the value is used as its value.
Note that enum objects can only be created with this procedure
(or the high-level macro) along with an enum type.
Each enum also gets a nonnegative integer ordinal, in the order of list, starting from zero.
The optional name argument is a Gauche’s extension. It attaches name to the created enum-type. It is only for debugging aid, and printed when you write the enum type.
Enum type is immutable. Once created, its enum members cannot be changed.
(make-enum-type '(one two three)) ⇒ #<enum-type (one ...)> (make-enum-type '(one two three) 'counts) ⇒ #<enum-type counts>
[SRFI-209]{srfi.209
}
Returns #t
if obj is an enum-type, #f
otherwise.
[SRFI-209]{srfi.209
}
Returns the number of enums the enum type etype has.
[SRFI-209]{srfi.209
}
Returns the minimun and maximum enum the enum type etype has.
(define fruits (make-enum-type '(apple orange banana mango papaya))) (enum-min fruits) ⇒ #<enum apple> (enum-max fruits) ⇒ #<enum papaya>
They throw an error if etype has no enums.
[SRFI-209]{srfi.209
}
Returns a list of enums the enum type etype has, in the order
of their ordinals.
(define fruits (make-enum-type '(apple orange banana mango papaya))) (enum-type-enums fruits) ⇒ (#<enum apple> #<enum orange> #<enum banana> #<enum mango> #<enum papaya>)
[SRFI-209]{srfi.209
}
Returns a list of names of enums the enum type etype has, in the order
of their ordinals.
(define fruits (make-enum-type '(apple orange banana mango papaya))) (enum-type-names fruits) ⇒ (apple orange banana mango papaya)
[SRFI-209]{srfi.209
}
Returns a list of the values of the enums the enum type etype has,
in the order of their ordinals.
[SRFI-209]{srfi.209
}
Returns #t
iff enum type etype contains enum,
#f
otherwise.
[SRFI-209]{srfi.209
}
Returns an enum object in etype whose name is symbol.
If etype doesn’t have an enum of such name, #f
is returned.
[SRFI-209]{srfi.209
}
Returns n-th enum of etype.
If n is over the range of enums in etype, #f
is returned.
[SRFI-209]{srfi.209
}
Returns the ordinal of the enum in etype whose name is symbol.
An error is thrown if etype doesn’t have such symbol.
[SRFI-209]{srfi.209
}
Returns the value of the enum in etype whose name is symbol.
An error is thrown if etype doesn’t have such symbol.
[SRFI-209]{srfi.209
}
Returns the name of the n-th enum of etype.
An error is thrown if n is out of the range.
[SRFI-209]{srfi.209
}
Returns the value of the n-th enum of etype.
An error is thrown if n is out of the range.
[SRFI-209]{srfi.209
}
Returns a comparator that can compare two enums that belong to etype.
See srfi.114
- Comparators, for the details of comparators.
[SRFI-209]{srfi.209
}
Returns enum’s type (<enum-type>
instance), enum’s name (symbol),
enum’s ordinal (nonnegative exact integer), and enum’s value (any object),
respectively.
Note that enum objects can only be constructed with the enum type
it belongs (by define-enum
or make-enum-type
), and
they are immutable.
[SRFI-209]{srfi.209
}
Returns #t
if obj is an enum, #f
otherwise.
[SRFI-209]{srfi.209
}
These comapres enums by its ordinals. All the enums must belong
to the same enum type, or an error is thrown.
[SRFI-209]{srfi.209
}
Returns the next enum of enum, that is,
an enum that belongs to the same enum type as enum and
has an incremented ordinal. If enum is the last enum of
its type, #f
is returned.
[SRFI-209]{srfi.209
}
Returns the previous enum of enum, that is,
an enum that belongs to the same enum type as enum and
has a decremented ordinal. If enum is the first enum of
its type, #f
is returned.
[SRFI-209]{srfi.209
}
Returns an enum set that contains all enums in the enum type etype.
[SRFI-209]{srfi.209
}
Returns an enum set that contains enum … of an enum type
etype. An error is thrown unless all enum … belong
to etype.
[SRFI-209]{srfi.209
}
Returns an empty enum set for the enum type etype.
[SRFI-209]{srfi.209
}
Returns an enum set of an enum type etype, contaning enums
listed in enum-list. If enum-list contains anything
other than enums from etype, an error is thrown.
[SRFI-209]{srfi.209
}
The etype-or-eset argument must be an enum type or an enum set.
Returns a new enum set on an enum type etype-or-eset (if it is
an enum set, its enum type is used), which contains enums
having the same names as the names of enums contained in eset.
The eset’s enum type doesn’t need to be the same as the
result enum set.
[SRFI-209]{srfi.209
}
Returns a copy of an enum set eset.
[SRFI-209]{srfi.209
}
Returns #t
if obj is an enum set, #f
otherwise.
[SRFI-209]{srfi.209
}
Returns #t
if enum is a member of eset, #f
otherwise.
[SRFI-209]{srfi.209
}
Returns a#t
if eset is empty.
[SRFI-209]{srfi.209
}
Returns #t if two enum sets on the same enum type share
no common enums. An error is thrown unless eset1 and eset2
are for the same enum type..
[SRFI-209]{srfi.209
}
This determines subset relationship of two enum sets. Both enum sets
must be for the same enum type, or an error is thrown.
[SRFI-209]{srfi.209
}
Returns #t
if the names of the member of enum set eset1
is a subset of the names of the member of eset2. Otherwise
returns #f
. Unlike enum-set<?
, two enum set can be
of different enum types. Comparison is done with the names only.
[SRFI-209]{srfi.209
}
Returns #t
if any or every application of proc to the
elemens of enum set eset yields true. Otherwise returns #f
.
[SRFI-209]{srfi.209
}
Returns the enum type of the enum set eset.
[SRFI-209]{srfi.209
}
Returns the number of elements in the enum set eset.
[SRFI-209]{srfi.209
}
Returns a list of enums contained in the enum set eset,
in the increasing order of enumcs.
[SRFI-209]{srfi.209
}
Returns an enum set that contains all the members in the enum set eset
plus enum …. All enums must belong to the same enum type
of eset.
The linear update version enum-set-adjoin!
may modify eset
to produce the result.
[SRFI-209]{srfi.209
}
Returns an enum set that contains all the members in the enum set eset
except enum …. All enums must belong to the same enum type
of eset.
The linear update version enum-set-delete!
may modify eset
to produce the result.
[SRFI-209]{srfi.209
}
Returns an enum set that contains all the members in the enum set eset
the enums in list. An error is thrown if
list contains non-enums, or enums that do not belong to the same
enum type of eset.
The linear update version enum-set-delete-all!
may modify eset
to produce the result.
[SRFI-209]{srfi.209
}
Applies pred on each enum in eset, and returns the number
of times when pred yields a true value.
[SRFI-209]{srfi.209
}
Returns a new enum set that contains the enums that satisfy or
do not satisfy pred.
[SRFI-209]{srfi.209
}
Applies proc on each enum in eset,
and returns a list of the results, in the increasing order of enums.
[SRFI-209]{srfi.209
}
Applies proc on each enum in eset in the increasing order.
The results of proc are discarded.
[SRFI-209]{srfi.209
}
For each enum in eset,
apply kons on it and the current
state value, whose initial value is knil. The result
of kons is used as the state value of the next iteration.
The last result of kons is returned.
The eset is traversed in increasing order.
[SRFI-209]{srfi.209
}
Returns an enum set of the same enum type as eset, but
contains enums that are not contained in eset.
The linear updating version enum-set-complement!
may reuse
the storage of eset.
[SRFI-209]{srfi.209
}
Two enum sets must be of the same enum type.
Returns an enum set of the same type
that contains enums each of which is in at least
either of eset1 or eset2.
The linear updating version enum-set-union!
may reuse
the storage of eset1.
[SRFI-209]{srfi.209
}
Two enum sets must be of the same enum type.
Returns an enum set of the same type
that contains enums each of which is in both
eset1 and eset2.
The linear updating version enum-set-intersection!
may reuse
the storage of eset1.
[SRFI-209]{srfi.209
}
Two enum sets must be of the same enum type.
Returns an enum set of the same type
that contains enums each of which is in eset1
but not in eset2.
The linear updating version enum-set-difference!
may reuse
the storage of eset1.
[SRFI-209]{srfi.209
}
Two enum sets must be of the same enum type.
Returns an enum set of the same type
that contains enums each of which is in either eset1
or eset2, but not both.
The linear updating version enum-set-xor!
may reuse
the storage of eset1.
R6RS also has enumerations, but it does not have separate enum object, nor explicitly deal with enum types. Symbols are used to represent each enum, and enum types are implicitly dealt with enum sets.
[SRFI-209]{srfi.209
}
Creates a new enum type and defines a macro named type-name
and make-set. VEry similar to define-enum
, except that
the macro invocation (type-name symbol)
returns
the symbol itself instead of an enum object.
[SRFI-209]{srfi.209
}
Creates a new enum type with the enums each of which has the name
given in symbol-list. Returns a fresh enum set containing
all the enums. The created enum type ca be obtained with
enum-set-type
.
[SRFI-209]{srfi.209
}
Returns a fresh enum set that contains all the enums of the enum type
of the given enum set eset.
It is basically (enum-type->enum-set (enum-set-type eset))
.
[SRFI-209]{srfi.209
}
Returns a procedure that accepts a list of symbols as an argument,
and returns a fresh enum set that contains the enums whose names
are given in the list.
[SRFI-209]{srfi.209
}
Returns #t
if symbol is the name of a member of eset,
or #f
symbol is a valid name of the enum type of
eset but not in eset. An error is signaled
if symbol is not a valid name of enums in the enum type.
[SRFI-209]{srfi.209
}
Returns a procedure that takes one argument, a symbol, and
returns the ordinal of the enum that has the given symbol as a name.
If the given symbol is not a name of any enums in the enum type
of eset, #f
is returned. Note that the enum with the
given symbol does not need to be in eset; basically, eset
is used only to obtain the enum type.
[SRFI-209]{srfi.209
}
Returns a list of the names of the members of eset.
Same as (map enum-name (enum-set->enum-list eset))
.