Writing a “cheating” Quine in Clojurescript

China☆狼群 提交于 2019-12-25 07:19:11

问题


Suppose we wanted to write a cheating quine in clojure, we could do:

(ns cheating-quine)
... stuff here doesn't really matter ...
(println (slurp *file*))

Now, this does not work in Lein Figwheel because the value of file ends up being something like /tmp/form-init########.clj, and contains bootstrapping code of some sort.

Question: how can we get this "cheating" quine to work in clojurescript?

Note: the goal is NOT to write a quine. The goal is to write a cljs program which has access to the file it's defined in. This whole "cheating quine" thing is trying to explain the problem better then my previous failed attempt of

Clojure + Clojurescript: Macro to read code of current file


回答1:


A cheating solution is to distribute the source code (e.g. put it in resources and serve it as a file) and compile a bootstrap to load and execute code, and get it to pull down your file, and in the file load the same file... that way it can do a HTTP request for the code that is currently running, and execute it. This is essentially how ClojureScript browser REPLs such as http://clojurescript.io/ work... They compile and execute incoming code on the fly. clojuresript.io is a good working example of ClojureScript bootstrapping if you are interested in it, but a word of caution, there is quite a lot going on so expect to invest quite some time :)



来源:https://stackoverflow.com/questions/40230525/writing-a-cheating-quine-in-clojurescript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!