(ns clojure-tutorialspoint.core)


; Adapted from 
;Learn Clojure
;tutorialspoint
;https://www.tutorialspoint.com/clojure/clojure_loops.htm

(defn hello-world [username]
      (println (format "Hello, %s" username)))
   
   (hello-world "world")
   
   (+ 4 6)
   
   (ns clojure.examples.hello
   (:gen-class))

(defn hello-world [username]
   (println (format "Hello, %s" username)))

(hello-world "world")

(ns clojure.examples.hello
   (:gen-class))
(defn Example []
   (println (str "Hello World"))
   (println (+ 1 2)))
(Example)

*ns*

(ns example.namespace
  (:require [clojure.java.io])
  (:use [clojure.data])
  (:import [java.util List Set]))


(ns clojure.examples.hello
   (:gen-class))

;; This program displays Hello World
(defn Example []
   (println "Hello World"))
(Example)



(ns clojure.examples.hello
      (:gen-class))
   (defn Example []
      (println (+ 1 2 3)))
   (Example)
   
   
   (+ 1 2 3 4 5); 15
(+ 1, 2, 3, 4, 5);15


;
(ns clojure.examples.hello
   (:gen-class))

;; This program displays Hello World
(defn Example []
   ;; The below code declares a integer variable
   (def x 1)
   
   ;; The below code declares a float variable
   (def y 1.25)
   
   ;; The below code declares a string variable
   (def str1 "Hello")
   (println x)
   (println y)
   (println str1))
(Example); 1 1.25 Hello

; Variables declaration
(ns clojure.examples.hello
   (:gen-class))

;; This program displays Hello World
(defn Example []
   ;; The below code declares a integer variable
   (def x 1)
   
   ;; The below code declares a float variable
   (def y 1.25)

   ;; The below code declares a string variable
   (def str1 "Hello")
   
   ;; The below code declares a boolean variable
   (def status true))
(Example)

;declaration
(ns clojure.examples.hello
   (:gen-class))

;; This program displays Hello World
(defn Example []
   ;; The below code declares a Boolean variable with the name of status
   (def status true)
   
   ;; The below code declares a Boolean variable with the name of STATUS
   (def STATUS false)
   
   ;; The below code declares a variable with an underscore character.
   (def _num1 2))
(Example)

;printing
(ns clojure.examples.hello
   (:gen-class))

;; This program displays Hello World
(defn Example []
   ;; The below code declares a integer variable
   (def x 1)
   
   ;; The below code declares a float variable
   (def y 1.25)
   
   ;; The below code declares a string variable
   (def str1 "Hello")
   (println x)
   (println y)
   (println str1))
(Example)

;OPERATORS (operator operand1 operand2 operandn)
(+ 1 2)
(= 2 2)
(not false) ; true
(not (= 2 3));true
(or true true); true


; Bits
(ns clojure.examples.hello
      (:gen-class))
   
   ;; This program displays Hello World
   (defn Example []
      (def x (bit-and 00111100 00001101))
      (println x)
      
      (def x (bit-or 00111100 00001101))
      (println x)
      
      (def x (bit-xor 00111100 00001101))
      (println x)) 
   (Example); #'clojure.examples.hello/Example 576 37441 36865
nil

;LOOPS
(ns clojure.examples.hello
   (:gen-class))

;; This program displays Hello World
(defn Example []
   (def x (atom 1))
   (while ( < @x 5 )
      (do
         (println @x)
         (swap! x inc))))
(Example); 1 2 3 4

(ns clojure.examples.hello
   (:gen-class))

;; Do with sequence
(defn Example []
   (doseq [n [0 1 2]]
   (println n)))
(Example);0 1 2

(ns clojure.examples.hello
   (:gen-class))

;; Do times
(defn Example []
   (dotimes [n 5]; seq 0...4
   (println n)))
(Example); 0 1 2 3 4


;; Loop

(ns clojure.examples.hello
   (:gen-class))

(defn Example []
   (loop [x 10]
      (when (> x 1)
         (println x)
         (recur (- x 2))))) 
(Example); 10 8 6 4 2


;Decision-making structures
=> (ns clojure.examples.hello
      (:gen-class))
   
   (defn Example [] (
      if ( = 2 2)
      (println "Values are equal")
      (println "Values are not equal")))
   (Example); Values are equal

   ;if-do
