On 05/01/2016 13:44, Josh Kamau wrote:
Here is an extremely simple example:
Problem: add a list of numbers
java: You put something like this in a class in a method
int[] numbers = {1,2,3,4,5,6,7,7,8,4,3} ;
int sum = 0 ;
for(int i = 0; i < sum.length; i++) {
sum = sum + i;
}
return sum;
clojure: You put this in a clojure namespace
(def numbers [1 2 3 4 54 56 6])
;;to get the sum
(reduce + numbers)
You get the idea ;)
Josh
Even more painful is:
public class Klass {
public HashMap<String, String> data = new HashMap<String, String>();
data.put("key1", "value1");
data.put("key2", "value2");
data.put("key3", "value3");
data.put("key4", "value4");
}
.... versus:
(def my-map {:key1 "value1" :key2 "value2" :key3 "value3" :key4 "value4"})
The complexity is compounded in Java if you want mixed-type values or
arbitrary nesting. In Clojure it is pure simplicity:
(def my-map {:key1 "value1" :key2 55 :key3 [:m1 {:k1 "val1" :k2 67} :m2
{:k1 "val1" :k2 [1 2 3 4]}] :key4 #{2 4 "name" :id}})
(get-in my-map [:key3 3 :k2 3]) ; => 4
gvim
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.