unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
From: Arne Babenhauserheide <arne_bab@web.de>
To: source liu <sourceonly@gmail.com>
Cc: "guile-user@gnu.org" <guile-user@gnu.org>
Subject: Re: get all available symbols in scheme
Date: Mon, 13 Jun 2016 23:46:20 +0200	[thread overview]
Message-ID: <8737oghidv.fsf@web.de> (raw)
In-Reply-To: <CAAzVxgMFTTPLt9698dY89Z+1MKbd8_J9LyWusvtCZQ0q08=3YQ@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 2656 bytes --]

Hi,

Welcome to Guile!

source liu writes:

> I wonder if there is some way to dump all available symbols in current
> enviroment(something like “dir” in python), i think it is very useful when
> you are trying new modules

> I have tried the guile reference guide as well as google,but cant find any
> clue

It’s pretty hidden. The easiest way is activating readline and just
hitting tab twice:

    echo "(use-modules (ice-9 readline))(activate-readline)" >> ~/.guile

Essentially this:

(module-map (λ (sym var) sym) (resolve-interface '(guile)))


and

    ,use ; returns (guile-user)
    ,in (guile-user) ,use ; returns module listing
    ,in (guile) ,b ; returns the bindings
    ,in ...

Essentially this:

    (map (λ (x) (cons (module-name x) (module-map (λ (sym var) sym) (resolve-interface (module-name x))))) (module-uses (resolve-module '(guile-user))))


There actually isn’t a dir function, yet…

(import (ice-9 optargs))
(import (oop goops))
(use-modules (texinfo reflection))

; define basic dir
(define* (dir #:key (all? #f))
  (if all?
      (map (λ (x) (cons (module-name x)
                        (module-map (λ (sym var) sym) (resolve-interface (module-name x)))))
           (module-uses (current-module)))
      (module-map (λ (sym var) sym) (current-module))))
; add support for giving the module as argument
(define-generic dir)
(define-method (dir (all? <boolean>)) (dir #:all? all?))
(define-method (dir (m <list>)) (module-map (λ (sym var) sym) (resolve-interface m)))
; add support for using modules directly (interfaces are also modules, so this catches both)
(define-method (dir (m <module>)) (module-map (λ (sym var) sym) (resolve-interface (module-name m))))


Now there is dir, but its output is a bit unwieldy for large modules…


You can use it this:

(dir) ; all local bindings, excluding imported modules
(dir #t) ; all available bindings, including imported modules
(dir #:all? #t) ; same as above

(dir '(ice-9 optargs)) ; all exported bindings from the (ice-9 optargs) module
; => (let-optional* define* let-keywords let-keywords* define*-public defmacro* defmacro*-public let-optional lambda*)

(dir (resolve-module '(ice-9 optargs)) ; all bindings in the module
; => (let-optional* parse-lambda-case %module-public-interface let-keywords let-keywords* define*-public defmacro* defmacro*-public *uninitialized* let-optional vars&inits)


I just added this to guile-basics: http://www.draketo.de/proj/guile-basics/#sec-2-5


Best wishes,
Arne
-- 
Unpolitisch sein
heißt politisch sein
ohne es zu merken

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 818 bytes --]

  parent reply	other threads:[~2016-06-13 21:46 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-06-13 18:38 get all available symbols in scheme source liu
2016-06-13 19:22 ` Panicz Maciej Godek
2016-06-13 21:46 ` Arne Babenhauserheide [this message]
2016-06-14  6:42   ` tomas
2016-06-15 17:57     ` Performance implications of having many methods? " Arne Babenhauserheide
2016-06-13 22:07 ` Taylan Ulrich Bayırlı/Kammer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=8737oghidv.fsf@web.de \
    --to=arne_bab@web.de \
    --cc=guile-user@gnu.org \
    --cc=sourceonly@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).