Upcoming SlideShare
×

# Clojure functions 4

474

Published on

0 Likes
Statistics
Notes
• Full Name
Comment goes here.

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

• Be the first to like this

Views
Total Views
474
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
3
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Clojure functions 4

1. 1. Clojure Example Functions ilegra - LDCBy Jackson dos Santos
2. 2. frequenciesuser => (frequencies (1 2 3)){1 1, 2 1, 3 1}
3. 3. gensymuser => (gensym "Blastoise")Blastoise342user=> (gensym)G__345
4. 4. memfnuser=> (map (memfn charAt i) ["fred" "ethel" "lucy"] [1 2 3])(r h y)user=>(map #(.charAt %1 %2) ["fred" "ethel" "lucy"] [1 2 3])(r h y)
5. 5. bank-transfer(defn bank-withdraw[bank-account value-to-withdraw] (dosync (ref-set bank-account (assoc @bank-account :value (- (get @bank-account :value) value-to-withdraw))) ))(defn bank-deposit [bank-account value-to-deposit] (dosync (ref-set bank-account (assoc @bank-account :value (+ (get @bank-account :value) value-to-deposit))) )(defn bank-transfer[account-to-debit account-to-credit value] (bank-deposit account-to-credit value) (bank-withdraw account-to-debit value))
6. 6. bank-transfer(deftest test-bank-transfer (def client1 (ref (struct bank-account "Blastoise" 151))) (def client2 (ref (struct bank-account "Pikachu" 1500))) (bank-transfer client2 client1 132.98M) (is (= 283.98M (get @client1 :value))) (is (= 1367.02M (get @client2 :value))))
7. 7. Java and Clojure IntegrationRT.loadResourceScript("cartax.clj");Var carType = RT.var("cartax", "car");return carType.get();
8. 8. Java and Clojure IntegrationRT.loadResourceScript("cartax.clj");Var carType = RT.var("cartax", "car");return carType.get();
9. 9. Java and Clojure IntegrationVar structMap = RT.var("clojure.core", "struct-map");return structMap.invoke(carStruct,getStructMapKeyVal("owner-name"), ownerName,getStructMapKeyVal("car-type"), carType,getStructMapKeyVal("cur-speed"), curSpeed,getStructMapKeyVal("licence-plate"), licencePlate)(struct-map car :owner-name “valor” :car-type “valor” :cur-speed valor :licence-plate “valor”)
10. 10. Java and Clojure Integrationtry { Var t = RT.var("cartax", "verify_car_speed"); return t.invoke(carStruct);} catch (Exception e) { throw new RuntimeException(e);}
11. 11. Java and Clojure Integrationtry { RT.loadResourceScript("cartax.clj"); Object car = buildCar(getCarReference(), "Jackson", "Fuca", 101, "AAAA"); Object verifiedCar = verifyCarSpeed(car); System.out.println(verifiedCar);} catch (Exception e) { throw new RuntimeException(e);}
12. 12. 4Clojure Problems Solved
13. 13. 4Clojure Problem
14. 14. 4Clojure Problem
1. #### A particular slide catching your eye?

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