-
-
Save mhuebert/419f5d8ab7b99b13dc202addb9b88b50 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(require '[maria.eval :as e] | |
'[goog.object :as gobj] | |
'[clojure.string :as string]) | |
(defn resolve-to-val [sym] | |
(let [{the-name :name} (e/resolve-var sym)] | |
(->> (string/split (str the-name) #"[\./]") | |
(map munge) | |
(to-array) | |
(apply gobj/getValueByKeys js/window)))) | |
(def function-whitelist | |
'[first ffirst second rest last nth take drop vector list hash-map cons conj]) | |
;; XXX gives an error, but works anyway | |
(defn suggest [& args] | |
(let [inputs (butlast args) | |
output (last args)] | |
(->> function-whitelist | |
(map (fn [f] | |
(when (try (= (apply (resolve-to-val f) inputs) output) | |
(catch js/Error e nil)) | |
`(~(name f) ~@inputs )))) | |
(remove nil?)))) | |
(suggest | |
[1 2 3 4] | |
1) | |
;;=> (("first" [1 2 3 4])) | |
(suggest | |
[1 2 3 4] | |
[2 3 4]) | |
;;=> (("rest" [1 2 3 4])) | |
(suggest | |
[1 2 3 4] | |
1 | |
2) | |
;;=> (("nth" [1 2 3 4] 1)) | |
(suggest | |
1 | |
[1 2 3 4] | |
[2 3 4]) | |
;;=> (("drop" 1 [1 2 3 4])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment