Java Magazine, Sept/Oct 2017
ORACLE COM JAVAMAGAZINE SEPTEMBER OCTOBER 2017 64 jvm languages a list of integers and you want to double all of the integers and then sum them You might notice that this is simply a map and reduce problem Syntactically Clojure expresses that exact idea ns demo hello lists def my list 1 2 3 4 5 reduce map 2 my list 30 Clojure has support for an idea known as a keyword which should not be confused with the concept of reserved keywords in other languages including Java such as static or final The code above demonstrates a few simple ideas I define a new list containing the values 1 2 3 4 and 5 In Clojure a list can be defined using a single quote followed by a pair of parentheses containing the values you want in your list I write an expression to handle the reduction reduce takes a function and a list I pass into it the function bound to the symbol and the list returned from the map function Using the list defined on the first line I map over each value and multiply it by two I handle this using an anonymous function provided as the first argument to map The is a placeholder the argument passed into it With multiple arguments you can index them such as 1 and 2 Like most functional languages Clojure takes a high level approach to problems Instead of requiring code to process each individual step functional languages are often seen as a way to write code that explains the problem youre trying to solve instead of worrying about the implementation details such as iterating over a list Clojure for the Java Developer Despite being a compiled language Clojure brings a form of dynamism to the JVM that allows it to feel like a powerful scripting language Every feature of Clojure is supported at runtime as well as at compile time Additionally with Clojure you wont have to leave the comfort of your favorite Java libraries Clojure has full support for Java interoperability which allows you to
You must have JavaScript enabled to view digital editions.