Clojure – Decision Making ”; Previous Next 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. Sr.No. Methods & Description 1 If Statement In Clojure, the condition is an expression which evaluates it to be either true or false. ”If” the condition is true, then statement#1 will be executed, else statement#2 will be executed. 2 If/do Expression The ‘if-do’ expression in Clojure is used to allow multiple expressions to be executed for each branch of the ‘if’ statement. 3 Nested If Statement Multiple ”if” statements embedded inside each other. 4 Case Statement Clojure offers the ‘case’ statement which is similar to the ‘switch’ statement available in the Java programming language. 5 Cond Statement Clojure offers another evaluation statement called the ‘cond’ statement. This statement takes a set of test/expression pairs. Print Page Previous Next Advertisements ”;
Category: clojure
Clojure – REPL
Clojure – REPL ”; Previous Next 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. This process enables a quick feedback cycle that isn’t possible in most other languages. Starting a REPL Session A REPL session can be started in Leiningen by typing the following command in the command line. lein repl This will start the following REPL window. You then start evaluating Clojure commands in the REPL window as required. To start a REPL session in Eclipse, click the Menu option, go to Run As → Clojure Application. This will start a new REPL session in a separate window along with the console output. Conceptually, REPL is similar to Secure Shell (SSH). In the same way that you can use SSH to interact with a remote server, Clojure REPL allows you to interact with a running Clojure process. This feature can be very powerful because you can even attach a REPL toa live production app and modify your program as it runs. Special Variables in REPL REPL includes some useful variables, the one widely used is the special variable *1, *2, and *3. These are used to evaluate the results of the three most recent expressions. Following example shows how these variables can be used. user => “Hello” Hello user => “World” World user => (str *2 *1) HelloWorld In the above example, first two strings are being sent to the REPL output window as “Hello” and “World” respectively. Then the *2 and *1 variables are used to recall the last 2 evaluated expressions. Print Page Previous Next Advertisements ”;