clojure – 네임스페이스안에 정의되어 있는 함수 보기

클로저 개발을 하다 보면, 네임스페이스 안에 정의되어 있는 함수들을 보고 싶을 때가 있다.

LightTable이 아닌 autocomplete가 제공되지 않는 IDE에서 개발을 할때면, 아.. 뭐지? 공욕을 치룰때가 있다.

그럼 어떻게 네임스페이스안의 함수들을 볼 수 있을지 알아 보자.

1. keys 함수를 이용하여, 함수 시퀀스 반환하기.

user=> (keys (ns-publics 'foo))

그 외 ns- 많은 함수들을 제공하니 찾아서 실험해보도록 하자.

2. dir 함수를 이용하기. dir함수는 clojure.repl에 선언되어 있는 함수이다.

user=> (require 'clojure.repl)
user=> (clojure.repl/dir 'foo)

그 외 다른 방법도 있으나, 위 두가지 방법이 제일 괜찮은거 같다.

이상.

Clojure > How to list the functions of a namespace.

ref

(keys (ns-publics 'foo))

to list Vars exported by the namespace foo; e.g. for clojure.contrib.monads this returns

(defmonad censor m-when-not m+write+m maybe-m maybe-t ...)

(the … stands for quite a lot more).

More generally, there’s a bunch of functions whose names start in ns- which list Vars by namespace, with certain additional criteria attached:

ns-map — the most general function of all, returns a map keyed by symbols (non-namespace-qualified symbols actually), where the value corresponding to each symbol is the Var or class that symbol resolves to in the given namespace.

ns-interns — like ns-map, but includes only the Vars interned in the given namespace (as opposed to Vars from other namespaces which are accessible from the given namespace due to a use or refer call or the implicit referral of Vars from clojure.core.

ns-publics — like ns-interns, but includes only the non-private Vars.

ns-imports — like ns-map, but includes only the entries whose values correspond to Java classes.

There’s also ns-aliases which lists symbols which can be used as shorthand aliases when referring to Vars from other namespaces; e.g. if you call (require ‘[clojure.contrib.math :as math]), ns-aliases will include an entry with the key of math (the symbol), whose value will be the actual namespace clojure.contrib.math. These mapping are not included in the map returned by ns-map.

or you can use as the following.

(dir clojure.string)