Your SlideShare is downloading. ×
0
Clojure Example Functions                        ilegra - LDCBy Jackson dos Santos
odduser =>(odd? 1)true
oddThe same thing in java(2 % 1) ? true : false
evenuser => (even? 2)true
evenThe same thing in java(2 % 2) ? true : false
dotimesuser => (dotimes [i 2] (println i))012
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...
Car tax function(defn verify_car_speed [car]    (if (> 100 (get car :cur-speed)) true        (do            (def extraTax ...
Car tax tests(deftest test-car-speed-with-less-than-100  (def carToTest (struct car "FUCA" "PASSANGER" 99 1))  (def carRet...
Bank functions(defn bank-deposit [bank-account value-to-deposit]  (def value-to-credit (+ (get @bank-account :value) value...
Bank tests(deftest test-deposit-100  (def client-rich (ref (struct bank-account "Eike Batista" 100.02M)))  (bank-deposit c...
Upcoming SlideShare
Loading in...5
×

Clojure functions examples

741

Published on

Published in: Economy & Finance, Business
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
741
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
4
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "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))))
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×