On this page:
keyset?
keyset
empty-keyset
keyset-contains?
keyset-index-of
keyset-ref
keyset-size
keyset-add
keyset-remove
in-keyset
into-keyset
for/  keyset
for*/  keyset
keyset->list
list->keyset
keyset->set
set->keyset
7.4

4.9 Keysets

 (require rebellion/collection/keyset)
  package: rebellion

A keyset is a data structure representing an immutable sorted set of keywords. Keysets can be more efficient than generic sorted set implementations. For example, the keyset constructor is a macro that sorts the keywords at compile-time.

procedure

(keyset? v)  boolean?

  v : any/c
A predicate for keysets. Implies sequence?.

syntax

(keyset keyword ...)

Constructs a keyset containing each keyword, excluding duplicates. The keywords are sorted by keyword<? at compile-time, when the keyset form is expanded. The expanded code of a keyset form runs in linear time.

Examples:
> (keyset #:banana #:orange #:apple #:grape)

(keyset #:apple #:banana #:grape #:orange)

> (keyset #:orange #:banana #:banana)

(keyset #:banana #:orange)

value

empty-keyset : keyset? = (keyset)

The empty keyset, which contains no keywords.

procedure

(keyset-contains? keys kw)  boolean?

  keys : keyset?
  kw : keyword?
Returns #t if kw is present in keys. This is a constant-time operation.

Examples:
> (define fruits (keyset #:banana #:orange #:apple #:grape))
> (keyset-contains? fruits '#:orange)

#t

> (keyset-contains? fruits '#:walnut)

#f

procedure

(keyset-index-of keys kw)  natural?

  keys : keyset?
  kw : keyword?
Returns the position of kw in keys. This is a constant-time operation.

Examples:
> (define fruits (keyset #:banana #:orange #:apple #:grape))
> (keyset-index-of fruits '#:grape)

2

> (keyset-index-of fruits '#:banana)

1

procedure

(keyset-ref keys pos)  keyword?

  keys : keyset?
  pos : natural?
Returns the keyword at position pos in keys. This is a constant-time operation.

Examples:
> (define fruits (keyset #:banana #:orange #:apple #:grape))
> (keyset-ref fruits 0)

'#:apple

> (keyset-ref fruits 3)

'#:orange

procedure

(keyset-size keys)  natural?

  keys : keyset?
Returns the number of keywords in keys. This is a constant-time operation.

Examples:
> (define fruits (keyset #:banana #:orange #:apple #:grape))
> (keyset-size fruits)

4

procedure

(keyset-add keys kw)  keyset?

  keys : keyset?
  kw : keyword?
Adds kw to keys, returning the modified keyset.

Examples:
> (define fruits (keyset #:banana #:orange #:apple #:grape))
> (keyset-add fruits '#:peach)

(keyset #:apple #:banana #:grape #:orange #:peach)

procedure

(keyset-remove keys kw)  keyset?

  keys : keyset?
  kw : keyword?
Removes kw from keys, returning the modified keyset.

Examples:
> (define fruits (keyset #:banana #:orange #:apple #:grape))
> (keyset-remove fruits '#:banana)

(keyset #:apple #:grape #:orange)

procedure

(in-keyset keys)  (sequence/c keyword?)

  keys : keyset?
Returns a sequence of the keywords in keys, in ascending order.

Examples:
> (define fruits (keyset #:banana #:orange #:apple #:grape))
> (for/set ([kw (in-keyset fruits)])
    (keyword->immutable-string kw))

(set "banana" "grape" "apple" "orange")

A reducer that collects keywords into a keyset.

Example:
> (reduce into-keyset '#:apple '#:orange '#:banana)

(keyset #:apple #:banana #:orange)

syntax

(for/keyset (for-clause ...) body-or-break ... body)

 
  body : keyword?
Iterates like for, but collects each body which must evaluate to a keyword — into a keyset.

Example:
> (for/keyset ([str (in-list (list "hello" "darkness" "my" "old" "friend"))])
    (string->keyword str))

(keyset #:darkness #:friend #:hello #:my #:old)

syntax

(for*/keyset (for-clause ...) body-or-break ... body)

 
  body : keyword?
Iterates like for*, but collects each body which must evaluate to a keyword — into a keyset.

Example:
> (for*/keyset ([str (in-list (list "hello" "darkness" "my" "old" "friend"))])
    (string->keyword str))

(keyset #:darkness #:friend #:hello #:my #:old)

procedure

(keyset->list keys)  (listof keyword?)

  keys : keyset?
Converts keys into a plain list of keywords.

Examples:
> (define greek-gods (keyset #:zeus #:hera #:hades #:athena #:poseidon))
> (keyset->list greek-gods)

'(#:athena #:hades #:hera #:poseidon #:zeus)

procedure

(list->keyset kws)  keyset?

  kws : (listof keyword?)
Sorts kws and removes duplicates, returning a keyset.

Example:
> (list->keyset (list '#:banana '#:orange '#:orange '#:apple '#:grape))

(keyset #:apple #:banana #:grape #:orange)

procedure

(keyset->set keys)  (immutable-set/c keyword?)

  keys : keyset?
Converts keys into a plain set of keywords.

Example:
> (keyset->set (keyset #:red #:blue #:green #:yellow))

(set '#:yellow '#:blue '#:green '#:red)

procedure

(set->keyset kw-set)  keyset?

  kw-set : (immutable-set/c keyword?)
Sorts kw-set, returning a keyset.

Example:
> (set->keyset (set '#:red '#:blue '#:green '#:yellow))

(keyset #:blue #:green #:red #:yellow)