SlideShare a Scribd company logo
Clojure Example Functions

                        ilegra - LDC
By Jackson dos Santos
frequencies
user => (frequencies '(1 2 3))
{1 1, 2 1, 3 1}
gensym
user => (gensym "Blastoise")
Blastoise342


user=> (gensym)
G__345
memfn
user=> (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)
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)
)
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)))
)
Java and Clojure Integration
RT.loadResourceScript("cartax.clj");


Var carType = RT.var("cartax", "car");
return carType.get();
Java and Clojure Integration
RT.loadResourceScript("cartax.clj");


Var carType = RT.var("cartax", "car");
return carType.get();
Java and Clojure Integration
Var 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”)
Java and Clojure Integration
try {
        Var t = RT.var("cartax", "verify_car_speed");
        return t.invoke(carStruct);
} catch (Exception e) {
        throw new RuntimeException(e);
}
Java and Clojure Integration
try {
        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);
}
4Clojure Problems Solved
4Clojure Problem
4Clojure Problem

More Related Content

PDF
RxJS 5 in Depth
PDF
Functional JavaScript for everyone
PPTX
Correcting Common .NET Async/Await Mistakes
PPTX
Correcting Common Async/Await Mistakes in .NET
PDF
Modern c++ Memory Management
PDF
C c++-meetup-1nov2017-autofdo
DOCX
Jarmo van de Seijp Shadbox ERC223
PDF
Understanding the nodejs event loop
RxJS 5 in Depth
Functional JavaScript for everyone
Correcting Common .NET Async/Await Mistakes
Correcting Common Async/Await Mistakes in .NET
Modern c++ Memory Management
C c++-meetup-1nov2017-autofdo
Jarmo van de Seijp Shadbox ERC223
Understanding the nodejs event loop

What's hot (20)

PDF
Grailsでドメイン駆動設計を実践する時の勘所
PDF
Ns2: OTCL - PArt II
PDF
Ns2: Introduction - Part I
TXT
Penjualan swalayan
PDF
Меняем javascript с помощью javascript
PDF
rx.js make async programming simpler
PDF
NS2: AWK and GNUplot - PArt III
PDF
Google App Engine Developer - Day3
PDF
Multi qubit entanglement
PDF
Pemrograman visual
PPT
Gearmam, from the_worker's_perspective copy
PDF
Design Patterns in .Net
PDF
Add Some Fun to Your Functional Programming With RXJS
PDF
Gearman, from the worker's perspective
PPTX
Kamil Chmielewski, Jacek Juraszek - "Hadoop. W poszukiwaniu złotego młotka."
PDF
Map/reduce, geospatial indexing, and other cool features (Kristina Chodorow)
DOCX
Exchange 2010/2013 Exchange Management Shell Command
PDF
Compose Async with RxJS
PDF
Quantum neural network
PDF
Broken windows de práticas ágeis
Grailsでドメイン駆動設計を実践する時の勘所
Ns2: OTCL - PArt II
Ns2: Introduction - Part I
Penjualan swalayan
Меняем javascript с помощью javascript
rx.js make async programming simpler
NS2: AWK and GNUplot - PArt III
Google App Engine Developer - Day3
Multi qubit entanglement
Pemrograman visual
Gearmam, from the_worker's_perspective copy
Design Patterns in .Net
Add Some Fun to Your Functional Programming With RXJS
Gearman, from the worker's perspective
Kamil Chmielewski, Jacek Juraszek - "Hadoop. W poszukiwaniu złotego młotka."
Map/reduce, geospatial indexing, and other cool features (Kristina Chodorow)
Exchange 2010/2013 Exchange Management Shell Command
Compose Async with RxJS
Quantum neural network
Broken windows de práticas ágeis
Ad

Viewers also liked (7)

PDF
Clojure functions
PPSX
Celery Introduction
PDF
Clojure functions examples
PDF
Clojure functions 3
Ad

Similar to Clojure functions 4 (20)

