Character Symbols in Clojure

Further references: https://clojure.org/guides/weird_characters

See WIP List of symbols glossary at the bottom of this notebook.

{:deps {org.clojure/clojure {:mvn/version "1.10.3"}
        ;; complient is used for autocompletion
        ;; add your libs here (and restart the runtime to pick up changes)
        compliment/compliment {:mvn/version "0.3.9"}}}
Extensible Data Notation
{:hello (clojure-version)}
0.1s

# The octothorpe, pound sign, hashtag, number sign

The # symbol has a lot of different meanings in different contexts, such as "number" (in particular in Northern America), "heading" in Markdown, hashtag in Twitter, "pound key" on phone keypads, etc..

In Clojure code, the # symbol typically refers to sets. It is also used to denote the shorthand syntax for anonymous functions.

Glossary of Symbols and Their Meaning in Clojure

  • Octothorpe #

    • Regular expressions (TODO: add examples)

    • Anonymous function shorthand syntax: An anonymous function is a function without a name, sometimes called a "lambda function" in other languages. Examples: #(+ 5 %) is the shorthand equivalent to (fn [x] (+ 5 x))

    • Sets: A collection of items where order is not preserved, and duplicates are allowed. Examples: #{:a :c :b} is a set which contains 3 keywords. Another valid set is #{4 22 0 17 9}

    • "Dispatch character" (TODO: add examples)

    • Var quotes: #' (TODO: add examples)

    • Symbolic values ## (TODO: add examples)

    • Tagged literals (TODO: add examples)

  • Underscore _

    • (It is convention to use `_` in place of named arguments where the results are not meant to be stored or reused, ie. side effects such as a loggers, or for complying with APIs or function signature contracts) (TODO: add examples)

      • Question: Is there a widely accepted name/label for this usage, such as "placeholder" or "ignore this"?

  • At Symbol @

    • Deref (TODO: add examples)

  • Ampersand &

    • (TODO: add examples)

  • Single quote '

    • "quoting" with the ' symbol is used in Clojure to command the reader to read but not evaluate the form that follows it. For example, in order to make a list in Clojure, the quoted form '(:a :b :c) correctly makes a list literal, whereas the unquoted form (:a :b :c) does not.

  • Percent Sign % The percent sign is used to denote anonymous function arguments in the following ways: %, %n, and %&

    • Single argument % (TODO: add actual code examples)

    • Numbered arguments %1 %2 %3 etc. (TODO: add actual code examples)

    • "Following arguments" / "the rest of the arguments" %& (TODO: add actual code examples)

  • Semi-colon ;

    • Partial-line or single-line comments can be made with one or more semi-colons. How many semi-colons you use is up to you/your team - as far as the code is concerned, the number of semi-colons does not matter. (TODO: add examples)

  • Up Caret ^

    • Metadata marker (TODO: add examples)

  • Colon :

    • The colon symbol : is used to indicate a keyword. Use keywords instead of strings where appropriate, such as the keys for hash-maps, as they generally provide greater performance and lower memory usage. (TODO: add actual code examples in context)

    • Double colons :: are used to auto-resolve keywords in a given namespace. (TODO: add examples)

  • "/" Namespace separator

  • "\" Character literal

    • Special characters: \newline\space\tab\formfeed\backspace, and \return.

  • Syntax quote `

  • Unquote ~

  • Inner class reference $

  • Multi-symbol Combos

    • "Discard" / "have reader ignore next form" #_ (useful for multi-line comments) (TODO: add examples)

    • Unquote splicing ~@

    • Threading macros

      • Thread-first -> (TODO: add examples)

      • Thread-last ->> (TODO: add examples)

      • some->

      • cond->

      • as->

    • #: and #:: Namespace Map Syntax

    • <symbol># Generation of new symbols ("gensym")

    • #? Reader conditional

    • #?@ Splicing reader conditional

    • *var-name* "earmuffs" to denote "special vars", "dynamic vars"

      • Examples: Core Clojure standard out and standard in streams, *out* and *in* respectively

Runtimes (1)