(ns clojure.examples.hello
      (:gen-class))
   
   ;; This program displays Hello World
   (defn Example [] (
      if (= 2 2)
         (do(println "Both the values are equal")
            (println "true"))
         (do(println "Both the values are not equal")
            (println "false"))))
   (Example);Both the values are equal   true
   
   
   
   
   (ns clojure.examples.hello
   (:gen-class))
(defn Example [] (
   if ( and (= 2 2) (= 3 3))
   (println "Values are equal")
   (println "Values are not equal")))
(Example);Values are equal

;case
(ns clojure.examples.hello
   (:gen-class))

;; This program displays Hello World
(defn Example []
   (def x 5) 
   (case x 5 (println "x is 5")
      10 (println "x is 10")
      (println "x is neither 5 nor 10")))
(Example); x is 5
   

;FUNCTIONS

(ns clojure.examples.hello
   (:gen-class))

;; This program displays Hello World
(defn Example []
   (def x 1)
   (def y 1.25)
   (def str1 "Hello")
   (println x)
   (println y)
   (println str1))
(Example)



(ns clojure.examples.hello
      (:gen-class))
   
   ;; 
   (defn Example []
      ((fn [x] (* 2 x)) 2))
   (Example); 4
   
   
   (defn demo1 [] (* 2 2))
   (demo1);4
(defn demo2 [x] (* 2 x))
(demo2 3) ; 6
(defn demo3 [x y] (* 2 x y))
(demo3 2 3); 12
(demo3 demo1 4);error
(demo3 (demo1) 4); 32
(demo3 (demo2 5) 7);140

   
(defn demo 
   [message & others]
   (str message (clojure.string/join " " others)))
(demo "Hello" "This" "is" "the" "message"); HelloThis is the message

;Higher order function = functions that 
;that take other functions as arguments
(filter even? (range 0 10)); (0 2 4 6 8)

;NUMBERS and their tests
(def x 5)
(def y 5.25)

(ns clojure.examples.hello
      (:gen-class))
   
   ;; This program displays Hello World
   (defn Example []
      (def x 5)
      (def y 5.25)
      (println (type x))
      (println (type y)))
   (Example);nil #'clojure.examples.hello/Example java.lang.Long java.lang.Double nil
   
   (ns clojure.examples.hello
   (:gen-class))

;; zero?
(defn Example []
   (def x (zero? 0))
   (println x)
   
   (def x (zero? 0.0))
   (println x)
   
   (def x (zero? 1))
   (println x))
(Example);true true false

;;pos?
(ns clojure.examples.hello
   (:gen-class))

;; This program displays Hello World
(defn Example []
   (def x (pos? 0));false
   (println x)
   
   (def x (pos? -1))
   (println x);false
   
   (def x (pos? 1))
   (println x));true
(Example)


;; neg?
(ns clojure.examples.hello
   (:gen-class))

(defn Example []
   (def x (neg? -1))
   (println x);true
   
   (def x (neg? 0))
   (println x) false
  
   (def x (neg? 1))
   (println x)) false
(Example)

;even?
(ns clojure.examples.hello
   (:gen-class))

;; This program displays Hello World
(defn Example []
   (def x (even? 0))
   (println x) ;true
   
   (def x (even? 2))
   (println x);true
   
   (def x (even? 3))
   (println x));false
(Example)

;odd?
(ns clojure.examples.hello
   (:gen-class))
;; This program displays Hello World
(defn Example []
   (def x (odd? 0))
   (println x) ;false
   
   (def x (odd? 2))
   (println x);false
   
   (def x (odd? 3))
   (println x));true
(Example)

;number?
(ns clojure.examples.hello
   (:gen-class))

 ;; This program displays Hello World
(defn Example []
   (def x (number? 0))
   (println x);true
   
   (def x (number? 0.0))
   (println x);true
   
   (def x (number? :a))
   (println x));false
(Example)

;number?
(ns clojure.examples.hello
   (:gen-class))

;; This program displays Hello World
(defn Example []
   (def x (integer? 0))
   (println x);true
   
   (def x (integer? 0.0))
   (println x));false
(Example)

;float?
(ns clojure.examples.hello
   (:gen-class))

;; This program displays Hello World
(defn Example []
   (def x (float? 0))
   (println x);false
   
   (def x (float? 0.0))
   (println x));true
(Example)

;RECURSION
(ns clojure.examples.hello
   (:gen-class))

;; This program displays Hello World
(defn Example []
   (loop [i 0]
      (when (< i 5)
      (println i)
      (recur (inc i)))))
(Example);0 1 2 3 4

;File I/O
(ns clojure.examples.hello
   (:gen-class))

