Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find a list of all functions in a Racket module?

Tags:

module

racket

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()?

like image 608
ben rudgers Avatar asked Jul 31 '14 15:07

ben rudgers


People also ask

What is a function in racket?

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.

How do you define a variable in a racket?

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)


2 Answers

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 ())))
like image 91
ben rudgers Avatar answered Sep 28 '22 00:09

ben rudgers


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.
like image 35
Asumu Takikawa Avatar answered Sep 27 '22 23:09

Asumu Takikawa