Successfully reported this slideshow.
Upcoming SlideShare
×

# Clojure functions examples

910 views

Published on

Published in: Economy & Finance, Business
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

### Clojure functions examples

1. 1. Clojure Example Functions ilegra - LDCBy Jackson dos Santos
2. 2. odduser =>(odd? 1)true
3. 3. oddThe same thing in java(2 % 1) ? true : false
4. 4. evenuser => (even? 2)true
5. 5. evenThe same thing in java(2 % 2) ? true : false
6. 6. dotimesuser => (dotimes [i 2] (println i))012
7. 7. dotimesThe same thing in javaVector v = new Vector();v.add(0);v.add(1);v.add(2);for (int i = 0; i < v.size(); i++) {System.out.println(v.get(i));}
8. 8. Car tax function(defn verify_car_speed [car] (if (> 100 (get car :cur-speed)) true (do (def extraTax (* (- (get car :cur-speed) 100) 20)) (assoc car :tax (+ extraTax 10)) ) ))
9. 9. Car tax tests(deftest test-car-speed-with-less-than-100 (def carToTest (struct car "FUCA" "PASSANGER" 99 1)) (def carReturned (verify_car_speed carToTest)) (is (= nil (get carReturned :tax))))(deftest test-car-speed-with-more-than-100 (def carToTest (struct car "FUCA" "PASSANGER" 101 1)) (def carReturned (verify_car_speed carToTest)) (is (= 30 (get carReturned :tax))))
10. 10. Bank functions(defn bank-deposit [bank-account value-to-deposit] (def value-to-credit (+ (get @bank-account :value) value-to-deposit)) (dosync (ref-set bank-account (assoc @bank-account :value value-to-credit)) ))(defn bank-withdraw[bank-account value-to-withdraw] (def value-to-dec (- (get @bank-account :value) value-to-withdraw)) (dosync (ref-set bank-account (assoc @bank-account :value value-to-dec)) ))(defn bank-assesment[bank-account] (str "Client " (get @bank-account :name)" has " (get @bank-account :value) " in his account"))
11. 11. Bank tests(deftest test-deposit-100 (def client-rich (ref (struct bank-account "Eike Batista" 100.02M))) (bank-deposit client-rich 100) (is (= 200.02 (get @client-rich :value))))(deftest test-withdraw-99.03 (def client-poor (ref (struct bank-account "Jackson" 224.07M))) (bank-withdraw client-poor 99.03M) (is (= 125.04 (get @client-poor :value))))(deftest test-assesment (def rich-client (ref (struct bank-account "Silvio Santos" 1000))) (bank-deposit rich-client 100) (is (= "Client Silvio Santos has 1100 in his account" (bank-assesment rich-client))))