Clojure – Agents

Clojure – Agents ”; Previous Next As pointed out many times, Clojure is a programming language wherein many of the data types are immutable, which means that the only way one can change the value of a variable is to create a new variable and assign the new value to it. However, Clojure does provide some elements, which can create an mutable state. We have seen that this can be achieved with the atom data type. The other way this can be achieved is via Agents. Agents provide independent, asynchronous change of individual locations. Agents are bound to a single storage location for their lifetime, and only allow mutation of that location (to a new state) to occur as a result of an action. Actions are functions (with, optionally, additional arguments) that are asynchronously applied to an Agent’s state and whose return value becomes the Agent’s new state. The following operations are possible in Clojure with regards to Agents. Sr.No. Operations & Description 1 agent An agent is created by using the agent command. 2 send This function is used to send across a value to the agent. 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. 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. Print Page Previous Next Advertisements ”;

Clojure – Lists

Clojure – Lists ”; Previous Next 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. Example Following is an example of creating a list of numbers in Clojure. Live Demo (ns clojure.examples.example (:gen-class)) (defn example [] (println (list 1 2 3 4))) (example) Output The above code produces the following output. (1 2 3 4) Following is an example of creating a list of characters in Clojure. Live Demo (ns clojure.examples.example (:gen-class)) (defn example [] (println (list ”a ”b ”c ”d))) (example) The above code produces the following output. (a b c d) Following are the list methods available in Clojure. Sr.No. Lists & Description 1 list* Creates a new list containing the items prepended to the rest, the last of which will be treated as a sequence. 2 first This function returns the first item in the list. 3 nth This function returns the item in the ‘nth’ position in the list. 4 cons Returns a new list wherein an element is added to the beginning of the list. 5 conj Returns a new list wherein the list is at the beginning and the elements to be appended are placed at the end. 6 rest Returns the remaining items in the list after the first item. Print Page Previous Next Advertisements ”;

Clojure – Atoms

Clojure – Atoms ”; Previous Next Atoms are a data type in Clojure that provide a way to manage shared, synchronous, independent state. An atom is just like any reference type in any other programming language. The primary use of an atom is to hold Clojure’s immutable datastructures. The value held by an atom is changed with the swap! method. Internally, swap! reads the current value, applies the function to it, and attempts to compare-and-set it in. Since another thread may have changed the value in the intervening time, it may have to retry, and does so in a spin loop. The net effect is that the value will always be the result of the application of the supplied function to a current value, atomically. Example Atoms are created with the help of the atom method. An example on the same is shown in the following program. Live Demo (ns clojure.examples.example (:gen-class)) (defn example [] (def myatom (atom 1)) (println @myatom)) (example) Output The above program produces the following result. 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. Sr.No. Operations & Description 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. Print Page Previous Next Advertisements ”;

Clojure – Numbers

Clojure – Numbers ”; Previous Next 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. Following is an example of numbers in Clojure. (def x 5) (def y 5.25) Where ‘x’ is of the type Integer and ‘y’ is the float. In Java, the following classes are attached to the numbers defined in Clojure. To actually see that the numbers in Clojure are derived from Java classes, use the following program to see the type of numbers assigned when using the ‘def’ command. Example Live Demo (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) The ‘type’ command is used to output the class associated with the value assigned to a variable. Output The above code will produce the following output. Java.lang.long Java.lang.double Number Tests The following test functions are available for numbers. Sr.No. Numbers & Description 1 zero? Returns true if the number is zero, else false. 2 pos? Returns true if number is greater than zero, else false. 3 neg? Returns true if number is less than zero, else false. 4 even? Returns true if the number is even, and throws an exception if the number is not an integer. 5 odd? Returns true if the number is odd, and throws an exception if the number is not an integer. 6 number? Returns true if the number is really a Number. 7 integer? Returns true if the number is an integer. 8 float? Returns true if the number is a float. Print Page Previous Next Advertisements ”;

Clojure – Exception Handling

