Search code examples
clojureclojurescript

Demystifying getting started with Clojure / ClojureScript


I am just getting started with learning ClojureScript and am baffled by a number of things, mainly to do with the dev environment. I'd really like to get cracking building a web page but am not very close a few days in.

I am reading some books, I have got Clojure and Leiningen installed with a re-frame Chestnut template, the Clojure plugin for VS Code, IntelliJ and Cursive up and running.

So I have a few mini questions:

  1. Why on earth do I keep reading that you can use React with ClojureScript? React is obviously a JavaScript library.

  2. What is js-interop and why would you use it?

  3. Why are REPLs so important? If you're targeting the JVM or browser, why bother with a REPL?

  4. Do I have to use lein run before a REPL will run with updated code?

  5. I get this error more than I get code to do anything:

user=> (run)
Syntax error compiling at (REPL:1:1).
Unable to resolve symbol: run in this context

What does it mean? This is what's stopping me from running Figwheel.


Solution

  • Just learning Clojure now with a target of trying ClojureScript later, possibly with React. I have been at this for a few weeks now working through "The Joy of Clojure" (the book) and assorted websites and YouTube presentations and am still not ready to fully know what I am doing (though I have understood lazy evaluation and concurrency support. Yeah!). And I used to do a bit of Scheme ... a long time ago. Actually I have to diversify into just trying some development now because I'm getting downcast; there is a lot to digest.

    1. You can use React with ClojureScript because there are "bindings" for React, allowing React elements to call ClojureScript code. Search for "ClojureScript and React" on Google. But you need to get comfortable with ClojureScript first, then get acquainted with React, possibly with JavaScript basics, then try your hand at this unconventional approach.
    2. You use "javascript-interop" if you want to call JavaScript code and use JavaScript data structures from your ClojureScript code. The two sides of this "interop" frontier have somewhat different approaches and philosophy. (A big indication of that is that "immutable.js" is even a thing). The same for "java-interop".
    3. REPLs are important because they allow you enter and run your code immediately. Excellent for testing and experimentation. They are also give an interface to the actually running system, somewhat handing you the facilities of a debugger combined with the possibility to change your code, manually replacing functions, as it runs. This is a "live" approach to development coming from the Smalltalk world, as opposed to the "dead" approach, where cycle between editing, compile everything, running, checking the results, stopping, editing etc. etc. Note that the JVM has its own REPL nowadays. Note that when building the browser application in React, you will immediately be told to perform live debugging from your code editor by connecting it to the Chrome browser. Same goal. Read this: The REPL and main entry points. Read this: Progamming at the REPL. Check out some YouTube videos, maybe this. Experiment by starting the REPL and typing code in directly.
    4. You can run the REPL directly provided by Clojure (start clj) or the REPL provided by Leiningen (start lein repl), which is the REPL of Clojure with extras. There is probably a REPL for ClojureScript on Node.js, too. You can enter code directly or load it from a file using (load-file "file") for example.
    5. It means there is no function run to run. You have to write it first. lein run executes the application inside a "project" directory layout, and will look for function -main by default. Read up on Leiningen: Leiningen Tutorial.

    Be patient, this path is arduous and demands persistence.