関数へのオプションのフラグとしてキーワードを実装する最良の方法は何ですか? 次のような関数呼び出しを行いたい:
(myfunction 5)
(myfunction 6 :do-this)
(myfunction 3 :go-here)
(myfunction 2 :do-this :do-that)
defn を使用すると、次のような関数を定義できます。
(defn myfunction [value & flags] ... )
しかし、flags
リストになります。リストを検索する独自の関数を作成できますが、そのような関数はコア ライブラリに含まれていないため、慣用的ではないと思います。
私が今使っているもの:
(defn flag-set? [list flag] (not (empty? (filter #(= flag %) list))))
(defn flag-add [list flag] (cons flag list))
(defn flag-remove [list flag] (filter #(not= flag %) list))