Clojure – Exception Handling ”; Previous Next 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. Exception is broadly classified into the following categories − Checked Exception − The classes that extend Throwable class except RuntimeException and Error are known as checked exceptions. E.g. IOException, SQLException, etc. Checked exceptions are checked at compile-time. Let’s consider the following program which does an operation on a file called Example.txt. However, there can be always a case wherein the file Example.txt does not exist. Live Demo (ns clojure.examples.example (:gen-class)) ;; This program displays Hello World (defn Example [] (def string1 (slurp “Example.txt”)) (println string1)) (Example) If the file Example.txt does not exist, then the following exception will be generated by the program. Caused by: java.io.FileNotFoundException: Example.txt (No such file or directory) at java.io.FileInputStream.open0(Native Method) at java.io.FileInputStream.open(FileInputStream.java:195) at java.io.FileInputStream.<init>(FileInputStream.java:138) at clojure.java.io$fn__9185.invoke(io.clj:229) at clojure.java.io$fn__9098$G__9091__9105.invoke(io.clj:69) at clojure.java.io$fn__9197.invoke(io.clj:258) at clojure.java.io$fn__9098$G__9091__9105.invoke(io.clj:69) From the above exception, we can clearly see that the program raised a FileNotFoundException. Unchecked Exception − The classes that extend RuntimeException are known as unchecked exceptions. For example, ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException, etc. Unchecked exceptions are not checked at compile-time rather they are checked at runtime. One classical case is the ArrayIndexOutOfBoundsException which happens when you try to access an index of an array which is greater than the length of the array. Following is a typical example of this sort of mistake. Live Demo (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) When the above code is executed, the following exception will be raised. caught exception: java.lang.ArrayIndexOutOfBoundsException: 5 This is our final block Let”s move on Error Error is irrecoverable e.g. OutOfMemoryError, VirtualMachineError, AssertionError, etc. These are errors which the program can never recover from and will cause the program to crash. We now need some mechanism to catch these exceptions so that the program can continue to run if these exceptions exist. The following diagram shows how the hierarchy of exceptions in Clojure is organized. It’s all based on the hierarchy defined in Java. Catching Exceptions Just like other programming languages, Clojure provides the normal ‘try-catch‘ block to catch exceptions as and when they occur. Following is the general syntax of the try-catch block. (try (//Protected code) catch Exception e1) (//Catch block) All of your code which could raise an exception is placed in the Protected code block. In the catch block, you can write custom code to handle your exception so that the application can recover from the exception. Let’s look at our earlier example which generated a file-not-found exception and see how we can use the try catch block to catch the exception raised by the program. Live Demo (ns clojure.examples.example (:gen-class)) (defn Example [] (try (def string1 (slurp “Example.txt”)) (println string1) (catch Exception e (println (str “caught exception: ” (.getMessage e)))))) (Example) The above program produces the following output. caught exception: Example.txt (No such file or directory) From the above code, we wrap out faulty code in the try block. In the catch block, we are just catching our exception and outputting a message that an exception has occurred. So, we now have a meaningful way of capturing the exception, which is generated by the program. Multiple Catch Blocks One can have multiple catch blocks to handle multiple types of exceptions. For each catch block, depending on the type of exception raised you would write code to handle it accordingly. Let’s modify our earlier code to include two catch blocks, one which is specific for our file not found exception and the other is for a general exception block. Live Demo (ns clojure.examples.example (:gen-class)) (defn Example [] (try (def string1 (slurp “Example.txt”)) (println string1) (catch java.io.FileNotFoundException e (println (str “caught file exception: ” (.getMessage e)))) (catch Exception e (println (str “caught exception: ” (.getMessage e))))) (println “Let”s move on”)) (Example) The above program produces the following output. caught file exception: Example.txt (No such file or directory) Let”s move on From the above output, we can clearly see that our exception was caught by the ‘FileNotFoundException’ catch block and not the general one. Finally Block The finally block follows a try block or a catch block. A finally block of code always executes, irrespective of occurrence of an Exception. Using a finally block allows you to run any cleanup-type statements that you want to execute, no matter what happens in the protected code. Following is the syntax for this block. (try (//Protected code) catch Exception e1) (//Catch block) (finally //Cleanup code) Let’s modify the above code and add the finally block of code. Following is the code snippet. Live Demo (ns clojure.examples.example (:gen-class)) (defn Example [] (try (def string1 (slurp “Example.txt”)) (println string1) (catch java.io.FileNotFoundException e (println (str “caught file exception: ” (.getMessage e)))) (catch Exception e (println (str “caught exception: ” (.getMessage e)))) (finally (println “This is our final block”))) (println “Let”s move on”)) (Example) The above program produces the following output. caught file exception: Example.txt (No such file or directory) This is our final block Let”s move on From the above program, you can see that the final block is also implemented after the catch block catches the required exception. Since Clojure derives its exception handling from Java, similar to Java, the following methods are available in Clojure for managing the exceptions. public String getMessage() − Returns a detailed message about the exception that has occurred. This message is initialized in the Throwable constructor. public Throwable getCause() − Returns the cause of the exception as represented by a Throwable object. public String toString() − Returns the name of the class concatenated with the result of getMessage(). public void printStackTrace() − Prints the

Clojure – Namespaces

Clojure – Namespaces ”; Previous Next 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]) In the above statement, ‘clojure.set’ is a namespace which contains various classes and methods to be used in the program. For example, the above namespace contains the function called map-invert, which is used to invert a map of key-values. We cannot use this function unless we explicitly tell our program to include this namespace. Let’s look at the different methods available for namespaces. Sr.No. Methods & Description 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. 9 un-alias Returns a map containing only those entries in map whose key is in keys. Print Page Previous Next Advertisements ”;

