Thursday, September 03, 2020

#100DaysOfClojure (Day [4] Basics)

Today I can dip my toes into the language itself. I will be looking at the some of the syntax and forms, Clojure refers to valid code as forms. Clojure has literals which consist of primitive data types, symbols and collections. 


Clojure also has 2 kinds of structures, literals and operations and which follow this format:

(operation argument)

The ( represents a list and an invocation
The operation is a symbol and a function
The argument can be primitive data, a collection or code

Example of adding integers
(+ 1 1)
=> 2

Example of adding ratios
(+ 1/2 1/3)
=> 5/6

Example of subtracting floating point numbers
(- 1.5 0.2)
=> 1.3

Example of adding Hexadecimals
(+ 0x01 0x02)
=> 3

Example of system documentation
(doc +)
-------------------------
clojure.core/+
([] [x] [x y] [x y & more])
  Returns the sum of nums. (+) returns 0. Does not auto-promote
  longs, will throw on overflow. See also: +'
=> nil
*note, even the documentation follows the format, that is how consistent the structure is

There are 4 types of collections : Lists, Vectors, Maps and Sets. All collections are immutable and persistent.

Examples of the collections
 
'(1 2 3)     ; list : they are ordered and can only be accessed by traversing the order
[1 2 3]      ; vector : vectors can be accessed with a index
#{1 2 3}     ; set : the elements are unique and they provide set operations ( union, intersection etc.)
{:a 1, :b 2} ; map : stores key-value pairs


Tomorrow I will look at some function definitions 

(println "Bye 4 Now!")

No comments: