Skip to content

Instantly share code, notes, and snippets.

@jackrusher
Last active July 23, 2017 12:24
Show Gist options
  • Save jackrusher/9fc116c156f86dcf855c03ba9f16d404 to your computer and use it in GitHub Desktop.
Save jackrusher/9fc116c156f86dcf855c03ba9f16d404 to your computer and use it in GitHub Desktop.
(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