Clojure function argument syntax

I am trying to understand the syntax of a call to load a library:

(use 'clojure.contrib.duck-streams) 

It makes sense to me because it applies the quotation mark macro to clojure.contrib.duck streams so that the reader does not try to evaluate this line.

Now, if I want to use the: only tag to load only the reader, why is this correct:

 (use '[clojure.contrib.duck-streams :only (reader)]) 

instead:

 (use '[clojure.contrib.duck-streams :only reader]) 

I read this as meaning the omission of arguments in this argument, but REPL complains about Don't know how to create ISeq from Symbol. Why are parsers around the reader?

This is also equivalent to the first line and is valid:

 (use '[clojure.contrib.duck-streams]) 

So it seems that "string are" [string] are equivalent arguments, which I don't understand either.

+6
clojure
source share
2 answers

:only requires a list of characters. This is how the function is written. Note the docstring for refer , which uses use .

  refers to all public vars of ns, subject to filters. filters can include at most one each of: :exclude list-of-symbols :only list-of-symbols :rename map-of-fromsymbol-tosymbol 

It is written so that you can specify more than one character if you want.

 (use '[clojure.contrib.duck-streams :only (reader writer)]) 

As discussed in this recent post , if you write a function that accepts or returns a variable number of arguments, it is recommended that you always have it / return a list or a vector or set, even if it accepts / returns a single element. Because:

  • nil , often used to represent "null elements", possibly seq . Empty collections are also available seq .
  • Two or more list items are available seq .
  • It also makes sense to be able to use a single seq element, putting it in a list.

It would be inconvenient to have a case with one subject - a special case. It is more consistent and easier to program when you consider one subject as a kind of degenerate case and drop it into the list.

Note that all use cares whether the :only argument :only sequential character set. This means that all lists, vectors, and sets work.

 (use '[clojure.contrib.duck-streams :only [reader writer]]) (use '[clojure.contrib.duck-streams :only #{reader writer}]) 

The only Symbol , however, is not capable of seq , so you get the exception you make.

Take a look at core.clj if you want to see how all this is implemented.

+14
source share

This is not just β€œparens” - partners are the syntax for creating a list. It will appear in the element after: only the tag is expected to be a list of things to which it relates.

+1
source share

All Articles