Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

R functions arglist #22

Open
genmeblog opened this issue Feb 6, 2020 · 6 comments
Open

R functions arglist #22

genmeblog opened this issue Feb 6, 2020 · 6 comments
Labels
enhancement New feature or request

Comments

@genmeblog
Copy link
Member

genmeblog commented Feb 6, 2020

I prepared POC for adding arglist for RObjects acting as function using formals. It can be helpful for REPL driven development. Arglists conform calling way in clojuress (associative destructuring on variadic position)

Examples:

(formals base/mean)
;; => ([x & {:keys [...]}])
(formals stats/arima0) 
;; => ([x & {:keys [order seasonal xreg include.mean delta transform.pars fixed init method n.cond optim.control]}])
(formals dev/dev-off)
;; => ([& {:keys [which]}])
(formals base/Sys-info)
;; => ([])

formals can be used to create :arglists meta tag.

(I checked and it works on CIDER)

@daslu
Copy link
Member

daslu commented Feb 6, 2020

Nice.

This is related to #8 .

@daslu daslu added the enhancement New feature or request label Feb 7, 2020
@genmeblog
Copy link
Member Author

Just to leave it before pull request. Formals is defined as below:

(def ^:private empty-symbol (symbol ""))

(defn formals
  [{:keys [code class]}]
  (when (= class ["function"])
    (let [args (-> (format "formals(%s)" code)
                   r
                   r->clj)
          {:keys [obl opt]} (reduce (fn [m [k v]]
                                      (let [selector (if (and (= empty-symbol v)
                                                              (not (seq (:obl m)))) :obl :opt)]
                                        (update m selector conj (symbol k))))
                                    {:obl [] :opt []}
                                    args)]
      (cond
        (and (seq obl)
             (seq opt)) (list (conj obl '& {:keys opt}))
        (seq obl) (list obl)
        (seq opt) (list ['& {:keys opt}])
        :else '([])))))

(alter-meta! #'stats/arima0 assoc :arglists (formals stats/arima0))

(meta #'stats/arima0)
;; => {:asdf 12, :name arima0, :ns #namespace[stats], :arglists ([x & {:keys [order seasonal xreg include.mean delta transform.pars fixed init method n.cond optim.control]}])}

Adding metadata can be done during intern in require-r.

@genmeblog
Copy link
Member Author

Sorry for duplicate.

@daslu
Copy link
Member

daslu commented Feb 13, 2020

Hi @genmeblog .

  1. This is great!

  2. Added some regression tests for the functions you mentioned:
    aecefd3

  3. Added support for primitive functions: ef312b7

  4. Thinking about it, maybe the handling of arglists could be done not only when doing require-r, but rather, every time we create an RObject. This way, when we create an R function with (r 'sum) or (r '[function [x y] (+ x y)]), we get the arglists too. What do you think?
    If this sounds good, I can do it tomorrow.

(edit: typo)

@genmeblog
Copy link
Member Author

Great! Regarding 4: this would be the best solution. But remember that meta are attached to symbol not to object itself.

daslu added a commit that referenced this issue Feb 15, 2020
- extended and renewed datavis API, function arglists, bugfix with deps loading
- see changelog
- related issues: #22, #16, #8
@genmeblog
Copy link
Member Author

should we close this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants