I know I can read the documentation, but because that requires switching my attention away from my editor and the REPL, I'd like to be able to see a list of functions provided the modules I am using.
Is there something in Racket analogous to Ruby's Foo.methods()
?
A function is a set of expressions that's only evaluated when explicitly invoked at run time. Optionally, a function can take arguments as input, and return values as output.
In Racket, a variable is a name which is bound to some data object (using a pointer). There are no type declarations for variables. The rule for evaluating symbols: a symbol evaluates to the value of the variable it names. We can bind variables using the special form define: (define symbol expression)
The function module->exports
provides a list of names from a module provide
expression.
> (require racket/date)
> (module->exports 'racket/date) ;module name is passed not the module
'()
'((0
(julian/scalinger->string ())
(date->julian/scalinger ())
(find-seconds ())
(date-display-format ())
(date->string ())
(date*->seconds ())
(date->seconds ())
(current-date ())))
reference: racket discussion list
Because module->exports
returns two values, define-values
is needed to capture the list of functions and the list of macros for use elsewhere:
; my-module.rkt
#lang racket
(provide (all-defined-out))
(define-syntax while
#|
Macros do not allow documentation strings
While macro from StackOverflow.
http://stackoverflow.com/questions/10968212/while-loop-macro-in-drracket
|#
(syntax-rules ()
((_ pred? stmt ...)
(do () ((not pred?))
stmt ...))))
(define my-const 9)
(define (prn a)
"Prints the passed value on a new line"
(printf "\n~a" a))
(define (my-func a)
"Another documentation string"
(prn a))
Referenced in:
;some-other-file.rkt
#lang racket
(require "my-module.rkt")
(define-values (functions-and-constants macros)
(module->exports '"my-module.rkt"))
In REPL:
> functions-and-constants
'((0 (my-const ()) (my-func ()) (prn ())))
> macros
'((0 (while ())))
If you use xrepl
, you can use the describe
command to get more usefully formatted information:
Welcome to Racket v6.1.0.3.
-> ,describe racket/date
; `racket/date' is a module,
; located at <collects>/racket/date.rkt
; imports: <collects>/racket/base.rkt, <collects>/racket/contract/base.rkt,
; <collects>/racket/promise.rkt.
; direct syntax exports: current-date, date*->seconds,
; date->julian/scalinger, date->seconds, date->string, date-display-format,
; find-seconds, julian/scalinger->string.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With