Java Magazine, Sept/Oct 2017
ORACLE COM JAVAMAGAZINE SEPTEMBER OCTOBER 2017 69 jvm languages approaches this paradigm through a very common functional toolkit consisting of immutable persistent data structures and first class functions Lets compare this approach with a map in Java Here is the Java code import java util HashMap HashMap String Integer map new HashMap map put java 7 System out println map map put java 8 System out println map Im sure what this is doing is rather obvious When it prints the map the first time you have a key named java and a value of 7 When it prints after putting a new value in the map with the java key it prints a map with a key named java and a value of 8 In Clojure this operation would appear like this ns demo hello immutability def map java 7 println map assoc map java 8 println map In Clojure this code will print java 7 both times This is because maps like most data structures in Clojure are immutable The line assoc map java 8 actually returns a new map that is cloned from the original and it would look like this java 8 Immutability is a core concept of the language that allows Clojure to avoid common parallel programming pitfalls such as deadlocks or data races Functions in Clojure are first class This means that they can be stored in data structures or passed around as arguments In fact function application the idea of applying a function to all
You must have JavaScript enabled to view digital editions.