Demystifying getting started with Clojure / ClojureScript [closed] - clojure

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
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:
Why on earth do I keep reading that you can use React with ClojureScript? React is obviously a JavaScript library.
What is js-interop and why would you use it?
Why are REPLs so important? If you're targeting the JVM or browser, why bother with a REPL?
Do I have to use lein run before a REPL will run with updated code?
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.

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.
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.
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".
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.
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.
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.

Related

Edit, compile, and run Clojure code in a browser?

I would like to know if a butt IDE like Koding or Butt9 supports actually executing Clojure code in the browser. All I could find was this which didn't even work, returning an error in the Koding terminal when running the java command. I'd be willing to host my butt IDE myself if necessary, I have some extra hardware I could throw together into a server. The reason I would like this is I just got a Chromebook and will be taking a programming course featuring Clojure at school. I couldn't get Linux running in Crouton very well so I decided to look into butt IDEs. Anyone have suggestions?
edit: seems like Codenvy could be what I'm looking for. Any other suggestions though?
Clojurescript can run a REPL in a browser.
Examples: http://www.tryclj.com, http://clojurescript.net
Some documentation (scroll down): The-REPL-and-Evaluation-Environments
Also see the links under "REPL" at the Clojurescript Wiki
That's not an IDE, obviously, but yes, you can run Clojure code in a Browser. Maybe you can integrate it into your workflow with an IDE.

Can I create a program in Clojure able to edit it's own code?

I am thinking of creating an educational program in Clojure that would show a console where the user could edit code in order to change a running code in another part of the screen (like a game, for instance). Would that be possible? If so, what should I study in order to be able to do this?
It is possible. See this Interactive programming Flappy Bird in ClojureScript . It uses clojurescript and figwheel as its development environment.
The awesome thing about figwheel is that you can change the flappybird running code in the browser by simply evaluating new code in the repl and then figwheel push the new code to the browser. The changes happened in real time.
You can start studying clojure and clojurescript.
Hm, changing code during runtime is one of the basic concepts behind clojure.
Watch this for more on this topic: https://www.youtube.com/watch?v=P76Vbsk_3J0
Otherwise every repl or form of a repl can do that.
To get started have a look at Lighttable: http://lighttable.com/ It has inline evaluation in the editor.

Hello World - Clojurescript