Clojure – Sets

Clojure – Sets ”; Previous Next 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. Live Demo (ns clojure.examples.example (:gen-class)) (defn example [] (println (set ”(1 1 2 2)))) (example) Output The above code produces the following output. #{1,2} Following are the methods available in Clojure for sets. Sr.No. Sets & Description 1 sorted-set Returns a sorted set of elements. 2 get Returns the element at the index position. 3 contains? Finds out whether the set contains a certain element or not. 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 Return a set that is the union of the input sets 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? Print Page Previous Next Advertisements ”;

Clojure – Automated Testing

Clojure – Automated Testing ”; Previous Next In this chapter, let’s discuss automated testing options provided by Clojure. Testing for Client Applications In order to use testing for Clojure framework, you have to use the dependencies located at https://github.com/slagyr/speclj#manual-installation This URL provides the speclj framework, which is used as a Test data driven or Behaviour driven test framework for Clojure. You have to ensure that you use the Clojure 1.7.0 framework when using any of the ‘speclj’ libraries. By default, the test files will be different from the Clojure code files and need to be placed in a ‘spec’ directory. Following is a sample code for a test file. (ns change.core-spec (:require [speclj.core :refer :all])) (describe “Truth” (it “is true” (should true)) (it “is not false” (should-not false))) (run-specs) Following things need to be noted about the above code − We first have to ensure to use the ‘require’ statement to include all the core libraries in the ‘speclj’ framework. Next is the ‘describe’ function. This is used to provide a description for the test case being created. Next function is the ‘it’ function, which is the actual test case. In the first test case, the “is true” string is the name given to the test case. Should and should-not are known as assertions. All assertions begin with should. Should and should-not are just two of the many assertions available. They both take expressions that they will check for truthy-ness and falsy-ness respectively. If you run the test case, you will get the following output. The output shows the time taken in milliseconds for the test case to run. ←[32m.←[0m←[32m.←[0m Finished in 0.00014 seconds Testing for Web-based Applications Selenium is one of the key frameworks used for testing modern day web-based applications. Clojure libraries are also available which can be used for testing web-based applications. Let’s look at how we can use the Selenium libraries for testing Clojure web-based applications. Step 1 − The first step is to ensure we are using the Ring and Compojure framework to create a web-based application, which needs to be tested. Let’s use one of the examples from our earlier chapters. The following code is a simple web application, which displays “Hello World” in the browser. (ns my-webapp.handler (:require [compojure.core :refer :all] [compojure.route :as route] [ring.middleware.defaults :refer [wrap-defaults site-defaults]])) (defroutes app-routes (GET “/” [] “Hello World”) (route/not-found “Not Found”)) (def app (wrap-defaults app-routes site-defaults)) Step 2 − Next make sure to download the selenium jar file https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-server/2.47.0 and include it in your classpath. Step 3 − Also ensure to download the ‘clj’ web driver, which will be used for running the web test from the following location. https://clojars.org/clj-webdriver/versions/0.7.1 Step 4 − In your project directory, create another directory called features and create a file called ‘config.clj’. Step 5 − Next add the following code to the ‘config.clj’ file created in the earlier step. ns clj-webdriver-tutorial.features.config) (def test-port 3000) (def test-host “localhost”) (def test-base-url (str “http://” test-host “:” test-port “/”)) The above code basically tells the web test framework to test the application, which gets loaded at the URL http://localhost:3000 Step 6 − Finally, let’s write our code to carry out our test. (ns clj-webdriver-tutorial.features.homepage (:require [clojure.test :refer :all] [ring.adapter.jetty :refer [run-jetty]] [clj-webdriver.taxi :refer :all] [clj-webdriver-tutorial.features.config :refer :all] [clj-webdriver-tutorial.handler :refer [app-routes]])) (ns clj-webdriver-tutorial.features.homepage (:require [clojure.test :refer :all] [ring.adapter.jetty :refer [run-jetty]] [clj-webdriver.taxi :refer :all] [clj-webdriver-tutorial.features.config :refer :all] [clj-webdriver-tutorial.handler :refer [app-routes]])) (defn start-server [] (loop [server (run-jetty app-routes {:port test-port, :join? false})] (if (.isStarted server) server (recur server)))) (defn stop-server [server] (.stop server)) (defn start-browser [] (set-driver! {:browser :firefox})) (defn stop-browser [] (quit)) (deftest homepage-greeting (let [server (start-server)] (start-browser) (to test-base-url) (is (= (text “body”) “Hello World”)) (stop-browser) (stop-server server))) The above code is going to take the following actions − Start the server for the application. Open the root path in the browser. Check if the “Hello World” message is present on the page. Close the browser. Shut down the server. Print Page Previous Next Advertisements ”;

