Java Magazine, September/October 2017
ORACLE COM JAVAMAGAZINE SEPTEMBER OCTOBER 2017 70 jvm languages values in a list is handled with a function that accepts another function and a list as an argument For example you can use apply to apply a function to each value in a given list You can choose to use an anonymous function as the parameter or one already bound in a namespace or local binding You could write the initial example which doubled values in one of these two ways ns demo hello apply def double x x 2 apply double list 1 2 3 4 2 4 6 8 apply 2 list 1 2 3 4 2 4 6 8 If youve worked with Lisp or a metalanguage before youve probably come to expect a certain finesse from functional programming languages known as destructuring It is a convenient way of extracting multiple values from data stored in possibly nested key value pairs and lists Essentially destructuring allows you to declaratively define the pieces of data from a data structure that youd like to use or bind Destructuring is ofered by Clojure out of the box and it makes working with complex data structures much easier If you have a function that takes in a list of keywords and returns the first keyword as a string you might write something like this ns demo hello transform defn first keyword string my list name first my list
You must have JavaScript enabled to view digital editions.