;; The slurp command opens a reader on a file and 
;reads all its contents, returning a string.
(defn Example []
   (def string1 (slurp "Example.txt"))
   (println string1))
(Example)

;STRINGS
(ns clojure.examples.hello
   (:gen-class))
(defn hello-world []
   (println "Hello World")
   (println "This is a demo application"))
(hello-world)

;Basic String Operations

;Clojure has a number of operations that can be performed on strings. Following are the operations.
;S.No. 	String Operations & Description


;1 	str
;The concatenation of strings can be done by the simple str function.
(ns clojure.examples.hello
   (:gen-class))
(defn hello-world []
   (println (str "Hello" "World"));HelloWorld
   (println (str "Hello" "World" "Again")));hellowWorldAgain
(hello-world)





;2 	format
;The formatting of strings can be done by the simple format function. The format function formats a string using java.lang.String.format.
(ns clojure.examples.hello
   (:gen-class))
(defn hello-world []
   (println (format "Hello , %s" "World"));Hello , World
   (println (format "Pad with leading zeros %06d" 1234)));Pad with leading zeros 001234
(hello-world)

;3 	count chars
(ns clojure.examples.hello
   (:gen-class))
(defn hello-world []
   (println (count "Hello u")))
(hello-world);7
;


;4 	subs
;Returns the substring of ‘s’ beginning at start inclusive, 
;and ending at end (defaults to length of string), exclusive.
(ns clojure.examples.hello
   (:gen-class))
(defn hello-world []
   (println (subs "HelloWorld" 2 5));llo
   (println (subs "HelloWorld" 5 7)));Wo
(hello-world)

;5 	compare; Returns a negative number, zero, or a positive number 
;when ‘x’ is logically 'less than', 'equal to', or 'greater than' ‘y’.
(ns clojure.examples.hello
   (:gen-class))
(defn hello-world []
   (println (compare "Hello" "hello"));-35
   (println (compare "Hello" "Hello")));0
(hello-world)



;6 	lower-case
;Converts string to all lower-case.


;7 	upper-case
;Converts string to all upper-case.


;8 	join
;Returns a string of all elements in collection, 
;as returned by (seq collection), 
;separated by an optional separator.
(ns clojure.examples.hello
   (:gen-class))
(defn hello-world []
   (println (clojure.string/join ", " [1 2 3])))
(hello-world) 1, 2, 3

;9 	split
;Splits string on a regular expression.
(ns clojure.examples.hello
   (:gen-class))
(defn hello-world []
   (println (clojure.string/split "Hello World" #" ")))
(hello-world);[Hello World]


;10 	split-lines
;Split strings is based on the escape characters \n or \r\n.
(ns clojure.examples.hello
   (:gen-class))
(defn hello-world []
   (println (clojure.string/split-lines "Hello\nWorld")))
(hello-world);[Hello World]

;11 	reverse
;Reverses the characters in a string.


;12 	replace
;Replaces all instance of a match in a string with the replacement string.
(ns clojure.examples.hello
   (:gen-class))
(defn hello-world []
   (println (clojure.string/replace "The tutorial is about Groovy" #"Groovy"
      "Clojure")))
(hello-world);The tutorial is about Clojure



;13 	trim
;Removes whitespace from both ends of the string.


;14 	triml
;Removes whitespace from the left hand side of the string.


;15 	trimr
;Removes whitespace from the right hand side of the string.


;LISTS
(ns clojure.examples.example
   (:gen-class))
(defn example []
   (println (list 1 2 3 4)))
(example);(1 2 3 4)

(ns clojure.examples.example
   (:gen-class))
(defn example []
   (println (list 'a 'b 'c 'd)))
(example); (a b c d)

;list*
(ns clojure.examples.example
   (:gen-class))
(defn example []
   (println (list* 1 [2,3])))
(example); (1 2 3)

;first
(ns clojure.examples.example
   (:gen-class))
(defn example []
   (println (first (list 1 2,3))))
(example);1

;nth
(ns clojure.examples.example
   (:gen-class))
(defn example []
   (println (nth (list 1 2,3) 0)); 1
   (println (nth (list 1 2,3) 2))); 3
(example)

;cons
(ns clojure.examples.example
   (:gen-class))
(defn example []
   (println (cons 0 (list 1 2,3))))
(example); (0 1 2 3)

;conj
(ns clojure.examples.example
   (:gen-class))
(defn example []
   (println (conj (list 1 2,3) 4 5)))
(example);(4 5 1 2 3)

;rest: (delete the first element) 
;Returns the remaining items in the list after the first item.
(ns clojure.examples.example
   (:gen-class))
(defn example []
   (println (rest (list 1 2,3))))
(example); (2 3)


;SETS
(ns clojure.examples.example
   (:gen-class))
(defn example []
   (println (set '(1 1 2 2))))
(example); #{1 2}


;Following are the methods available in Clojure for sets.

;1 	sorted-set
;Returns a sorted set of elements.
(ns clojure.examples.example
   (:gen-class))
(defn example []
   (println (sorted-set 3 2 1)))
(example)#{1 2 3}

;2 	get if it exists
(ns clojure.examples.example
               (:gen-class))
            (defn example []
               (println (get (set '(3 2 1 -1 0)) 0))
               (println (get (set '(3 2 1 -1)) 1))
               (println (get (set '(3 2 1 -1)) 2))
               (println (get (set '(3 2 1 -1)) 3))
               (println (get (set '(3 2 1 -1)) 4))
               (println (get (set '(3 2 1 -1)) -1))
               )
            (example); 0 1 2 3 nil -1

;3 	contains?
;Finds out whether the set contains a certain element or not.
(ns clojure.examples.example
      (:gen-class))
   (defn example []
      (println (contains? (set '(3 2 1)) 2));true
      (println (contains? (set '(3 2 1)) 5)));false
   (example)

;4 	conj
;Appends an element to the set and returns the new set of elements.

;5 	disj
;Disjoins an element from the set.

;6 	union
;Joins   sets.
(ns clojure.examples.example
   (:require [clojure.set :as set])
   (:gen-class))
(defn example []
   (println (set/union #{1 2} #{3 4})))
(example)#{1 4 3 2}

;7 	difference
;Return a set that is the first set without elements of the remaining sets.


;8 	intersection
;Return a set that is the intersection of the input sets.


;9 	subset?
;Is set1 a subset of set2?

;10 	superset?
;Is set1 a superset of set2?


;VECTORS
(ns clojure.examples.example
   (:require [clojure.set :as set])
   (:gen-class))
(defn example []
   (println (vector 1 2 3)))
(example);[1 2 3]

;Following are the methods available in Clojure.
;1 	vector-of
;Creates a new vector of a single primitive type ‘t’, 
;where ‘t’ is one of :int :long :float :double :byte :short :char or :boolean.


;2 	nth
;This function returns the item in the nth position in the vector.

;3 	get
;Returns the element at the index position in the vector.


;4 	conj
;Appends an element to the vector and returns the new set of vector elements.


;5 	pop
;For a list or queue, returns a new list/queue without the first item, for a vector, returns a new vector without the last item.


;6 	subvec
;Returns a sub vector from a starting and ending index.


;MAPS

(ns clojure.examples.example
   (:gen-class))
(defn example []
   (def demokeys (hash-map "z" "1" "b" "2" "a" "3"))
   (println demokeys))
(example);{z 1, a 3, b 2}


;sorted-map
(ns clojure.examples.example
   (:gen-class))
(defn example []
   (def demokeys (sorted-map "z" "1" "b" "2" "a" "3"))
   (println demokeys))
(example);{a 3, b 2, z 1}

;Following are the methods available for maps:

;1 	get
;Returns the value mapped to key, not-found or nil if key is not present.
(ns clojure.examples.example
   (:gen-class))
(defn example []
   (def demokeys (hash-map "z" "1" "b" "2" "a" "3"))
   (println demokeys)
   (println (get demokeys "b")))
(example);{z 1, a 3, b 2}  2

;2 	contains?
;See whether the map contains a required key.
(ns clojure.examples.example
   (:gen-class))
(defn example []
   (def demokeys (hash-map "z" "1" "b" "2" "a" "3"))
   (println (contains? demokeys "b"));true
   (println (contains? demokeys "x")));false
(example)

;3 	find
;Returns the map entry for the key.
(ns clojure.examples.example
   (:gen-class))
(defn example []
   (def demokeys (hash-map "z" "1" "b" "2" "a" "3"))
   (println demokeys)
   (println (find demokeys "b"))
   (println (find demokeys "x")))
(example);{z 1, a 3, b 2} [b 2] nil

;4 	keys
;Returns the list of keys in the map.


;5 	vals
;Returns the list of values in the map.
(ns clojure.examples.example
   (:gen-class))
(defn example []
   (def demokeys (hash-map "z" "1" "b" "2" "a" "3"))
   (println (vals demokeys)))
(example); (1 3 2)


;6 	dissoc
;Dissociates a key value entry from the map.

;7 	merge
;Merges two maps entries into one single map entry.


;8 	merge-with
;Returns a map that consists of the rest of the maps conj-ed onto the first.


;9 	select-keys
;Returns a map containing only those entries in map whose key is in keys.


;10 	rename-keys
;Renames keys in the current HashMap to the newly defined ones.

;11 	map-invert
;Inverts the maps so that the values become the keys and vice versa.




;NAMESAPCES
;Let’s look at the different methods available for namespaces.
;1 	*ns*
;This is used to look at your current namespace.

;2 	ns
;This is used to create a new namespace and associate it with the running program.


;3 	alias
;Add an alias in the current namespace to another namespace. 
;Arguments are two symbols: the alias to be used and the symbolic name of the target namespace.


;4 	all-ns
;Returns a list of all namespaces.


;5 	find-ns
;Finds and returns a particular namespace.


;6 	ns-name
;Returns the name of a particular namespace.


;7 	ns-aliases
;Returns the aliases, which are associated with any namespaces.


;8 	ns-map
;Returns a map of all the mappings for the namespace.
(ns clojure.examples.example
   (:require [clojure.set :as set])
   (:gen-class))
(defn example []
   (println (count (ns-map 'clojure.core))))
(example);822

;9 	un-alias
;Returns a map containing only those entries in map whose key is in keys.


;Exception handling
(ns clojure.examples.example
   (:gen-class))
(defn Example []
   (try
      (aget (int-array [1 2 3]) 5)
      (catch Exception e (println (str "caught exception: " (.toString e))))
      (finally (println "This is our final block")))
   (println "Let's move on"))
(Example)
;caught exception: java.lang.ArrayIndexOutOfBoundsException: 5
;This is our final block
;Let's move on

;SEQUENCES
(ns clojure.examples.example
   (:gen-class))

;print sequence
(defn Example []
   (println (seq [1 2 3])))
(Example);(1 2 3)

;Following are the various methods available for sequences.

;1 	cons
;Returns a new sequence where ‘x’ is the first element and ‘seq’ is the rest.

;2 	conj
;Returns a new sequence where ‘x’ is the element that is added to the end of the sequence.

;3 	concat
;This is used to concat two sequences together.


;4 	distinct
;Used to only ensure that distinct elements are added to the sequence.


;5 	reverse
;Reverses the elements in the sequence.


;6 	first
;Returns the first element of the sequence.


;7 	last
;Returns the last element of the sequence.


;8 	rest
;Returns the entire sequence except for the first element.


;9 	sort
;Returns a sorted sequence of elements.


;10 	drop
;Drops elements from a sequence based on the number of elements, which needs to be removed.


;11 	take-last
;Takes the last list of elements from the sequence.

;12 	take
;Takes the first list of elements from the sequence.


;13 	split-at
;Splits the sequence of items into two parts. A location is specified at which the split should happen.
(ns clojure.examples.example
   (:gen-class))

;; splits
(defn Example []
   (def seq1 (seq [5 4 3 2 1]))
   (println (split-at 2 seq1)))
(Example);[(5 4) (3 2 1)]

;REGEX Regular expressions = patterns for searching
;The following methods are available for regular expressions.
;1 	re-pattern
;Returns an instance of java.util.regex.Pattern. 
;This is then used in further methods for pattern matching.

(ns clojure.examples.example
   (:gen-class))

;; This program will create a pattern object, 
;which will be a pattern of one or more digits.
(defn Example []
   (def pat (re-pattern "\\d+")))
(Example)




;2 	refind
;Returns the next regex match, if any, of string to pattern, using java.util.regex.Matcher.find()


;3 	replace
;The replace function is used to replace a substring in a string with a new string value. 
;The search for the substring is done with the use of a pattern.
(ns clojure.examples.example
   (:gen-class))

;; 
(defn Example []
   (def pat (re-pattern "\\d+"))
   (def newstr (clojure.string/replace "abc123de" pat "789"))
   (println newstr))
(Example);abc789de

;4 	replace-first
;The replace function is used to replace a substring in 
;a string with a new string value, 
;but only for the first occurrence of the substring. 
;The search for the substring is done with the use of a pattern.

;PREDICATES
;Predicates are functions that evaluate a condition 
;and provide a value of either true or false.

(ns clojure.examples.example
   (:gen-class))

;; This program displays Hello World
(defn Example []
   (def x (even? 0))
   (println x)
   
   (def x (neg? 2))
   (println x)
   
   (def x (odd? 3))
   (println x)
   
   (def x (pos? 3))
   (println x))
(Example)

;The following methods are available for predicates.
;1 	every-pred
;Takes a set of predicates and returns a function ‘f’ 
;that returns true if all of its composing predicates return 
;a logical true value against all of its arguments, else it returns false.

;2 	every?
;Returns true if the predicate is true for every value, else false.

;3 	some
;Returns the first logical true value for any predicate value of x in the collection of values.

;4 	not-any?
;Returns false if any of the predicates of the values in a collection are logically true, else true.
(ns clojure.examples.example
   (:gen-class))
(defn Example []
   (println (not-any? even? '(2 4 6))))
(Example);false

;DESTRUCTURING
(ns clojure.examples.example
   (:gen-class))
(defn Example []
   (def my-vector [1 2 3 4])
   (let [[a b c d] my-vector]
   (println a b  d)))
(Example);1 2 4

;the-rest = no matching elements
(ns clojure.examples.example
   (:gen-class))
(defn Example []
   (def my-vector [1 2 3 4])
   (let [[a b & the-rest] my-vector]
   (println a b the-rest)))
(Example);1 2 (3 4)

(ns clojure.examples.example
   (:gen-class))
(defn Example []
   (def my-map {"a" 1 "b" 2})
   (let [{a "a" b "b" c "c"} my-map]
   (println a b c)))
(Example); 1 2 nil

;DATE AND TIME
(ns example)
(defn Example []
   (def date (.toString (java.util.Date.)))
   (println date))
(Example);Thu May 25 08:22:05 COT 2017

(ns example)
(defn Example []
   (def date (.format (java.text.SimpleDateFormat. "MM/dd/yyyy") (new java.util.Date)))
   (println date))
(Example);05/25/2017

;Number of miliseconds since January 1, 1970, 00:00:00 GMT 
(ns example)
(import java.util.Date)
(defn Example []
   (def date (.getTime (java.util.Date.)))
   (println date))
(Example);1495718634812

;ATOMS: objects that can be changed and watched over with watchers
(ns clojure.examples.example
   (:gen-class))
(defn example []
   (def myatom (atom 1))
   (println @myatom))
(example);1

;The value of atom is accessed by using the @ symbol. 
;Clojure has a few operations that can be performed on atoms. 
;Following are the operations.
;1 	reset!
;Sets the value of atom to a new value without regard for the current value.

;2 	compare-and-set!
;Atomically sets the value of atom to the new value if and only if the current value of the atom is identical to the old value held by the atom. Returns true if set happens, else it returns false.

;3 	swap!
;Atomically swaps the value of the atom with a new one based on a particular function.
(ns clojure.examples.example
   (:gen-class))
(defn example []
   (def myatom (atom 1))
   (println @myatom)
   
   (swap! myatom inc)
   (println @myatom))
(example);1 2

;METADATA
;In Clojure, metadata is used to annotate the data in a collection or for the data stored in a symbol. 
;The following operations are possible in Clojure with regards to metadata.
;1 	meta-with
;This function is used to define a metadata map for any object.
(ns clojure.examples.example
   (:gen-class))
(defn Example []
   (def my-map (with-meta [1 2 3] {:prop "values"}))
   (println (meta my-map)))
(Example);{:prop values}



;2 	meta
;This function is used to see if any metadata is associated with an object.

;3 	vary-meta
;Returns an object of the same type and value as the original object, but with a combined metadata.


;STRUCTMAPS similar to Java classes
;StructMaps are used for creating structures in Clojure. 
;For example, if you wanted to create a structure which comprised of an Employee Name and Employeeid, 
;you can do that with StructMaps.




;The following operations are possible in Clojure with regards to StructMaps.
;1 	defstruct
;This function is used for defining the structure which is required.
(ns clojure.examples.example
   (:gen-class))
(defn Example []
   (println (defstruct Employee :EmployeeName :Employeeid)))
(Example);#'clojure.examples.example/Employee

;2 	struct
;This function is used to define a structure object of the type, which is created by the defstruct operation.
(ns clojure.examples.example
   (:gen-class))
(defn Example []
   (defstruct Employee :EmployeeName :Employeeid)
   (def emp (struct Employee "John" 1))
   (println emp))
(Example);{:EmployeeName John, :Employeeid 1}

;3 	struct-map
;This function is used to specifically assign values to key values 
;by explicitly defining which values get assigned to which keys in the structure.
(ns clojure.examples.example
   (:gen-class))
(defn Example []
   (defstruct Employee :EmployeeName :Employeeid)
   (def emp (struct-map Employee :EmployeeName "John" :Employeeid 1))
   (println emp))
(Example);{:EmployeeName John, :Employeeid 1}



;4 	Accessing Individual Fields
;Individual fields of the structure can be accessed by accessing the keys along with the structure object.
(ns clojure.examples.example
   (:gen-class))
(defn Example []
   (defstruct Employee :EmployeeName :Employeeid)
   (def emp (struct-map Employee :EmployeeName "John" :Employeeid 1))
   (println (:Employeeid emp))
   (println (:EmployeeName emp)))
(Example); 1 John



;5 	Immutable Nature
;By default structures are also immutable, so if we try to change the value of a particular key, it will not change.
(ns clojure.examples.example
   (:gen-class))
(defn Example []
   (defstruct Employee :EmployeeName :Employeeid)
   (def emp (struct-map Employee :EmployeeName "John" :Employeeid 1))
   (println (:EmployeeName emp))
   
   (assoc emp :EmployeeName "Mark")
   (println (:EmployeeName emp)))
(Example);John John


(ns clojure.examples.example
   (:gen-class))
(defn Example []
   (defstruct Employee :EmployeeName :Employeeid)
   (def emp (struct-map Employee :EmployeeName "John" :Employeeid 1))
   (def newemp (assoc emp :EmployeeName "Mark"))
   (println newemp))
(Example);{:EmployeeName Mark, :Employeeid 1}

;6 	Adding a New Key to the Structure
;Since structures are immutable, the only way that another key can be added to the structure 
;is via the creation of a new structure. 
;An example on how this can be achieved is shown in the following program.
(ns clojure.examples.example
   (:gen-class))
(defn Example []
   (defstruct Employee :EmployeeName :Employeeid)
   (def emp (struct-map Employee :EmployeeName "John" :Employeeid 1))
   (def newemp (assoc emp :EmployeeRank "A"))
   (println newemp))
(Example);{:EmployeeName John, :Employeeid 1, :EmployeeRank A}


;AGENTS
;Agents provide independent, asynchronous change of individual locations. 
;The following operations are possible in Clojure with regards to Agents.
;1 	agent
;An agent is created by using the agent command.
(ns clojure.examples.example
   (:gen-class))
(defn Example []
   (def counter (agent 0))
   (println counter))
(Example);#<Agent@7f0bc53e: 0>


(ns clojure.examples.example
   (:gen-class))
(defn Example []
   (def counter (agent 0))
   (println @counter))
(Example);0


;2 	send
;This function is used to send across a value to the agent.
(ns clojure.examples.example
   (:gen-class))
(defn Example []
   (def counter (agent 0))
   (println @counter)
   
   (send counter + 100)
   (println "Incrementing Counter")
   (println @counter))
(Example);
;0
;Incrementing Counter
;100


;3 	shutdown-agents
;This function is used to shut down any running agents.


;4 	send-off
;There are instances wherein an agent is assigned a function which is blocking in nature.
(ns clojure.examples.example
   (:gen-class))
(defn Example []
   (def counter (agent 0))
   (println @counter)
   
   (send-off counter + 100)
   (println @counter)
   (println @counter))
(Example); 0 0 100

;5 	await-for
;Since there is a delay when a value of an agent is updated, Clojure provided a ‘await-for’ function which is used to specify time in milliseconds to wait for the agent to be updated.


;6 	await
;Blocks the current thread (indefinitely!) until all actions dispatched thus far, from this thread or agent, to the agent(s) have occurred. Will block on failed agents.


;7 	agent-error
;Returns the exception thrown during an asynchronous action of the agent, if the agent fails. Returns nil if the agent does not fail.


;WATCHERS
;Watchers are functions added to variable types such as atoms and 
;reference variables which get invoked when a value of the variable type changes. 
(ns clojure.examples.example
   (:gen-class))
(defn Example []
   (def x (atom 0))
   (add-watch x :watcher
      (fn [key atom old-state new-state]
      (println "The value of the atom has been changed")
      (println "old-state" old-state)
      (println "new-state" new-state)))
(reset! x 2))
(Example)
;The value of the atom has been changed
;old-state 0
;new-state 2

;remove watch
(ns clojure.examples.example
   (:gen-class))
(defn Example []
   (def x (atom 0))
   (add-watch x :watcher
      (fn [key atom old-state new-state]
         (println "The value of the atom has been changed")
         (println "old-state" old-state)
         (println "new-state" new-state)))
   (reset! x 2)
   (remove-watch x :watcher)
(reset! x 4))
(Example)
;The value of the atom has been changed
;old-state 0
;new-state 2


;MACROS
;In any language, Macros are used to generate inline code.
; Macros are used to write code-generation routines, 
;which provide the developer a powerful way 
;to tailor the language to the needs of the developer.
(ns clojure.examples.example
      (:gen-class))
   (defn Example []
      (defmacro Simple []
         (println "Hello"))
      (macroexpand '(Simple)))
   (Example);Hello
   
   
(ns clojure.examples.example
   (:gen-class))
(defn Example []
   (defmacro Simple [arg]
      (list 2 arg))
   (println (macroexpand '(Simple 2))))
(Example); (2 2)

;REFERENCES
;Reference values are another way Clojure can work with 
;the demand to have mutable variables. 
;Clojure provides mutable data types such as atoms, agents, and reference types.

;Following are the operations available for reference values.
;1 	ref
;This is used to create a reference value. 
;When creating a reference value, there is an option to provide a validator function, 
;which will validate the value created.
(ns clojure.examples.example
   (:gen-class))
(defn Example []
   (def my-ref (ref 1 :validator pos?))
   (println @my-ref))
(Example);1



;2 	ref-set
;This function is used to set the value of a reference 
;to a new value irrespective of whatever is the older value.

;alter
;This function is used to alter the value of a reference type but in a safe manner. 
;This is run in a thread, which cannot be accessed by another process.
(ns clojure.examples.example
   (:gen-class))
(defn Example []
   (def names (ref []))
   
   (defn change [newname]
      (dosync
         (alter names conj newname)))
   (change "John")
   (change "Mark")
   (println @names))
(Example);[John Mark]


;4 	dosync
;Runs the expression (in an implicit do) in a transaction that encompasses expression and any nested calls.

;5 	commute
;Commute is also used to change the value of a reference type just like alter and ref-set.

;DATABASES
;the ability to connect to databases as MySQL


;JAVA
;Calling Java Methods
(ns Project
   (:gen-class))
(defn Example []
   (println (.toUpperCase "Hello World")))
(Example);HELLO WORLD

(ns Project
   (:gen-class))
(defn Example []
   (println (.indexOf "Hello World","e")))
(Example);1

(ns Project
   (:gen-class))
(defn Example []
   (def str1 (new String "Hello"))
   (println str1))
(Example);Hello

(ns Project
   (:gen-class))
(defn Example []
   (def my-int(new Integer 1))
   (println (+ 2 my-int)))
(Example);3

(ns Project
   (:gen-class))
(import java.util.Stack)
(defn Example []
   (let [stack (Stack.)]
   (.push stack "First Element")
   (.push stack "Second Element")
   (println (first stack))))
(Example);First Element

(ns Project
   (:gen-class))
(defn Example []
   (println (. Math PI)))
(Example);3.141592653589793

(ns Project
   (:gen-class))
(defn Example []
   (println (.. System getProperties (get "java.version"))))
(Example);1.8.0_131

;CONCURRENT PROGRAMMING
;In Clojure programming most data types are immutable, 
;thus when it comes to concurrent programming, 
;the code using these data types are pretty safe when the code runs on multiple processors. 
;But many a times, there is a requirement to share data, 
;and when it comes to shared data across multiple processors, 
;it becomes necessary to ensure that the state of the data is maintained 
;in terms of integrity when working with multiple processors. 
;This is known as concurrent programming and Clojure provides support for such programming.
(ns clojure.examples.example
   (:gen-class))
(defn Example []
   (def names (ref []))
   
   (defn change [newname]
      (dosync
         (alter names conj newname)))
   (change "John")
   (change "Mark")
   (println @names))
(Example);[John Mark]

(ns clojure.examples.example
   (:gen-class))
(defn Example []
   (def var1 (ref 10))
   (def var2 (ref 20))
   (println @var1 @var2)
   
   (defn change-value [var1 var2 newvalue]
      (dosync
         (alter var1 - newvalue)
         (alter var2 + newvalue)))
   (change-value var1 var2 20)
   (println @var1 @var2))
(Example)
;10 20
;-10 40

;DESKTOP AND WEB APPLICATIONS= GUI of Java
;creating Desktop and Web-based applications.


;TESTING
;Automated Testing


;LIBRARIES FOR MANY TASKS
;Json; XML; csv