Clojure – Useful Resources

Clojure – Useful Resources ”; Previous Next The following resources contain additional information on Clojure. Please use them to get more in-depth knowledge on this topic. Python Programming Certification 2024 Most Popular  9 Courses     1 eBooks Tutorialspoint More Detail Artificial Intelligence and Machine Learning Certification 2024 Most Popular  7 Courses     1 eBooks Tutorialspoint More Detail Java Certification 2024 Best Seller  7 Courses     1 eBooks Tutorialspoint More Detail Print Page Previous Next Advertisements ”;

Clojure – Applications

Clojure – Applications ”; Previous Next Clojure has some contributed libraries which have the enablement for creating Desktop and Web-based applications. Let’s discuss each one of them. Sr.No. Applications & Description 1 Desktop – See-saw See-saw is a library which can be used for creating desktop applications. 2 Desktop – Changing the Value of Text The value of the content in the window can be changed by using the ‘config!’ option. In the following example the config! option is used to change the window content to the new value of “Good Bye”. 3 Desktop – Displaying a Modal Dialog Box A modal dialog box can be shown by using the alert method of the see-saw class. The method takes the text value, which needs to be shown in the modal dialog box. 4 Desktop – Displaying Buttons Buttons can be displayed with the help of the button class. 5 Desktop – Displaying Labels Labels can be displayed with the help of the label class. 6 Desktop – Displaying Text Fields Text Fields can be displayed with the help of the text class. Web Applications – Introduction To create a web application in Clojure you need to use the Ring application library, which is available at the following link https://github.com/ring-clojure/ring You need to ensure you download the necessary jars from the site and ensure to add it as a dependency for the Clojure application. The Ring framework provides the following capabilities − Sets things up such that an http request comes into your web application as a regular Clojure HashMap, and likewise makes it so that you can return a response as a HashMap. Provides a specification describing exactly what those request and response maps should look like. Brings along a web server (Jetty) and connects your web application to it. The Ring framework automatically can start a web server and ensures the Clojure application works on this server. Then one can also use the Compojure framework. This allows one to create routes which is now how most modern web applications are developed. Creating your first Clojure application − The following example shows how you can create your first web application in Clojure. (ns my-webapp.handler (:require [compojure.core :refer :all] [compojure.route :as route] [ring.middleware.defaults :refer [wrap-defaults site-defaults]])) (defroutes app-routes (GET “/” [] “Hello World”) (route/not-found “Not Found”)) (def app (wrap-defaults app-routes site-defaults)) Let’s look at the following aspects of the program − The ‘defroutes’ is used to create routes so that request made to the web application to different routes can be directed to different functions in your Clojure application. In the above example, the “/” is known as the default route, so when you browse to the base of your web application, the string “Hello World” will be sent to the web browser. If the user hits any url which cannot be processed by the Clojure application, then it will display the string “Not Found”. When you run the Clojure application, by default your application will be loaded as localhost:3000, so if you browse to this location, you will receive the following output. Web Applications – Adding More Routes to Your Web Application You can also add more routes to your web application. The following example shows how to achieve this. (ns my-webapp.handler (:require [compojure.core :refer :all] [compojure.route :as route] [ring.middleware.defaults :refer [wrap-defaults site-defaults]])) (defroutes app-routes (GET “/” [] “Hello World”) (GET “/Tutorial” [] “This is a tutorial on Clojure”) (route/not-found “Not Found”)) (def app (wrap-defaults app-routes site-defaults)) You can see that adding a route in the application is as easy as just adding another GET function with the url route. (GET “/Tutorial” [] “This is a tutorial on Clojure”) If you browse to the location http://localhost:3000/Tutorial, you will receive the following output. Print Page Previous Next Advertisements ”;