2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Clojure tap のメモ

Posted at

Clojure 1.10 で tap が追加されたのでちょっと試してみる。

;; `add-tap`で関数を追加していないので何も起こらない
(tap> "foo")
;; => true

;; println を tap に追加
(add-tap println)
;; => nil

;; 標準出力に "foo" が出力される
(tap> "foo")
;; => true

;; もう一度 println を追加してみる
(add-tap println)
;; => nil

;; 標準出力に "foo" が出力される(2回出力されない)
;; 関数は (atom #{}) への conj で追加されるため、1つに保たれる
(tap> "foo")
;; => true

(defn print-class
  [x]
  (println "class:" (class x)))
;; => #'tap-example.core/print-class

;; 他の関数を追加
(add-tap print-class)
;; => nil

;; 標準出力に以下が出力される
;; java.lang.String
;; foo
(tap> "foo")
;; => true

;; print-class 関数を tap から削除
(remove-tap print-class)
;; => nil

;; 標準出力に "foo" のみが出力される
(tap> "foo")
;; => true

;; 関数は (atom #{}) への disj で削除されるため、エラーは発生しない
(remove-tap print-class)
;; => nil

現状、利用できる関数は tap>, add-tap, remove-tap のみで、登録した関数のリストを取得したり、一括して削除する方法は提供されていない模様。


以上

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?