Your SlideShare is downloading. ×
Clojure functions 4
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Clojure functions 4

455

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

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

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Clojure Example Functions ilegra - LDCBy Jackson dos Santos
  • 2. frequenciesuser => (frequencies (1 2 3)){1 1, 2 1, 3 1}
  • 3. gensymuser => (gensym "Blastoise")Blastoise342user=> (gensym)G__345
  • 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. 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. 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. Java and Clojure IntegrationRT.loadResourceScript("cartax.clj");Var carType = RT.var("cartax", "car");return carType.get();
  • 8. Java and Clojure IntegrationRT.loadResourceScript("cartax.clj");Var carType = RT.var("cartax", "car");return carType.get();
  • 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. Java and Clojure Integrationtry { Var t = RT.var("cartax", "verify_car_speed"); return t.invoke(carStruct);} catch (Exception e) { throw new RuntimeException(e);}
  • 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. 4Clojure Problems Solved
  • 13. 4Clojure Problem
  • 14. 4Clojure Problem

×