On this page:
->boolean
->string
->number
->inexact
->exact
->integer
->list
->vector
->symbol
->keyword
->bytes
->char
->stream
->generator
->set
->syntax
->code
->values

3 Type Transformers

 (require relation/transform) package: Relation

Generic utilities for transforming data into different types.

The type transformers provided by Racket out of the box are type-specific; for instance in order to convert data into a string, we would use symbol->string if the data is a symbol, and number->string if the data is a number. Likewise, converting a number to an integer from a more precise form, or vice versa, typically involves multiple steps and the method varies depending on the number’s type.

This module provides convenient interfaces to perform many such common type conversions, while keeping them agnostic to the source type. If mutable and immutable versions of a data type exist, these interfaces will return the immutable version.

See also: Sugar.

procedure

(->boolean v)  boolean?

  v : any/c
Maps the input data to a boolean. Note that in Racket, out of the box, anything that isn’t #f is treated as #t, including, for instance, the empty string and the number 0.

Examples:
> (->boolean 42)

#t

> (->boolean #f)

#f

> (->boolean "apple")

#t

procedure

(->string v)  string?

  v : any/c
Maps the input data to a string.

Examples:
> (->string 42)

"42"

> (->string 'apple)

"apple"

> (->string '(1 2 3))

"(1 2 3)"

> (->string ID)

""

procedure

(->number v)  number?

  v : any/c
Maps the input data to a number.

Examples:
> (->number "42")

42

> (->number #\a)

97

procedure

(->inexact v)  inexact?

  v : any/c
Maps the input data to an inexact number.

Examples:
> (->inexact 3/2)

1.5

> (->inexact "42")

42.0

procedure

(->exact v)  exact?

  v : any/c
Maps the input data to an exact number.

Examples:
> (->exact 1.5)

3/2

> (->exact "42")

42

procedure

(->integer v [#:round round])  integer?

  v : any/c
  round : (one-of/c 'up 'down 'nearest) = 'down
Maps the input data to an integer.

Examples:
> (->integer "42")

42

> (->integer 3/2)

1

> (->integer 3/2 #:round 'up)

2

> (->integer 3.6 #:round 'nearest)

4

procedure

(->list v)  list?

  v : any/c
Maps the input data to a list.

Examples:
> (->list "apple")

'(#\a #\p #\p #\l #\e)

> (->list #(1 2 3))

'(1 2 3)

> (->list (stream 1 2 3))

'(1 2 3)

> (->list (hash 'a 1 'b 2 'c 3))

'((a . 1) (c . 3) (b . 2))

procedure

(->vector v)  vector?

  v : any/c
Maps the input data to a vector.

Examples:
> (->vector "apple")

'#(#\a #\p #\p #\l #\e)

> (->vector '(1 2 3))

'#(1 2 3)

> (->vector (stream 1 2 3))

'#(1 2 3)

> (->vector (hash 'a 1 'b 2 'c 3))

'#((a . 1) (c . 3) (b . 2))

procedure

(->symbol v)  symbol?

  v : any/c
Maps the input data to a symbol.

Examples:
> (->symbol "apple")

'apple

> (->symbol '#:apple)

'apple

procedure

(->keyword v)  keyword?

  v : any/c
Maps the input data to a keyword.

Examples:
> (->keyword "apple")

'#:apple

> (->keyword 'apple)

'#:apple

procedure

(->bytes v)  bytes?

  v : any/c
Maps the input data to a byte string.

Examples:
> (->bytes "apple")

#"apple"

> (->bytes '(97 112 112 108 101))

#"apple"

procedure

(->char v)  char?

  v : any/c
Maps the input data to a character.

Examples:
> (->char "a")

#\a

> (->char 97)

#\a

> (->char 'a)

#\a

procedure

(->stream v)  stream?

  v : any/c
Maps the input data to a stream.

Examples:
> (->stream "apple")

#<stream>

> (->stream '(97 112 112 108 101))

'(97 112 112 108 101)

procedure

(->generator v return)  generator?

  v : any/c
  return : any/c
Maps the input data to a generator. If a return value is provided, it will be used as the return value of the generator once the sequence v has been exhausted. Any sequence can be transformed into a generator, and vice versa. This allows us to leverage sequence patterns for generators in a natural way, for instance cons-ing and extending generators to produce additional values by transforming them into streams and then back again.

Note that, owing to the stateful nature of the underlying generator, it’s possible that a stream constructed from a generator would continue to provide lazy evaluation but not take up constant memory. On the other hand, a stream to generator conversion should not incur any additional memory overhead.

Another thing to be wary of with a generator to stream conversion is that since the underlying generator is mutable, independent invocations of the generator after the stream has been constructed would affect the sequence represented by the stream, which is likely to result in unexpected behavior. In general it is advisable to manipulate stateful entities such as generators via a single common interface, whether that is, in the present case, the generator itself directly, or the stream representation of it – but not both.

Examples:
> (->generator "apple")

(generator #<procedure:generator>)

> (->generator '(97 112 112 108 101))

(generator #<procedure:generator>)

> (->list (->generator (conj (->stream (->generator '(1 2 3))) 4)))

'(4 1 2 3)

procedure

(->set v)  set?

  v : any/c
Maps the input data to a set.

Examples:
> (->set "apple")

(immutable-custom-set #f       '#hash((#\l . #t) (#\p . #t) (#\a . #t) (#\e . #t)))

> (->set '(1 2 2 3 3 3))

(immutable-custom-set #f       '#hash((1 . #t) (3 . #t) (2 . #t)))

procedure

(->syntax v)  syntax?

  v : any/c
Maps the input data to a syntax object.

Examples:
> (->syntax "apple")

#<syntax "apple">

> (->syntax 42)

#<syntax 42>

> (->syntax '(+ 1 2))

#<syntax (+ 1 2)>

procedure

(->code v)  any/c

  v : any/c
Maps the input data (for instance, a syntax object in a macro definition) to a datum, that is, to an elementary syntactic representation, the literal "code."

Examples:
> (->code "apple")

"apple"

> (->code #'42)

42

> (->code #'(+ 1 2))

'(+ 1 2)

> (->code #'(define (square x) (* x x)))

'(define (square x) (* x x))

> (eval (->code #'((λ (x) (* x x)) 4)))

16

procedure

(->values v)  values?

  v : any/c
Maps the input data to a set of values.

Examples:
> (->values #(1 2 3))

1

2

3

> (->values '(1 2 3))

1

2

3

> (->values "apple")

#\a

#\p

#\p

#\l

#\e