পৃষ্ঠাসমূহ

Search Your Article

CS

Pageviews

Showing posts with label Clojure Tutorial. Show all posts
Showing posts with label Clojure Tutorial. Show all posts

Friday, January 27, 2017

Clojure - Overview

Clojure is a high level, dynamic functional programming language. Clojure is designed based on the LISP programming language and has compilers which makes it run on both Java and .Net runtime environment.
Before we talk about Clojure, let’s just have a quick description of LISP programming language.

Clojure - Environment

Try it Option Online

We have set up the Clojure Programming environment on-line, so that you can compile and execute all the available examples on line. It gives you confidence in what you are reading and enables you to verify the programs with different options. Feel free to modify any example and execute it on-line.

Clojure - Basic Syntax

In order to understand the basic syntax of Clojure, let’s first look at a simple Hello World program.

Hello World as a Complete Program

Write ‘Hello world’ in a complete Clojure program. Following is an example.

Clojure - REPL

REPL (read-eval-print loop) is a tool for experimenting with Clojure code. It allows you to interact with a running program and quickly try out if things work out as they should. It does this by presenting you with a prompt where you can enter the code. It then reads your input, evaluates it, prints the result, and loops, presenting you with a prompt again.

Clojure - Data Types

Clojure offers a wide variety of built-in data types.

Built-in Data Types

Following is a list of data types which are defined in Clojure.
  • Integers − Following are the representation of Integers available in Clojure.
    • Decimal Integers (Short, Long and Int) − These are used to represent whole numbers. For example, 1234.

Clojure - Variables

In Clojure, variables are defined by the ‘def’ keyword. It’s a bit different wherein the concept of variables has more to do with binding. In Clojure, a value is bound to a variable. One key thing to note in Clojure is that variables are immutable, which means that in order for the value of the variable to change, it needs to be destroyed and recreated again.

Clojure - Operators

An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations.
Clojure has the following types of operators −
  • Arithmetic operators
  • Relational operators
  • Logical operators
  • Bitwise operators

Clojure - Loops

So far we have seen statements which are executed one after the other in a sequential manner. Additionally, statements are provided in Clojure to alter the flow of control in a program’s logic. They are then classified into flow of control statements which we will see in detail.

Clojure - Decision Making

Decision-making structures require that the programmer specify one or more conditions to be evaluated or tested by the program, along with a statement or statements to be executed if the condition is determined to be true, and optionally, other statements to be executed if the condition is determined to be false.

Clojure - Functions

Clojure is known as a functional programming language, hence you would expect to see a lot of emphasis on how functions work in Clojure. This chapter covers what all can be done with functions in Clojure.

Clojure - Numbers

Numbers datatype in Clojure is derived from Java classes.
Clojure supports integer and floating point numbers.
  • An integer is a value that does not include a fraction.
  • A floating-point number is a decimal value that includes a decimal fraction.

Clojure - Recursion

We have seen the recur statement in an earlier topic and whereas the ‘for’ loop is somewhat like a loop, recur is a real loop in Clojure.

Clojure - File I/O

Clojure provides a number of helper methods when working with I/O. It offers easier classes to provide the following functionalities for files.
  • Reading files
  • Writing to files
  • Seeing whether a file is a file or directory

Clojure - Strings

A String literal is constructed in Clojure by enclosing the string text in quotations. Strings in Clojure need to be constructed using the double quotation marks such as “Hello World”.

Clojure - Lists

List is a structure used to store a collection of data items. In Clojure, the List implements the ISeq interface. Lists are created in Clojure by using the list function.

Clojure - Sets

Sets in Clojure are a set of unique values. Sets are created in Clojure with the help of the set command.

Example

Following is an example of the creation of sets in Clojure.
(ns clojure.examples.example
   (:gen-class))

Clojure - Vectors

A Vector is a collection of values indexed by contiguous integers. A vector is created by using the vector method in Clojure.

Clojure - Maps

A Map is a collection that maps keys to values. Two different map types are provided - hashed and sorted. HashMaps require keys that correctly support hashCode and equals. SortedMaps require keys that implement Comparable, or an instance of Comparator.

Clojure - Namespaces

Namespaces in Clojure are used to differentiate classes into separate logical spaces just like in Java. Consider the following statement.
(:require [clojure.set :as set])

Clojure - Exception Handling

Exception handling is required in any programming language to handle the runtime errors so that the normal flow of the application can be maintained. Exception usually disrupts the normal flow of the application, which is the reason why we need to use exception handling in our application.