Clojure Study Notes (i)--Introduction, Installation and grammar

Source: Internet
Author: User

What is Clojure

Clojure is a dynamic, strongly-typed language that resides on the JVM.

Features of the Clojure:

    • Functional programming basics, including a set of persistent data structures that can be comparable to typical mutable data structures
    • A mature, efficient runtime environment provided by the JVM: so clojure can use Java class libraries, whereas Clojure libraries can also be used by Java
    • Interoperability with Jvm/java enables many architectural and operational requirements to be met: Clojure code can be packaged like Java code and deployed to any Java application where it can be deployed
    • A set of mechanisms that provide concurrency, parallel semantics: The application type of Clojure forces us to separate the state of the object from the identity of the object (this is a genius idea, see [note]), support for multithreading allows us not to lock manually, unlock can also write multithreaded code
    • is a Lisp dialect, thus providing a very flexible and powerful meta-programming capability: Clojure retains the best features of Lisp, removing the flaws of Lisp dialect.

Installing Clojure

The JRE is required because the Clojure needs to run on the JVM. You can then download the Clojure code in Http://clojure.org/community/downloads. With this, you can run Clojure's repl.

Run REPL

How to run REPL from the command line: 1. Enter the downloaded Clojure directory; 2. Run JAVA-CP Clojure-1.8.0.jar Clojure.main, if you see the following, success:

In order to run conveniently later, you can create a shell script cljrepl.sh, which reads as follows:

#!/bin/sh/home/namenode/code/workspace/clojure-1.8. 0  -cp clojure-1.8. 0

Then, modify the Execute permissions:

chmod +x cljrepl. SH

Then create a soft link:

sudo LN -s/home/namenode/code/workspace/clojure-1.8. 0/cljrepl. SH /bin/cljrepl

This allows the terminal to directly enter the CLJREPL can be directly run Clojure repl.

Installing the Clojure Command tool

You can install Clojure directly with Apt-get under Ubuntu.

sudo Install clojure1. 6

Test, create file Balance.clj (example from Java Virtual machine concurrency programming)

(Def balance (ref 0"balance is" @balance) (DoSync (ref-set balance"balance are now" @balance )

Run: Clojure BALANCE.CLJ will print the results

Grammar

Comparison of function call syntax in Clojure, Java, Python, and Ruby

The same as Sex

Clojure is represented by the Clojure's own data results: Atomic values (strings, numbers, and so on) and sets of literals. This feature is called "similarity", or "code as data".

Clojure does not define a syntax that will not be converted to an AST (abstract sytax tree, an abstraction), and Clojure code is written directly using the Clojure data structure that represents the abstract syntax tree.

Clojure uses data to represent the characteristics of a language code so that Clojure code can be easily used to write and transform other Clojure code. This is the foundation of macro, and the Meta programming tool in Clojure is much more powerful than the kind of macros and other text preprocessor that is available in the C language.

Clojure Reader

The function of Clojure reader is to convert the code written by programmers into Clojure data structures. All of reader's operations are defined by a function called read, which reads the text form of the code from a stream of characters, producing the data structure of the text. Clojure's REPL is the use of reader to read text code, the role of reader can actually be seen as the process of deserialization. The two functions that correspond to read and read-string are PR and PR-STR, and the two functions are the process of serialization.

All clojure data structures and value serialization are both human-readable and machine-readable

Scalar literal string

Like Java and other languages "Hello world"

and Clojure naturally supports multiple lines

Boolean value

Boolean value in Clojure with True and false

Nil

Nil in Clojure is similar to null in Java, and nil is logical false in judgment

Character

The character literal is represented by a backslash plus character:

For Unicode encoding and octal encoding, you can use the pair prefix:

There are also constants for some special characters:

\space\newline\formfeed\ return \backspace\tab

Key words

The keyword is always preceded by a colon, and it can contain any non-null character. If the keyword contains/, it indicates that the keyword is namespace-qualified. If the keyword starts with two colons (::), then it is the keyword for the current namespace. If the keyword starts with a two colon and contains a/, such as:: alias/kw, the keyword in a particular namespace is represented. This design is the same as the use and motivation of namespace entities in XML, meaning that the same name has different values and semantics in different namespaces.

Symbol

The symbol is also an identifier, and the value of the symbol is the value that it represents in the Clojure runtime, which allows the value that Var holds, the Java class, the local reference, and so on.

Digital

Hex: 0xFF octal: 040 (starting with 0) arbitrary binary: BrN (n = number, b for binary) rational number: expressed as a proportional number

Regular expressions

Start with #, do not need to escape backslash

Comments
    • Single-line comments start with semicolons
    • Form-level Comment #_ macro that tells reader to ignore the next Clojure form

Spaces and commas

In Reader's eyes, a comma is a space.

Set literal

Name space

All clojure codes are defined and evaluated in a namespace. Namespaces can Shancheng the Module,java package for Ruby and Python.

A reference type var in clojure is a modifiable memory address that can hold any value, and Var is associated with a symbol in the Var defined namespace, and then we can use this var to get the value of this var.

var is defined by Def in Clojure. Such as:

A var named x is defined in the current namespace user.

The current namespace is always bound to *ns*

Symbol Evaluation Block Evaluation: Quote

Block evaluation can also be expressed in single quotation marks

Code block: Do

Do will pass in all the expressions in turn, and the result of the last expression as the return value

Define VAR:DEF local bindings: Let

All local bindings are immutable, and let's bound arrays can be deconstructed between compilers during compilation, and the use of deconstruction can greatly simplify the operation of extracting the desired data from the bound data.

Deconstruction: Let

Many Clojure functions accept sequential data structures and map as parameters or return values, and accept or return abstract data types. This allows the function to call the Clojure class library, do not need additional code to interface with the implementation of the specific data structure, there is no need for some glue code to do type conversion things, can keep the code simple

Defining functions: FN

The function is to bind the parameter value to the parameter, in the execution

Defn

Defn based on FN, encapsulates the functions of Def and FN, defining a function with a name

Judgment: If,when,cond loop: loop and recur interoperability with Java:. And new exception handling: Try and throw State modification: set! Lock Primitives: Monitor-enter and Monitor-exit reference
    1. http://ifeve.com/stm-1/
    2. Dr. Alan Kay, "on themeaning of" object-oriented programming ", http://userpage.fu-berlin.de/~ram/ Pub/pub_jf47ht81ht/doc_kay_oop_en
    3. Chas Emerick, Brian Carper, Christophe Crand, "Clojure programming"

Clojure Study Notes (i)--Introduction, Installation and grammar

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.