Skip to content

Commit

Permalink
Disassociate fn arity choice from order of defined arities inline wit…
Browse files Browse the repository at this point in the history
…h clojure.
  • Loading branch information
GreshamDanielStephens committed Feb 16, 2021
1 parent 7425611 commit 632502d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/sci/impl/fns.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -222,12 +222,13 @@
f)))

(defn lookup-by-arity [arities arity]
(some (fn [f]
(let [{:sci.impl/keys [fixed-arity min-var-args-arity]} (meta f)]
(when (or (= arity fixed-arity )
(and min-var-args-arity
(>= arity min-var-args-arity)))
f))) arities))
(let [arities-with-variadic-last (sort-by (comp :sci.impl/min-var-args-arity meta) arities)]
(some (fn [f]
(let [{:sci.impl/keys [fixed-arity min-var-args-arity]} (meta f)]
(when (or (= arity fixed-arity)
(and min-var-args-arity
(>= arity min-var-args-arity)))
f))) arities-with-variadic-last)))

(defn eval-fn [ctx interpret {:sci.impl/keys [fn-bodies fn-name
var] :as f}]
Expand Down
2 changes: 2 additions & 0 deletions test/sci/core_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,8 @@
(is (= 2 (eval* '((fn foo [x & [y]] y) 1 2 3))))
(is (= 1 (eval* '((fn ([x] x) ([x y] y)) 1))))
(is (= 2 (eval* '((fn ([x] x) ([x y] y)) 1 2))))
(is (= "otherwise" (eval* '((fn ([x & xs] "variadic") ([x] "otherwise")) 1))))
(is (= "otherwise" (eval* '((fn ([x] "otherwise") ([x & xs] "variadic")) 1))))
(is (= '(2 3 4) (eval* '(apply (fn [x & xs] xs) 1 2 [3 4]))))
(is (thrown-with-msg? #?(:clj Exception :cljs js/Error)
#"Can't have fixed arity function with more params than variadic function"
Expand Down

0 comments on commit 632502d

Please sign in to comment.