parse-number-range
parse-number-range parses loop's convenient for-as-arithmetic syntax into 5 simple values:
from, to, limit-kind (:inclusive, :exclusive or nil if unbounded), by (step) and direction (+ or -)).
Further related utilities are provided.
Intended for easy implementation of analogous functionality in other constructs.
Here's the package name, along with 2 nicknames:
parse-number-rangeparse-numrangepnumrange
Explicit qualification of the exported symbols is recommended, and one might (:import-from) select symbols, but (:use) is strongly discouraged.
⚓
⚓
Here are the 5 "fundamental" values returned from parse and accepted by unparse:
- from
The form introduced by
:from,:upfromor:downfrom. Defaults to 0;- to
The form introduced by
:to,:upto,:downto,:belowor:above.nilmeans "unbounded";- limit-kind
:inclusiveor:exclusiveif there's a limit, ornilif unbounded;- by
The form introduced by
:by;- direction
+or-.
Some functions accept a keyword-policy &key argument, which must be either :strict or :loose. (from 1 to 10) would be valid with :loose but not with :strict, which demands (:from 1 :to 10).
Use of :loose is strongly discouraged, as it's an unnecessary and evil feature that typically results in arbitrary interning of symbols in arbitrary packages.
⚓
Function
parserange &key (keyword-policy:strict) (extrasp nil) (clause-kinds-p extrasp) (clause-keywords-p extrasp) (clauses-alist-p extrasp) => from to limit-kind by direction &key clause-kinds clause-keywords clauses-alistBy default,
parsereturns only the 5 "fundamental" values (see above):- from
Defaults to 0;
- to
Defaults to
nil;- limit-kind
Defaults to
nil(unbounded);- by
Defaults to 1;
- direction
Defaults to
+.
parsecan compute and return 3 additional pieces of information, on request:return value request-keyword description clause-kinds :clause-kinds-p The clause kinds that were present,
in the order they appeared in.clause-keywords :clause-keywords-p The clause keywords that were present,
in the order they appeared in.clauses-alist :clauses-alist-p Alist: (clause-kind . clause-keyword)
Function
unparsefrom to limit-kind by direction &key clause-kinds => range-
As you might have guessed, this does the reverse of
parse...unparseusesflags-to-keywordsinternally and thus inherits its biases.clause-kinds should be a list of up to 3 elements being a permutation of the clause-kinds
:from,:toand:by. This determines the order that the clauses will appear in, which might matter in the presence of side-effects.nildesignates the default order,(.:from:to:by)If the list has 0 or 1 elements, then the default order is used,
(.:from:to:by)If the list has 2 elements, then those clauses will appear in the order specified but no order is specified between the other elements and these ones and between the other elements among themselves. (Yeah, a less simplistic implementation would allow simpler and more useful semantics.)
If the list has 3 elements, then the order is completely specified, explicitly.
Function
canonicalizerange &key (clause-kinds:preserve) (keyword-policy:strict)This simply feeds the results of
parseintounparse. If clause-kinds is:preserve, then:clause-kinds-p twill be specified forparseand the resulting clause-kinds result will be fed tounparse. Else, the provided clause-kinds is fed directly tounparse.
⚓
Function
kindkeyword &key (errorp t) (keyword-policy:strict) => kind direction limit-kindThis function takes a
loopkeyword and returns the following information:Argument Return values keyword kind direction limit-kind :from :from nil nil :downfrom :from - nil :upfrom :from + nil :to :to nil :inclusive :upto :to + :inclusive :downto :to - :inclusive :below :to + :exclusive :above :to - :exclusive :by :by nil nil Function
flagskeyword &key (errorp t) (keyword-policy:strict) => direction limit-kindThis convenience function simply forwards to
kindand returns all values but the first.Function
flags-to-keywordsdirection limit-kind => from-keyword to-keyword-
This function does roughly the opposite of the KIND function. It returns a FROM-KEYWORD and a TO-KEYWORD (nil if unbounded) that properly indicate the supplied DIRECTION and LIMIT-KIND.
There are typically multiple valid sets of answers that could be returned. This function is currently biased as follows, with no way to request a different bias:
-
The direction and limit-kind will always be represented in the to-keyword, and never in the from-keyword, except for the following exception:
(flags-to-keywords '- :exclusive) => :downfrom, nil :to is always returned in preference to :upto.
-