Ashwin
Ashwin

Reputation: 13517

Meaning of # in clojure

In clojure you can create anonymous functions using #

eg

#(+ % 1) 

is a function that takes in a parameter and adds 1 to it.

But we also have to use # for regex eg

(clojure.string/split "hi, buddy" #",")

Are these two # related?

Upvotes: 3

Views: 596

Answers (4)

Simon Brooke
Simon Brooke

Reputation: 172

Other Lisps have proper programmable readers, and consequently read macros. Clojure doesn't really have a programmable reader - users cannot easily add new read macros - but the Clojure system does internally use read macros. The # read macro is the dispatch macro, the character following the # being a key into a further read macro table.

So yes, the # does mean something; but it's so deep and geeky that you do not really need to know this.

Upvotes: 0

Alan Thompson
Alan Thompson

Reputation: 29958

The two uses have no (direct) relationship.

In Clojure, when you see the # symbol, it is a giant clue that you are "talking" to the Clojure Reader, not to the Clojure Compiler. See the full docs on the Reader here: https://clojure.org/reference/reader.

The Reader is responsible for converting plain text from a source file into a collection of data structures. For example, comparing Clojure to Java we have

; Clojure          ; Java
"Hello"      =>    new String( "Hello" )

and

[ "Goodbye" "cruel" "world!" ]   ; Clojure vector of 3 strings

; Java ArrayList of 3 strings
var msg = new ArrayList<String>();
msg.add( "Goodbye" );
msg.add( "cruel" );
msg.add( "world!" );

Similarly, there are shortcuts that the Reader recognizes even within Clojure source code (before the compiler converts it to Java bytecode), just to save you some typing. These "Reader Macros" get converted from your "short form" source code into "standard Clojure" even before the Clojure compiler gets started. For example:

@my-atom                       =>   (deref my-atom)    ; not using `#`
#'map                          =>   (var map)
#{ 1 2 3 }                     =>   (hash-set 1 2 3)
#_(launch-missiles 12.3 45.6)  =>   ``                 ; i.e. "nothing"
#(+ 1 %)                       =>   (fn [x] (+ 1 x))

and so on. As the @ or deref operator shows, not all Reader Macros use the # (hash/pound/octothorpe) symbol. Note that, even in the case of a vector literal:

[ "Goodbye" "cruel" "world!" ]

the Reader creates a result as if you had typed:

(vector "Goodbye" "cruel" "world!" )

Upvotes: 5

akond
akond

Reputation: 16035

There are also sets #{}, fully qualified class name constructors #my.klass_or_type_or_record[:a :b :c], instants #inst "yyyy-mm-ddThh:mm:ss.fff+hh:mm" and some others.

They are related in a sence that in these cases # starts a sequence recognisible by clojure reader, which dispatches every such instance to an appropriate reader.There's a guide that expands on this.

I think this convention exists to reduce the number of different syntaxes to just one and thus simplify the reader.

Upvotes: 7

Ivan Grishaev
Ivan Grishaev

Reputation: 1681

Are these two # related?

No, they aren't. The # literal is used in different ways. Some of them you've already mentioned: these are an anonymous function and a regex pattern. Here are some more cases:

  • Prepending an expression with #_ just wipes it from the compiler as it has never been written. For example: #_(/ 0 0) will be ignored on reader level so none of the exception will appear.

  • Tagging primitives to coerce them to complex types, for example #inst "2019-03-09" will produce an instance of java.util.Date class. There are also #uuid and other built-in tags. You may register your own ones.

  • Tagging ordinary maps to coerce them to types maps, e.g. #project.models/User {:name "John" :age 42} will produce a map declared as (defrecord User ...).

Upvotes: 0

Related Questions