PDF
Use Kotlin scripts and Clova SDK to build your Clova extension
PPTX
Fact, Fiction, and FP
DOCX
A Shiny Example-- R
PPTX
Flying Futures at the same sky can make the sun rise at midnight
PDF
Юрий Буянов «Squeryl — ORM с человеческим лицом»
PPTX
Super Advanced Python –act1
ODP
Concurrency on the JVM
PDF
Pragmatic Real-World Scala (short version)
PDF
Pragmatic Real-World Scala
PDF
Emerging Languages: A Tour of the Horizon
PDF
リローダブルClojureアプリケーション
PDF
Flux and InfluxDB 2.0 by Paul Dix
DOCX
C programs
PPTX
Tools for Making Machine Learning more Reactive
PDF
ClojureScript loves React, DomCode May 26 2015
PDF
Refactoring to Macros with Clojure
PPTX
jQuery Foot-Gun Features
PDF
Cycle.js: Functional and Reactive
PDF
Flask patterns
PPT
Ken 20150306 心得分享
Use Kotlin scripts and Clova SDK to build your Clova extension
Fact, Fiction, and FP
A Shiny Example-- R
Flying Futures at the same sky can make the sun rise at midnight
Юрий Буянов «Squeryl — ORM с человеческим лицом»
Super Advanced Python –act1
Concurrency on the JVM
Pragmatic Real-World Scala (short version)
Pragmatic Real-World Scala
Emerging Languages: A Tour of the Horizon
リローダブルClojureアプリケーション
Flux and InfluxDB 2.0 by Paul Dix
C programs
Tools for Making Machine Learning more Reactive
ClojureScript loves React, DomCode May 26 2015
Refactoring to Macros with Clojure
jQuery Foot-Gun Features
Cycle.js: Functional and Reactive
Flask patterns
Ken 20150306 心得分享

More from Jackson dos Santos Olveira (20)

PDF
AWS Control Tower
PDF
PDF
An introduction to predictionIO
PDF
Introduction to HashiCorp Consul
PDF
Apache mahout - introduction
PDF
Managing computational resources with Apache Mesos
PDF
Introduction to CFEngine
PDF
PDF
Jboss Teiid - The data you have on the place you need
PDF
Apache PIG introduction
PPSX
Jboss AS7 New Main Features
PPT
Elastic search introduction
PPT
Presentation about ClosureScript fraemework
PDF
Clojure functions midje
AWS Control Tower
An introduction to predictionIO
Introduction to HashiCorp Consul
Apache mahout - introduction
Managing computational resources with Apache Mesos
Introduction to CFEngine
Jboss Teiid - The data you have on the place you need
Apache PIG introduction
Jboss AS7 New Main Features
Elastic search introduction
Presentation about ClosureScript fraemework
Clojure functions midje

Recently uploaded (20)

PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Modernizing your data center with Dell and AMD
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
cuic standard and advanced reporting.pdf
PPTX
breach-and-attack-simulation-cybersecurity-india-chennai-defenderrabbit-2025....
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Advanced IT Governance
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
NewMind AI Monthly Chronicles - July 2025
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
KodekX | Application Modernization Development
PPTX
Cloud computing and distributed systems.
PDF
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf
Review of recent advances in non-invasive hemoglobin estimation
Advanced methodologies resolving dimensionality complications for autism neur...
Modernizing your data center with Dell and AMD
NewMind AI Weekly Chronicles - August'25 Week I
cuic standard and advanced reporting.pdf
breach-and-attack-simulation-cybersecurity-india-chennai-defenderrabbit-2025....
MYSQL Presentation for SQL database connectivity
Unlocking AI with Model Context Protocol (MCP)
Advanced IT Governance
Network Security Unit 5.pdf for BCA BBA.
Spectral efficient network and resource selection model in 5G networks
Per capita expenditure prediction using model stacking based on satellite ima...
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
The AUB Centre for AI in Media Proposal.docx
NewMind AI Monthly Chronicles - July 2025
“AI and Expert System Decision Support & Business Intelligence Systems”
20250228 LYD VKU AI Blended-Learning.pptx
KodekX | Application Modernization Development
Cloud computing and distributed systems.
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf

Clojure functions 4

  • 1. Clojure Example Functions ilegra - LDC By Jackson dos Santos
  • 2. frequencies user => (frequencies '(1 2 3)) {1 1, 2 1, 3 1}
  • 3. gensym user => (gensym "Blastoise") Blastoise342 user=> (gensym) G__345
  • 4. memfn user=> (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 Integration RT.loadResourceScript("cartax.clj"); Var carType = RT.var("cartax", "car"); return carType.get();
  • 8. Java and Clojure Integration RT.loadResourceScript("cartax.clj"); Var carType = RT.var("cartax", "car"); return carType.get();
  • 9. Java and Clojure Integration Var 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 Integration try { Var t = RT.var("cartax", "verify_car_speed"); return t.invoke(carStruct); } catch (Exception e) { throw new RuntimeException(e); }
  • 11. Java and Clojure Integration try { 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); }