Basic functionality of cljc files - clojure

Normally, Clojure source files are named (for example) foo.clj, and Clojurescript source files are named foo.cljs. My impression is that in Clojure versions >= 1.7, I can name a file foo.cljc if I want it to be available for loading with require or use both from Clojure and Clojurescript.
Is this correct? It seems to be implicit in the primary documentation pages on Using cljc
and reader conditions, but as far as I can see it's never explicitly stated.
This is not a question about using reader conditionals to specify alternate code for running in Clojure and Clojurescript; it's more fundamental. For example, I have a source file that contains code that's completely generic: It will run in both Clojure and Clojurescript unchanged. Can I assume that by naming it with ".cljc", require will always find it from inside both Clojure and Clojurescript (assuming it's named correctly, is in the correct location, etc.)?
[I'm pretty sure that I'm right, but I'm not certain, and I thought it would be worth having the answer documented here if I'm correct.]

That's correct. When Clojure or ClojureScript needs to load a namespace, they first look for the platform-specific file (.class then .clj on Clojure and .cljs on ClojureScript) and if not found, then they look for the .cljc file.
You do still need to make sure it is available to the language on the classpath, as with other source files.

Yes.
The nuances is the precedence of loading, which is explained here:
https://github.com/clojure/clojure-site/commit/010134bcc2e8af899ba2b1e5816b0fcf7e007ba4
In short, cljc files are loaded last, and conditionals are only valid in cljc.

Related

Why are there clj and cljs folders in my lein re-frame template?

Why are there clj and cljs folders in my lein re-frame template as below? And why do they both include files called .core that appear to use the same namespaces? I've been told this is the place to start when learning re-frame, but I cannot find any explanations of why the templates are setup the way they are or created including the content they include.
There is no explanation for any of the boilerplate or code that comes with any lein template which make them very hard to use for beginners.
Thanks in advance.
This setup is used to separate Clojure Backend code from the ClojureScript frontend. It isn't actually necessary and I don't particularly recommend it but I can explain its history and why you'd want to do it.
For the ClojureScript side it really doesn't matter at all.
When builing a Clojure Backend you will often deploy in some "uberjar" or "uberwar" setup. This means that all source files and dependencies are packed into one single .jar file (basically just a zip file). This is typically done by including all files from a specified set of directories, so it would include src/clj but not src/cljs. If everything is in one directory it would add the .cljs files as well although they are never used by the Clojure backend. So in essence it just makes your "uberjar" bigger. It is not an important optimizations but some people prefer to keep things lean and clean.
In addition some developers just prefer to separate the code this way. In this case the template authors did.
One answer:
As the comments point out, some projects develop the backend code (Clojure) and the frontend code (ClojureScript) in the same project repo. I think this is a mistake as it can easily lead to confusion and entanglement (esp. if using lein to start both projects simultaneously). IMHO it is better to keep both front- and back-end parts in separate repositories. I would also strongly recommend using figwheel-main and the Clojure deps build tool for the CLJS code.
Another answer:
For CLJS code, any macros have to be defined in an "earlier" compilation stage. Thus, for namespaces defining macros, you often see files like either util.clj or util.cljc to define the macro, and then a file like util.cljs where the macro is used.
You can find more information below, but it is subtle & confusing:
https://clojurescript.org/guides/ns-forms
https://clojurescript.org/about/differences
https://blog.fikesfarm.com/posts/2018-08-12-two-file-clojurescript-namespace-pattern.html

What is the purpose of core.clj file?

I am just started on clojure and have a very basic question about core.clj.
What is a convention about it? What code should go there? Public API?
It is generated by leiningen when creating a project.
I have looked into source code of some libraries and this file is also present there (per package?).
Thanks
There's no defined meaning for it, but in an application it's often where you'll find the entry point, the main- function. For libraries, foo.core is often the namespace users will import to get your main functionality.
You don't have to do it that way, but it's a semi-predictable place to have the 'central bit' of whatever it is you're writing - even if your actual logic and algorithmic code is somewhere else.
Leiningen generates foo.core because it needs to pick a name and core is generic enough that it probably wont' be wrong. It's a style decision, but I typically opt to rename core.clj to a name that is actually meaningful for my project.
It's just a template file emitted by Leiningen. If I run lein new foo there is no specific or standard meaning to the namespace foo.core. It is entirely legal for the namespace foo to be the main API container for a project. It's just a free file for you to start working in.
That said, if you have a project foo, it is expected although not enforced that all your code for that one project exist in the foo.* namespace. Leiningen will allow you to build a project with the files src/foo.clj and src/bar.clj and it is entirely reasonable for foo to require bar, however when packaging your code for distribution this is probably a bad idea.

Beginning Clojure without Java experience - how to best organise and run projects?

Apologies in advance for the somewhat discursive nature of this clump of related questions; I hope the answers will be a useful resource for newcomers to Clojure.
I have just begun to learn Clojure, motivated in part by this essay. I'm not a professional developer but I have several decades of programming experience (ARexx, VB/VBScript/VBA, then Perl and daily use of R starting in 2011). My platform is Windows 7 64-bit. I'm using Emacs 24.3, cider 20131221 and Leiningen 2.3.3 on Java 1.7.0_45 Java Hotspot 64-bit server. I have bought Clojure Programming and the Clojure Data Analysis Cookbook and dipped into both. I have found them promising but I am getting lost in the detail.
Obviously the thing to do is to get stuck in and experiment with code exercises and small tasks, but the immediate problem for me has been the complexity of structuring, organising and even just plain running projects in Clojure. With R I can get away with a file of plain text containing the bulk of the code, perhaps with one or two others containing common functions for larger projects.
Clojure is very different and with no experience in Java I am struggling to put the pieces together. Clojure Programming has a whole chapter on organising and building projects, but it is so comprehensive that conversely I'm finding it difficult to tease out the information relevant to me now. I guess I'm looking for something like this answer on Swank, but the tools seem to have moved on since then. So here goes.
Leiningen produces amongst other things a project.clj file that contains the project definition and dependencies. I think I get this. Can I use this file for code not related to the definition, below the defproject, or is it best to leave this untouched and have the code itself in different clj file(s)?
If the answer is to leave the project.clj file alone, how is the relationship between that and other files established? Is it simply that all the clj files in the project folder are counted part of the project?
How do I define the main code file, the 'entry point' of the project? Let's say I have project.clj and main.clj with some helper functions in common.clj - how are the relations between these three files defined? I can call functions from main.clj but how does the project know that main is the core of the project if/when I package the project into an uberjar?
If I have a number of clj files, what is the best way to import functions? I have read about require and use (and import and refer and...) but I don't fully understand the difference and those two keywords are difficult to search for. The examples for REPL in the Clojure Data Analysis Cookbook most often opt for use. I found a similar question but it was a little over my head.
This is more tool-specific, but as Emacs seems to be widely used it seems fair to ask: what's a good workflow to run small bits of code given (say) the main.clj example given above? Currently I just open the main.clj file in Emacs, do an M-x cider-jack-in to establish the REPL, experiment in the REPL, then when I want to try something I select the whole buffer and select Eval region from the CIDER menu (C-c C-R). Is this standard operating procedure or utterly misguided?
Is there a convention for defining namespaces? I think I understand that namespaces can cover multiple clj files and that ns is used to define the namespace. Should I explicitly define the namespace (at the beginning of) every file of code? Clojure Programming has some recommendations but I'm interested in input from other users.
Clojure programming says to "Use underscores in filenames when namespaces contain dashes. Very simply, if your namespace is to be com.my-project.foo, the source code for that namespace should be in a file located at com/my_project/foo.clj". (EDIT as explained in this useful answer and also this one). This restriction would never have occured to me. Are there any other gotchas with regard to naming namespaces and variables? R frequently uses dots in variable names but I guess given the Java connection that dots should generally be avoided?
No, don't put actual code in there unless you know what you are doing (e. g. generate the version number for defproject from the local git repository like in the repositories of juxt)
The project.clj is simply one big parameter to Clojures build tool leiningen. See an example here https://github.com/technomancy/leiningen/blob/master/sample.project.clj. For example, you could specifiy a different source directory than src in the :source-path.
Default is the -main function in project.core, but you can specify various different configurations in the project.clj.
require is preferred. :use imports all publics of a namespace unless you use it in conjunction with :only. Require let's you use an alias for an entire namespace with :as, but you can have the same effect from use with :only using :refer. Notice that in ClojureScript :use without :only is not even allowed.
This is normal. There are other combos like e.g. C-c C-k to reload the entire file of the buffer. If you find yourself entering too many forms into a REPL and would rather edit them in a separate buffer https://www.refheap.com/22235.
I like to experiment trying to name namespaces in in verbs rather than nouns, e. g. I prefer myproject.parse, myproject.interpret, over myproject.parser, myproject.interpreter etc. But that's a question of personal style. EDIT: Yes, explicitly define the naming of the namespace by its filename and the ns form at the beginning of the source file. It is unusual to have multiple source-file defining one namespace.
Afaic this is the only caveat regarding naming of namespace. You can hardly know it in advance.
I like your "worried" approach. You will (hopefully) find out that Clojure and especially Leiningen are almost nonsense-free in terms of these questions.
Regarding REPL use: I saw your comment under #Mars answer that you want to use a REPL in a fashion that you can re-use what you are entering. Two things:
Dynamic development is awesome, allowing you to test small components or functions interactively without the need to run an entire program written for that purpose.
If you find your self entering huge forms at the REPL that you intend to de-/recompose into functions or tests later, I recommend editing them in a seperate clj file that is not part of the project source (i. e. not in a namespace). You can then use this Emacs hack to eval forms from a Clojure buffer in the REPL. Ideally split your Emacs in two windows (C-x 3) with the nrepl buffer on one side and your .clj on the other side. Then use C-x C-. from within the clj file to have the form at point pasted into the nrepl and be evaluated. Installation instructions are at the link (and your .emacs file usually resides in the home directory).
#Igrapenthin's answers are great. Here are a few other thoughts.
On namespaces, this tutorial is great.
Just to clarify re #2: No, don't just put the .clj files anywhere under the project. They have to be under src/, or in whatever directories are listed (as strings) in the vector after :source-paths in project.clj, if that entry exists. Then strip off that initial path when you're making your namespace names. This drove me crazy until I figured it out. (People who know better, please correct me if something here isn't right.)
One #3, you need Igraphenthin's answer, but why not just start by evaluating expressions in the REPL? I've been working on a project on and off for weeks, and it does a lot, but my -main function still doesn't do anything. I just run whatever parts I'm working on. Well, you're used to languages with fully operational prompts--you decide.
EDIT: Whether or not you define the -main function to do anything, you can also put :use or :require keywords in the ns statement that defines the namespace for that same file. These will automatically get invoked when you start the REPL with lein repl, and so whatever you have made available through the ns keywords will be available at the REPL. That way, you have your previous work available, but you can play around with it in different ways in the REPL. (Also, if you don't like the default name for the file that's automatically loaded, you can redefine it in project.clj with :main. Igraphenthin alluded to that.)

How can I convert clojure.contrib.monads to be used with clojurescript?

I would like to use the Clojure Monad library clojure.contrib.monads in ClojureScript. Is there an implementation that has been tested as known to work with ClojureScript as it does not appear in the standard ClojureScript distribution?
(I am assumming I may have to just copy and paste the code from a clj into a cljs. Although what about the macros, as these need to be in a clj file)
Update: (am I right in thinking that these are the two files I will need?)
Files
monads.clj
monadic_io_streams.clj
Ok, it looks like this doesn't exist. I'm going to have to go through the implementation in Clojure first to make sure that eval and similar stuff is not used, as Clojurescript does not support all the features of Clojure.
You may want to take a look at Jim Duey's protocol-monads, for which cljs support has been recently added - see this commit for details.

How can I use one file from my project in another file?

Simple question. I'm new to Clojure.
How can I use one file from my project in another file? Basically how can I include, import, or require another file? Not from libraries but fro my own code.
Thanks,
Alex
Normally you'll want to use the same method that you use with library code, which is to use / require your namespaces (through an ns form at the top of the file and sometimes the use / require functions at the REPL). For this to work, you have to make sure they are on the classpath. A short guide to that:
Follow the usual Clojure project structure: a src/ directory containing all your source files, where file src/foo/bar/baz.clj defines a namespace called foo.bar.baz. Note that you must maintain the directory structure / namespace name structure correspondence; things won't work otherwise. Also note that you must not use the _ character in namespace names or the - character (the hyphen) in filenames and whenever you use _ in filenames you must use a - in namespace names (and the other way around.) Finally, the directory hierarchy will be slightly more complicated with Maven projects, but don't worry about this for now (unless you're already a proficient user of Maven, in which case this won't be a problem for you).
Also see this answer of mine to an earlier SO question about Java classpath handling with Clojure for a more detailed step-by-step explanation of the filesystem hierarchy / classpath hierarchy correspondence.
If your code from the foo.bar namespace needs to use code from the foo.quux.baz namespace, do something like (ns foo.bar (:require [foo.quux.baz :as baz])) in foo/bar.clj and call functions from baz as baz/some-function. Or you can put (:use foo.quux.baz) in the ns form instead to call them directly (without the namespace qualifier, e.g. some-function). That's exactly the same thing as what you'd do for library code.
When working with your project's code from the REPL, make sure you include the src/ directory (the directory itself, not any of the files therein) on the classpath. You should probably consider using some tool to automate the REPL setup (including classpath management) for you; Leiningen is very popular with Clojurians and there are plugins for using Maven with Clojure too.
Warning: Your JVM-launching command might (in fact, probably will) recognise an environment variable called $CLASSPATH. As for its relationship to your Clojure projects, well, basically there should be none. More likely than not, your projects will require a different classpath each, with some possibly using versions of certain jars incompatible with those required by others (notably if you're using Clojure 1.1 -- latest stable release -- for some projects, while experimenting with 1.2 snapshots with others). Thus the correct way of managing the classpath is to prepare a minimal version for each project and pass that to the JVM launching command. As mentioned previously, you should invest some time in learning to use a good tool (like the above mentioned Leiningen) to set up the classpath for you as soon as possible so you don't need to care about this yourself.
(As a side note, you might have to add more than just the src/ directory and your jars to the classpath in some scenarios, e.g. if you plan on calling compile to produce .class files, you'll have to put the target directory on the classpath too. That's beyond the scope of this question, though.)
BTW, I've started this answer with the word "normally", because you could also use things like load & in-ns to split a single namespace into multiple files. Most of the time this won't be what you really want to do, though; just use a well thought out namespace layout instead.