Beginner here.
Can I compile an existing Clojure script to run it on the web using ClojureScript?
Let's say that I have a script that prints Hello world in my terminal, and I want to print that text on the browser. Should I rewrite a whole script with ClojureScript syntax, or should I just compile it using another compiler?
(ns clojure-hello-world.core
(:gen-class))
(defn -main [& args]
(println "Hello World"))
(Long answer :P)
Even though Clojure and ClojureScript share a good amount of features, there are some that are specific to one or the other. For example there are no real classes in JavaScript so the :gen-class specification in the ns form doesn't make much sense.
One important fact is that the syntax of both Clojure and ClojureScript is exactly the same, differences have to do mostly with the host VM in which they run (Java VM in the case of Clojure and JavaScript VM in the case of ClojureScript).
There is a list of the differences between the two Clojure implementations here.
There's also a tool called cljx to "write a portable codebase targeting Clojure/ClojureScript". Recently there has been some discussion on the Clojure Dev group around finally implementing feature expressions which would on one hand deprecate the use of cljx but on the other complicate the work that tools have to do to extract information from Clojure source files.
I would start with lein-cljsbuild to get started. This will get you going with a nice edit eval and look at browser loop. It's well worth getting this setup first because it makes learning ClojureScript much more fun. I promise it's worth the hassle. If you need more interactive support the folks in #clojure on freenode are very kind and helpful.
Basically, the Browser executes JavaScript. You compile your ClojureScript code to JavaScript. The Browser loads your JavaScript via an HTML page. So, you have to create an HTML Page and point your Browser at it.
The simplest way I got started was to use Luminous (http://www.luminusweb.net/docs/clojurescript.md).
However, Chestnut (https://github.com/plexus/chestnut) looks promising.

Clojure Worksheets

I have been learning Clojure a bit recently. Is there such a thing in Clojure world as Scala-like worksheets, into which I can put any code and get it evaluated as soon as I save it? Or maybe there's a similar solution for Clojure?
I am now working with lein repl and, sadly, can't say it's the most usable tool ever.
In Lisp development in general (and Clojure in particular) the preferred programming style is what's usually dubbed interactive programming - the developer keeps an image of the app loaded at all times and interacts with it via a REPL. You can easily modify the loaded code on the fly and test changes immediately in the REPL (that's not easy at all with Scala - one has to resort to something like JRebel to do it). I find the Scala worksheets a pretty primitive solution in comparison...
The workflow that I follow in Clojure is:
I open nREPL.el in Emacs - this loads my lein2 project with all of its dependencies and gives me a REPL which I can use the try out stuff
I write some code in source code and load the changed functions (maybe by evaluating a top level form with C-M-x
Afterwards I'd press C-x C-z to jump back to the REPL and I try out the new code in it
I go back to step 2
Basically the Clojure REPL is much more powerful than the Scala REPL and I personally consider it hugely superior to the Scala IDE worksheets. Once you get used to the interactive incremental style of programming Lisp offers everything else starts to look strangely complex by comparison. I'm not familiar with Eclipse's CounterClockWise Clojure plugin, but I'm pretty sure it offers similar functionality to Emacs's nREPL.el.
You might want to take a look at the autoexpect plugin for Leiningen. Every time you save any file in the working directory, the plugin compiles and runs your code; as a bonus, it will evaluate any "expect" function calls which can serve as tests. This is very helpful for test driven development and is a nice compliment to working with the REPL as described in the other answer (I often use one or the other or both together depending on how many test cases I have in place).
I should note that running autoexpect is far faster than running "lein test" or "lein run" repeatedly, due to the startup cost of the JVM and Leiningen.
It sounds like what you are looking for is the Clojure Koan plugin. This a worksheet-style problem-solving exercise tool that watches your edits and provides instant feedback on the correctness of your work.
For actual development workflow I second the advice others here have provided on tooling and interactive environment setup, but since you specifically said you are learning Clojure, this can be a fun approach. There is also a web application called 4Clojure that you might have fun playing with.
However you will eventually (or right away) want to get a smooth and convenient development environment set up, and I haven't seen any mention so far of a few important tools. One person mentioned Nrepl. If you like Emacs, there's a slime/swank-like interaction mode that jacks into nrepl called nrepl.el that provides very nice integration between editing files and messing around in the repl. Similarly there is VimClojure, and you can find plugins for IntelliJ (LaClojure) and Eclipse (Counterclockwise) that are also popular and well-maintained.
Someone mentioned autoexpect. If you are looking for a good TDD setup, I would recommend midje. If you are using a 2.0 preview release of leiningen there are a few issues with the lazytest integration being in flux, and lazytest itself is or should be deprecated. But I prefer midje over expectations, personally, and these problems will surely be worked out in the 2.0 release of lein-midje. The stable version of lein-midje that works with the non-preview (1.x) leiningen has autotest-like functionality using lazytest.
As nobody has mentioned it, cursive is really nice these days, although I was sad to move back to a full blown IDE. You can easily do the same thing as Emacs with the built in repl, just move into the namespace that you are working with and synchronise every time you make changes. It's a really nice way of working.

Clojure REPL philosophy and utility applications

Sorry for the long post, but this forum always asks for use cases :-).
I am frequently called upon to write utility applications (GUI and command line) for my organization. I most commonly write these in Java and more recently Scala.
The "philosophy" in Clojure (and other Lisps) seems to center around the REPL, and I have to agree that it makes a nice development environment, but I obviously cannot produce utilities that require that the users "Install Clojure and Clojure-contrib, then unzip the utility into a directory on your hard drive, start Clojure from the command line using the following classpath, ...". The users do not care that the utility is written in Clojure. They just want to point and click or, at most, type "java -jar Utility.jar -?" on the command line.
My question centers around compilation and separating the application into many namespaces/files.
I know that the main method must be in a Clojure file that includes the (gen-class...) command (or the :gen-class clause on the ns command). Do I do the same for the additional Clojure files, or should they be left as a source code that is packed in the JAR and loaded by the main file? How about testing the additional files from the REPL during development?
I have been using leiningen, cake, and maven to build self-contained JARs (containing unpacked clojure.jar, clojure-contrib.jar, and commons-cli.jar files), but so far I have been writing my code in the same file as the main method.
It's up to you whether you AOT-compile your .clj into .class or let Clojure do it dynamically at runtime. In general, I find it's easier during development to avoid AOT and just use AOT as a performance booster for cases where the cost of compiling your .clj on the fly doesn't make sense (limited environments like Google App Engine or utilities where startup time is critical). For server processes that will run for a long time, there is not much advantage in AOT-compiling.
There are a number of complexities around using AOT classes at dev times that are specific to different dev environments.
One down-side of AOT is that your compiled classes might be incompatible with a future version of Clojure and that is more likely than your clj files being incompatible. That may become more important over time.
When it comes to the separation, I'd keep them in separate namespaces/files:
One for the :gen-class namespace, containing -main and all the other Java-like things.
The other namespace for all the functions.
Ideally, your main should contain just the call to a function from the other namespace, or perhaps some logics for evaluating or repacking of the args.
Your question seems quite similar to the separation between logics code and ui code. You could see the namespace with :gen-class as just an interface provided by your program to Java code, nothing more.
When it comes to the utilities (like command-line, or Swing apps, etc.), there's a trouble with Java in general because of the JVM start time.
Now, you could solve this by making a server app run the REPL in the background all the time, and say, somehow receive an s-expr to be evaluated, and return the result. This could be done as a simple web application which receives an s-expr as URL parameter, and returns the result. Now, with this you could make all utilities in plain old Java, or even bash using wget, since all you'd have to do is access an URL (assuming that server with repl is running in background).
There's a good chance that something like this already exists, so if anyone knows - comments are more than welcome.
Oh, and another thing, the port where repl webapp is exposed would probably have to be closed to the outside world, to prevent Clojure-injections :D